From a1a65bb9f002b8daaf3349452741c5651da3aa31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Goetz?= Date: Thu, 19 Oct 2017 22:18:46 +0200 Subject: [PATCH] Support translations for the UI, fixes #1 --- docs/05_Configuration/_index.md | 35 +++++++++++++++++++++++++++ global.json | 31 ++++++++++++++++++++++++ libs/Format/HTML/ContentPage.php | 4 +-- libs/Format/HTML/Template.php | 22 ++++++++++++++++- libs/Server/ErrorPage.php | 2 +- templates/content.php | 9 ++++--- templates/home.php | 34 +++++++++----------------- templates/layout/00_layout.php | 2 +- templates/layout/05_page.php | 10 ++++---- templates/partials/navbar_content.php | 2 +- 10 files changed, 113 insertions(+), 38 deletions(-) diff --git a/docs/05_Configuration/_index.md b/docs/05_Configuration/_index.md index 3e5ba07..0e6f885 100644 --- a/docs/05_Configuration/_index.md +++ b/docs/05_Configuration/_index.md @@ -107,6 +107,41 @@ Directory structure: │ │ │ ├── 05_Code_Highlighting.md ``` +### Language + +You can change the default language with the `language` option. + +```json +{ + "language": "fr" +} +``` + +This will change the language of the text within Daux.io and also the `` attribute. + +You can override the translations with the `strings` option. +A string that isn't found will fall back to english. + +```json +{ + "strings": { + "fr": { + "CodeBlocks_title": "Afficher le code", + "CodeBlocks_hide": "Non", + "CodeBlocks_below": "En Dessous", + "CodeBlocks_inline": "A côté", + "CodeBlocks_show": "Afficher le code", + "Search_placeholder": "Rechercher...", + "Link_previous": "Précédent", + "Link_next": "Suivant", + "Edit_on": "Editer sur :name:", + "View_on_github": "Voir sur GitHub", + "View_documentation": "Voir la Documentation" + } + } +} +``` + ### Processor You can set the processor in the documentation or as an option to the command line. If you need it when running the server, you should add it to the configuration. diff --git a/global.json b/global.json index 527665d..332ee40 100755 --- a/global.json +++ b/global.json @@ -16,6 +16,37 @@ "folders": [] }, + "language": "en", + + "strings": { + "en": { + "CodeBlocks_title": "Code blocks", + "CodeBlocks_hide": "No", + "CodeBlocks_below": "Below", + "CodeBlocks_inline": "Inline", + "CodeBlocks_show": "Show Code Blocks", + "Search_placeholder": "Search...", + "Link_previous": "Previous", + "Link_next": "Next", + "Edit_on": "Edit on :name:", + "View_on_github": "View On GitHub", + "View_documentation": "View Documentation" + }, + "fr": { + "CodeBlocks_title": "Afficher le code", + "CodeBlocks_hide": "Non", + "CodeBlocks_below": "En Dessous", + "CodeBlocks_inline": "A côté", + "CodeBlocks_show": "Afficher le code", + "Search_placeholder": "Rechercher...", + "Link_previous": "Précédent", + "Link_next": "Suivant", + "Edit_on": "Editer sur :name:", + "View_on_github": "Voir sur GitHub", + "View_documentation": "Voir la Documentation" + } + }, + "timezone": "America/Los_Angeles", "live": { diff --git a/libs/Format/HTML/ContentPage.php b/libs/Format/HTML/ContentPage.php index ab4a3b8..86fdd9a 100644 --- a/libs/Format/HTML/ContentPage.php +++ b/libs/Format/HTML/ContentPage.php @@ -71,7 +71,7 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage $entry_page[$name] = $params['base_page'] . $params['entry_page'][$key]->getUrl(); } } else { - $entry_page['View Documentation'] = $params['base_page'] . $params['entry_page']->getUrl(); + $entry_page['__VIEW_DOCUMENTATION__'] = $params['base_page'] . $params['entry_page']->getUrl(); } } @@ -103,7 +103,7 @@ class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage $context = ['page' => $page, 'params' => $params]; - $template = new Template($params['templates'], $params['theme']['templates']); + $template = new Template($params); return $template->render($this->isLanding() ? 'theme::home' : 'theme::content', $context); } diff --git a/libs/Format/HTML/Template.php b/libs/Format/HTML/Template.php index ccd236e..ebfd3d5 100644 --- a/libs/Format/HTML/Template.php +++ b/libs/Format/HTML/Template.php @@ -9,12 +9,18 @@ class Template { protected $engine; + protected $params; + /** * @param string $base * @param string $theme */ - public function __construct($base, $theme) + public function __construct($params) { + $this->params = $params; + $base = $params['templates']; + $theme = $params['theme']['templates']; + // Use internal templates if no templates // dir exists in the working directory if (!is_dir($base)) { @@ -57,6 +63,20 @@ class Template return $this->renderNavigation($nav); }); + $this->engine->registerFunction('translate', function ($key) { + $language = $this->params['language']; + + if (array_key_exists($key, $this->params['strings'][$language])) { + return $this->params['strings'][$language][$key]; + } + + if (array_key_exists($key, $this->params['strings']['en'])) { + return $this->params['strings']['en'][$key]; + } + + return "Unknown key $key"; + }); + $this->engine->registerFunction('get_breadcrumb_title', function ($page, $base_page) { $title = ''; $breadcrumb_trail = $page['breadcrumb_trail']; diff --git a/libs/Server/ErrorPage.php b/libs/Server/ErrorPage.php index 2c03cb8..f9084d3 100644 --- a/libs/Server/ErrorPage.php +++ b/libs/Server/ErrorPage.php @@ -37,7 +37,7 @@ class ErrorPage extends SimplePage 'language' => '', ]; - $template = new Template($params['templates'], $params['theme']['templates']); + $template = new Template($params); return $template->render('error', ['page' => $page, 'params' => $params]); } diff --git a/templates/content.php b/templates/content.php index b7ec4ec..bb07a89 100644 --- a/templates/content.php +++ b/templates/content.php @@ -12,12 +12,13 @@ $edit_on = $params->getHTML()->getEditOn(); if ($edit_on) { ?> - Edit on + + translate("Edit_on")) ?> + -
@@ -30,11 +31,11 @@