Skip to content

Commit

Permalink
Merged pre-release to master branch
Browse files Browse the repository at this point in the history
  • Loading branch information
OvieDev committed Sep 19, 2022
1 parent bbade33 commit 1767300
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 28 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
],
"require": {
"simplehtmldom/simplehtmldom": "2.0-RC2"
},
"require-dev": {
"phpunit/phpunit": "9.5.24"
}
}
108 changes: 108 additions & 0 deletions source/Scheme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace OvieDev\Toungette;

use Exception;

class Scheme
{
public string $fallback;
public readonly array $json;
private array $schema;
private array $keys;

public function __construct($file, $fallback)
{
$json_raw = file_get_contents($file);
if (!$json_raw) {
throw new Exception("Couldn't find file or open it");
}
$this->json = json_decode($json_raw, true);
if (!isset($this->json)) {
throw new Exception("Invalid JSON");
}
$this->schema = $this->json['schema'];
$this->keys = $this->json['keys'];
foreach ($this->keys as $key) {
if (count($key)!=count($this->schema)) {
throw new Exception("Key breaks schema");
}
}
$this->fallback = $fallback;
}

public function reset_scheme(): void
{
$this->schema = $this->json['schema'];
$this->keys = $this->json['keys'];
}

public function modify_key(string $keyname, array $keys): void
{
if (count($keys)!=count($this->schema)) {
throw new Exception("Key order doesn't match the schema");
}
$this->keys[$keyname] = $keys;
}

public function delete_key(string $keyname): void
{
if (!array_key_exists($keyname, $this->keys)) {
throw new Exception("Key doesn't exists");
}

unset($this->keys[$keyname]);
}

public function push_to_schema(string $lang, bool $use_fallback, array $values): void
{
$this->schema[] = $lang;
if ($use_fallback) {
foreach ($this->keys as &$key) {
$key[] = $this->fallback;
}
}
else {
if (count($values)==count($this->keys)) {
$i = 0;
foreach ($this->keys as &$key) {
$key[] = $values[$i];
$i++;
}
}
else {
throw new Exception('$values don\'t match keys number');
}
}
}

public function pop_from_schema(): void {
array_pop($this->schema);
foreach ($this->keys as &$key) {
array_pop($key);
}
}

public function get_keys()
{
return $this->keys;
}

public function get_schema()
{
return $this->schema;
}

public function get_key_with_lang(string $key, string $lang): string
{
if (!in_array($lang, $this->schema)) {
$lang = $this->schema[0];
}
if (!array_key_exists($key, $this->keys)) {
throw new Exception("Key doesn't exists");
}

return $this->keys[$key][array_search($lang, $this->schema)];
}

}
34 changes: 6 additions & 28 deletions source/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@


namespace OvieDev\Toungette;
use Exception;
use OvieDev\Toungette\Scheme;
use simplehtmldom\HtmlDocument;

class Translator
{
private string $template_path;
private string $page_template;
private array $json_template;
public Scheme $scheme;
public string $lang;
private array $langs;
public string $text;

public function __construct($template, $page, $lang=null) {
$this->template_path = $template;
$this->page_template = $page;
if (isset($_GET['lang'])) {
$this->lang = $_GET['lang'];
Expand All @@ -26,32 +23,13 @@ public function __construct($template, $page, $lang=null) {
else {
$this->lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}
$this->parse_lang_template();
}

public function parse_lang_template(): void
{
$json_raw = file_get_contents($this->template_path);
if (!$json_raw) {
throw new Exception("Couldn't find file or open it");
}
$json = json_decode($json_raw, true);
if (!isset($json)) {
throw new Exception("Invalid JSON");
}
$this->json_template = $json;
$this->langs = $this->json_template['schema'];
foreach ($this->json_template['keys'] as $key) {
if (count($key)!=count($this->langs)) {
throw new Exception("Key breaks schema");
}
}
$this->scheme = new Scheme($template, "Fallback text");
}

private function get_lang_index(): int
{
$index = 0;
foreach ($this->langs as $l) {
foreach ($this->scheme->get_schema() as $l) {
if ($l==$this->lang) {
return $index;
}
Expand Down Expand Up @@ -90,9 +68,9 @@ private function translate_links($page): string
public function translate(): void
{
$html = file_get_contents($this->page_template);
foreach (array_keys($this->json_template['keys']) as $key) {
foreach (array_keys($this->scheme->get_keys()) as $key) {
$pattern = '/(?<!\/){'.$key.'}/';
$html = preg_replace($pattern, $this->json_template['keys'][$key][$this->get_lang_index()], $html);
$html = preg_replace($pattern, $this->scheme->get_keys()[$key][$this->get_lang_index()], $html);
}
$html = preg_replace('/(?<=[\s\t\n])\//', '', $html);
$html = $this->translate_links($html);
Expand Down
71 changes: 71 additions & 0 deletions tests/SchemeTests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php declare(strict_types=1);
use PHPUnit\Framework\TestCase;
use OvieDev\Toungette\Scheme;

final class SchemeTests extends TestCase{

private Scheme $scheme;

protected function setUp(): void
{
$this->scheme = new Scheme("schem.json", "fallback");
}

public function test_add(): void {
$this->scheme->modify_key("key.new", ["hey", "im", "new"]);

$this->assertSame(["hey", "im", "new"], $this->scheme->get_keys()["key.new"]);
}

/**
* @depends test_add
*/
public function test_reset(): void {
$this->scheme = new Scheme("schem.json", "fallback");
$this->scheme->modify_key("key.new", ["hey", "im", "new"]);
$this->scheme->reset_scheme();

$this->assertSame(1, count($this->scheme->get_keys()));
}


public function test_remove_key(): void
{
$this->scheme->delete_key("key.coolkey");

$this->assertArrayNotHasKey("key.coolkey", $this->scheme->get_keys());

$this->expectException(Exception::class);
$this->scheme->delete_key("key.notexist");
}

public function test_push(): void
{
$this->scheme->push_to_schema("de", true, []);

$this->assertContains("de", $this->scheme->get_schema());
$this->assertContains($this->scheme->fallback, $this->scheme->get_keys()["key.coolkey"]);

$this->scheme->push_to_schema("fr", false, ["not a fallback"]);

$this->assertContains("fr", $this->scheme->get_schema());
$this->assertContains("not a fallback", $this->scheme->get_keys()["key.coolkey"]);
}

/**
* @depends test_push
*/
public function test_pop(): void
{
$this->scheme->push_to_schema("de", true, []);

$this->scheme->pop_from_schema();
$this->assertSame(3, count($this->scheme->get_schema()));
}

public function test_find_key(): void
{
$val = $this->scheme->get_key_with_lang("key.coolkey", "en");
$this->assertSame("hi", $val);
}
}
10 changes: 10 additions & 0 deletions tests/test_component_schem.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"schema": ["en", "pl", "es"],
"keys": {
"key.coolkey": [
"hi",
"cool",
"key"
]
}
}

0 comments on commit 1767300

Please sign in to comment.