Skip to content

Commit 1767300

Browse files
committed
Merged pre-release to master branch
1 parent bbade33 commit 1767300

File tree

5 files changed

+198
-28
lines changed

5 files changed

+198
-28
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
],
1717
"require": {
1818
"simplehtmldom/simplehtmldom": "2.0-RC2"
19+
},
20+
"require-dev": {
21+
"phpunit/phpunit": "9.5.24"
1922
}
2023
}

source/Scheme.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace OvieDev\Toungette;
4+
5+
use Exception;
6+
7+
class Scheme
8+
{
9+
public string $fallback;
10+
public readonly array $json;
11+
private array $schema;
12+
private array $keys;
13+
14+
public function __construct($file, $fallback)
15+
{
16+
$json_raw = file_get_contents($file);
17+
if (!$json_raw) {
18+
throw new Exception("Couldn't find file or open it");
19+
}
20+
$this->json = json_decode($json_raw, true);
21+
if (!isset($this->json)) {
22+
throw new Exception("Invalid JSON");
23+
}
24+
$this->schema = $this->json['schema'];
25+
$this->keys = $this->json['keys'];
26+
foreach ($this->keys as $key) {
27+
if (count($key)!=count($this->schema)) {
28+
throw new Exception("Key breaks schema");
29+
}
30+
}
31+
$this->fallback = $fallback;
32+
}
33+
34+
public function reset_scheme(): void
35+
{
36+
$this->schema = $this->json['schema'];
37+
$this->keys = $this->json['keys'];
38+
}
39+
40+
public function modify_key(string $keyname, array $keys): void
41+
{
42+
if (count($keys)!=count($this->schema)) {
43+
throw new Exception("Key order doesn't match the schema");
44+
}
45+
$this->keys[$keyname] = $keys;
46+
}
47+
48+
public function delete_key(string $keyname): void
49+
{
50+
if (!array_key_exists($keyname, $this->keys)) {
51+
throw new Exception("Key doesn't exists");
52+
}
53+
54+
unset($this->keys[$keyname]);
55+
}
56+
57+
public function push_to_schema(string $lang, bool $use_fallback, array $values): void
58+
{
59+
$this->schema[] = $lang;
60+
if ($use_fallback) {
61+
foreach ($this->keys as &$key) {
62+
$key[] = $this->fallback;
63+
}
64+
}
65+
else {
66+
if (count($values)==count($this->keys)) {
67+
$i = 0;
68+
foreach ($this->keys as &$key) {
69+
$key[] = $values[$i];
70+
$i++;
71+
}
72+
}
73+
else {
74+
throw new Exception('$values don\'t match keys number');
75+
}
76+
}
77+
}
78+
79+
public function pop_from_schema(): void {
80+
array_pop($this->schema);
81+
foreach ($this->keys as &$key) {
82+
array_pop($key);
83+
}
84+
}
85+
86+
public function get_keys()
87+
{
88+
return $this->keys;
89+
}
90+
91+
public function get_schema()
92+
{
93+
return $this->schema;
94+
}
95+
96+
public function get_key_with_lang(string $key, string $lang): string
97+
{
98+
if (!in_array($lang, $this->schema)) {
99+
$lang = $this->schema[0];
100+
}
101+
if (!array_key_exists($key, $this->keys)) {
102+
throw new Exception("Key doesn't exists");
103+
}
104+
105+
return $this->keys[$key][array_search($lang, $this->schema)];
106+
}
107+
108+
}

source/Translator.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33

44
namespace OvieDev\Toungette;
5-
use Exception;
5+
use OvieDev\Toungette\Scheme;
66
use simplehtmldom\HtmlDocument;
77

88
class Translator
99
{
10-
private string $template_path;
1110
private string $page_template;
12-
private array $json_template;
11+
public Scheme $scheme;
1312
public string $lang;
14-
private array $langs;
1513
public string $text;
1614

1715
public function __construct($template, $page, $lang=null) {
18-
$this->template_path = $template;
1916
$this->page_template = $page;
2017
if (isset($_GET['lang'])) {
2118
$this->lang = $_GET['lang'];
@@ -26,32 +23,13 @@ public function __construct($template, $page, $lang=null) {
2623
else {
2724
$this->lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
2825
}
29-
$this->parse_lang_template();
30-
}
31-
32-
public function parse_lang_template(): void
33-
{
34-
$json_raw = file_get_contents($this->template_path);
35-
if (!$json_raw) {
36-
throw new Exception("Couldn't find file or open it");
37-
}
38-
$json = json_decode($json_raw, true);
39-
if (!isset($json)) {
40-
throw new Exception("Invalid JSON");
41-
}
42-
$this->json_template = $json;
43-
$this->langs = $this->json_template['schema'];
44-
foreach ($this->json_template['keys'] as $key) {
45-
if (count($key)!=count($this->langs)) {
46-
throw new Exception("Key breaks schema");
47-
}
48-
}
26+
$this->scheme = new Scheme($template, "Fallback text");
4927
}
5028

5129
private function get_lang_index(): int
5230
{
5331
$index = 0;
54-
foreach ($this->langs as $l) {
32+
foreach ($this->scheme->get_schema() as $l) {
5533
if ($l==$this->lang) {
5634
return $index;
5735
}
@@ -90,9 +68,9 @@ private function translate_links($page): string
9068
public function translate(): void
9169
{
9270
$html = file_get_contents($this->page_template);
93-
foreach (array_keys($this->json_template['keys']) as $key) {
71+
foreach (array_keys($this->scheme->get_keys()) as $key) {
9472
$pattern = '/(?<!\/){'.$key.'}/';
95-
$html = preg_replace($pattern, $this->json_template['keys'][$key][$this->get_lang_index()], $html);
73+
$html = preg_replace($pattern, $this->scheme->get_keys()[$key][$this->get_lang_index()], $html);
9674
}
9775
$html = preg_replace('/(?<=[\s\t\n])\//', '', $html);
9876
$html = $this->translate_links($html);

tests/SchemeTests.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php declare(strict_types=1);
2+
use PHPUnit\Framework\TestCase;
3+
use OvieDev\Toungette\Scheme;
4+
5+
final class SchemeTests extends TestCase{
6+
7+
private Scheme $scheme;
8+
9+
protected function setUp(): void
10+
{
11+
$this->scheme = new Scheme("schem.json", "fallback");
12+
}
13+
14+
public function test_add(): void {
15+
$this->scheme->modify_key("key.new", ["hey", "im", "new"]);
16+
17+
$this->assertSame(["hey", "im", "new"], $this->scheme->get_keys()["key.new"]);
18+
}
19+
20+
/**
21+
* @depends test_add
22+
*/
23+
public function test_reset(): void {
24+
$this->scheme = new Scheme("schem.json", "fallback");
25+
$this->scheme->modify_key("key.new", ["hey", "im", "new"]);
26+
$this->scheme->reset_scheme();
27+
28+
$this->assertSame(1, count($this->scheme->get_keys()));
29+
}
30+
31+
32+
public function test_remove_key(): void
33+
{
34+
$this->scheme->delete_key("key.coolkey");
35+
36+
$this->assertArrayNotHasKey("key.coolkey", $this->scheme->get_keys());
37+
38+
$this->expectException(Exception::class);
39+
$this->scheme->delete_key("key.notexist");
40+
}
41+
42+
public function test_push(): void
43+
{
44+
$this->scheme->push_to_schema("de", true, []);
45+
46+
$this->assertContains("de", $this->scheme->get_schema());
47+
$this->assertContains($this->scheme->fallback, $this->scheme->get_keys()["key.coolkey"]);
48+
49+
$this->scheme->push_to_schema("fr", false, ["not a fallback"]);
50+
51+
$this->assertContains("fr", $this->scheme->get_schema());
52+
$this->assertContains("not a fallback", $this->scheme->get_keys()["key.coolkey"]);
53+
}
54+
55+
/**
56+
* @depends test_push
57+
*/
58+
public function test_pop(): void
59+
{
60+
$this->scheme->push_to_schema("de", true, []);
61+
62+
$this->scheme->pop_from_schema();
63+
$this->assertSame(3, count($this->scheme->get_schema()));
64+
}
65+
66+
public function test_find_key(): void
67+
{
68+
$val = $this->scheme->get_key_with_lang("key.coolkey", "en");
69+
$this->assertSame("hi", $val);
70+
}
71+
}

tests/test_component_schem.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"schema": ["en", "pl", "es"],
3+
"keys": {
4+
"key.coolkey": [
5+
"hi",
6+
"cool",
7+
"key"
8+
]
9+
}
10+
}

0 commit comments

Comments
 (0)