From 5da0aa8996f0abd40184502c914262c02315f588 Mon Sep 17 00:00:00 2001 From: enrico Date: Wed, 26 Sep 2018 14:59:20 +0200 Subject: [PATCH 1/3] Add italian translation to global.json --- global.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/global.json b/global.json index d222860..1ab8ddf 100755 --- a/global.json +++ b/global.json @@ -76,6 +76,20 @@ "View_on_github": "Bei GitHub anzeigen", "View_documentation": "Dokumentation anzeigen", "Table_of_contents": "Inhaltsverzeichnis" + }, + "it": { + "CodeBlocks_title": "Blocchi di codice", + "CodeBlocks_hide": "No", + "CodeBlocks_below": "Sotto", + "CodeBlocks_inline": "In linea", + "CodeBlocks_show": "Mostra blocchi di codice", + "Search_placeholder": "Cerca...", + "Link_previous": "Pagina precedente", + "Link_next": "Pagina successiva", + "Edit_on": "Modifica su :name:", + "View_on_github": "Guarda su GitHub", + "View_documentation": "Leggi la documentazione", + "Table_of_contents": "Contenuti" } }, From 2bf251201c0c137c7e93915f6bb1e1106d0f4e31 Mon Sep 17 00:00:00 2001 From: enrico Date: Wed, 26 Sep 2018 15:01:00 +0200 Subject: [PATCH 2/3] Add italian translation to global.json --- global.json | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/global.json b/global.json index 1ab8ddf..d222860 100755 --- a/global.json +++ b/global.json @@ -76,20 +76,6 @@ "View_on_github": "Bei GitHub anzeigen", "View_documentation": "Dokumentation anzeigen", "Table_of_contents": "Inhaltsverzeichnis" - }, - "it": { - "CodeBlocks_title": "Blocchi di codice", - "CodeBlocks_hide": "No", - "CodeBlocks_below": "Sotto", - "CodeBlocks_inline": "In linea", - "CodeBlocks_show": "Mostra blocchi di codice", - "Search_placeholder": "Cerca...", - "Link_previous": "Pagina precedente", - "Link_next": "Pagina successiva", - "Edit_on": "Modifica su :name:", - "View_on_github": "Guarda su GitHub", - "View_documentation": "Leggi la documentazione", - "Table_of_contents": "Contenuti" } }, From 0d72c5a0561ad1bebdd6b6d0223cf4abd4c3e41a Mon Sep 17 00:00:00 2001 From: enrico Date: Thu, 27 Sep 2018 10:13:43 +0200 Subject: [PATCH 3/3] Use the page language for the framework translation --- libs/Format/HTML/Template.php | 17 +++- tests/Format/HTML/TableOfContentsTest.php | 3 +- tests/Format/Template/TranslateTest.php | 100 ++++++++++++++++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 tests/Format/Template/TranslateTest.php diff --git a/libs/Format/HTML/Template.php b/libs/Format/HTML/Template.php index fa0900b..baa1488 100644 --- a/libs/Format/HTML/Template.php +++ b/libs/Format/HTML/Template.php @@ -1,4 +1,6 @@ -registerFunction('translate', function ($key) { $language = $this->params['language']; - if (array_key_exists($key, $this->params['strings'][$language])) { - return $this->params['strings'][$language][$key]; + if (isset($this->engine->getData('page')['page'])) { + $page = $this->engine->getData('page'); + if (is_array($page['page'])) { + $language = $page['page']['language']; + } + } + + if (array_key_exists($language, $this->params['strings'])) { + if (array_key_exists($key, $this->params['strings'][$language])) { + return $this->params['strings'][$language][$key]; + } } if (array_key_exists($key, $this->params['strings']['en'])) { diff --git a/tests/Format/HTML/TableOfContentsTest.php b/tests/Format/HTML/TableOfContentsTest.php index 78582d4..d2b50c6 100644 --- a/tests/Format/HTML/TableOfContentsTest.php +++ b/tests/Format/HTML/TableOfContentsTest.php @@ -1,5 +1,6 @@ [ + 'Page.md' => 'some text content', + ], + 'it' => [ + 'Page.md' => 'another page', + ], + ]; + $root = vfsStream::setup('root', null, $structure); + + $config->setDocumentationDirectory($root->url()); + $config['valid_content_extensions'] = ['md']; + $config['mode'] = Daux::STATIC_MODE; + $config['index_key'] = 'index.html'; + + $tree = new Root($config); + Builder::build($tree, []); + + return $tree; + } + + public function translateDataProvider() + { + return [ + ['Previous', 'en'], + ['Pagina precedente', 'it'], + ]; + } + + /** + * @dataProvider translateDataProvider + * + * @param $expectedTranslation + * @param $language + */ + public function testTranslate($expectedTranslation, $language) + { + $current = $language . '/Page.html'; + $entry = $this->prophesize(Entry::class); + + $config = new Config(); + $config['tree'] = $this->getTree($config); + $config['title'] = ''; + $config['index'] = $entry->reveal(); + $config['language'] = $language; + $config['base_url'] = ''; + $config['base_page'] = ''; + $config['templates'] = ''; + $config['page']['language'] = $language; + + $config['html'] = [ + 'search' => '', + 'float' => false, + 'toggle_code' => false, + 'piwik_analytics' => '', + 'google_analytics' => '', + ]; + $config['theme'] = [ + 'js' => [''], + 'css' => [''], + 'fonts' => [''], + 'favicon' => '', + 'templates' => 'name', + ]; + $config['strings'] = [ + 'en' => ['Link_previous' => 'Previous',], + 'it' => ['Link_previous' => 'Pagina precedente',], + ]; + + + $config->setCurrentPage(DauxHelper::getFile($config['tree'], $current)); + + $template = new Template($config); + $value = $template->getEngine($config)->getFunction('translate')->call(null, ['Link_previous']); + + $this->assertEquals($expectedTranslation, $value); + } +}