From a897c1da90928f9a677439eb2afbd4fb58fde7d3 Mon Sep 17 00:00:00 2001 From: Janne Koschinski <janne@kuschku.de> Date: Mon, 19 Sep 2016 01:15:30 +0200 Subject: [PATCH] Added translation functionality --- backend/helper/RendererHelper.php | 6 ++++- backend/helper/TranslationHelper.php | 38 ++++++++++++++++++++++++++++ backend/helper/ViewHelper.php | 13 +++++++++- res/search.css | 14 ++++++++++ res/search.js | 2 -- res/translations.js | 15 ----------- templates/login.phtml | 6 ++--- templates/search.phtml | 10 +++++--- translations/de.json | 19 ++++++++++++++ translations/en.json | 19 ++++++++++++++ 10 files changed, 116 insertions(+), 26 deletions(-) create mode 100644 backend/helper/TranslationHelper.php delete mode 100644 res/translations.js create mode 100644 translations/de.json create mode 100644 translations/en.json diff --git a/backend/helper/RendererHelper.php b/backend/helper/RendererHelper.php index 54ff2d3..5a06c0e 100644 --- a/backend/helper/RendererHelper.php +++ b/backend/helper/RendererHelper.php @@ -3,12 +3,15 @@ namespace QuasselRestSearch; require_once 'ViewHelper.php'; +require_once 'TranslationHelper.php'; class RendererHelper { private $config; + private $translator; public function __construct(Config $config) { $this->config = $config; + $this->translator = new TranslationHelper($config); } public function renderError($e) { @@ -31,7 +34,8 @@ class RendererHelper { } public function renderPage(string $template, array $vars = null) { - $viewHelper = new ViewHelper($vars); + $translation = $this->translator->loadTranslation($this->translator->findMatchingLanguage($_SERVER['HTTP_ACCEPT_LANGUAGE'])); + $viewHelper = new ViewHelper($translation, $vars); $viewHelper->render($template); } diff --git a/backend/helper/TranslationHelper.php b/backend/helper/TranslationHelper.php new file mode 100644 index 0000000..6e5cdbc --- /dev/null +++ b/backend/helper/TranslationHelper.php @@ -0,0 +1,38 @@ +<?php + +namespace QuasselRestSearch; + +class TranslationHelper { + protected $template_dir; + + public function __construct($translation) { + $this->setPath('../../translations/'); + } + + public function setPath(string $path) { + $this->template_dir = realpath(dirname(__FILE__) . '/' . $path); + } + + public function findMatchingLanguage(string $language_str) : string { + $languages = explode(",", $language_str); + foreach ($languages as $language) { + $language = explode(";", $language)[0]; + if ($this->exists($language)) { + return $language; + } + } + return "en"; + } + + public function exists($language) : bool { + return file_exists($this->path($language)); + } + + private function path($language) : string { + return $this->template_dir . '/' . $language . '.json'; + } + + public function loadTranslation($language) : array { + return json_decode(file_get_contents($this->path($language)), true); + } +} \ No newline at end of file diff --git a/backend/helper/ViewHelper.php b/backend/helper/ViewHelper.php index 7ba94c2..ff7e78a 100644 --- a/backend/helper/ViewHelper.php +++ b/backend/helper/ViewHelper.php @@ -4,9 +4,11 @@ namespace QuasselRestSearch; class ViewHelper { protected $template_dir; + protected $translation; protected $vars = []; - public function __construct($vars = null) { + public function __construct($translation, $vars = null) { + $this->translation = $translation; $this->setPath('../../templates/'); if ($vars !== null) { $this->vars = $vars; @@ -18,6 +20,15 @@ class ViewHelper { } public function render($template_file) { + $translation = $this->translation; + $t = function ($path) use ($translation) { + $arr = explode(".", $path); + $var = $translation; + foreach ($arr as $key) + $var = $var[$key]; + echo $var; + }; + $path = $this->template_dir . '/' . $template_file . '.phtml'; if (file_exists($path)) { include $path; diff --git a/res/search.css b/res/search.css index 60d5ddd..4739af0 100644 --- a/res/search.css +++ b/res/search.css @@ -120,6 +120,13 @@ nav input[type=text]::-moz-placeholder { -moz-osx-font-smoothing: grayscale; } +nav input[type=text]::-webkit-input-placeholder { + color: #ffffff; + opacity: 1; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + nav input[type=text]:focus::-moz-placeholder { color: #757575; opacity: 1; @@ -127,6 +134,13 @@ nav input[type=text]:focus::-moz-placeholder { -moz-osx-font-smoothing: grayscale; } +nav input[type=text]:focus::-webkit-input-placeholder { + color: #757575; + opacity: 1; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; +} + #searchbar { max-width: 1136px; margin: 10px auto; diff --git a/res/search.js b/res/search.js index 0c96b81..ddf2db7 100644 --- a/res/search.js +++ b/res/search.js @@ -1,5 +1,3 @@ -var translation = translations.en; - var state = { query: "", selected_history_entry: -1, diff --git a/res/translations.js b/res/translations.js deleted file mode 100644 index a963c48..0000000 --- a/res/translations.js +++ /dev/null @@ -1,15 +0,0 @@ -var translations = {}; - -translations.en = { - results: { - show_more: "Show More Results", - load_more: "Load More Results" - }, - context: { - load_later: "Load Later Context", - load_earlier: "Load Earlier Context" - }, - history: { - error_unavailable: "No search history available" - } -}; \ No newline at end of file diff --git a/templates/login.phtml b/templates/login.phtml index a73a601..ecf7084 100644 --- a/templates/login.phtml +++ b/templates/login.phtml @@ -2,7 +2,7 @@ <html> <head> <meta charset="utf-8"> - <title>Quassel Search</title> + <title><?php $t('title'); ?></title> <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width"> <meta name="apple-mobile-web-app-capable" content="yes"/> @@ -15,8 +15,8 @@ </head> <body> -<h1>Quassel Search</h1> -<h2>You have to login to access this page</h2> +<h1><?php $t('title'); ?></h1> +<h2><?php $t('login.description'); ?></h2> <form method="post" action="login.php?action=login"> <input name="username" type="text" placeholder="Username"> diff --git a/templates/search.phtml b/templates/search.phtml index 9d16a10..423d31c 100644 --- a/templates/search.phtml +++ b/templates/search.phtml @@ -2,7 +2,7 @@ <html> <head> <meta charset="utf-8"> - <title>Quassel Search</title> + <title><?php $t('title'); ?></title> <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, width=device-width"> <meta name="apple-mobile-web-app-capable" content="yes"/> @@ -17,12 +17,12 @@ <nav> <div class="wrapper"> <div id="searchbar"> - <input name="q" id="q" placeholder="Search" type="text"> + <input name="q" id="q" placeholder="<?php $t('search'); ?>" type="text"> <div id="searchicon" class="icon">search</div> </div> </div> <div id="actions"> - <a title="Logout" href="login.php?action=logout"> + <a title="<?php $t('logout'); ?>" href="login.php?action=logout"> <span class="icon">account_circle</span> </a> </div> @@ -38,7 +38,9 @@ <div id="bg"></div> <script src="res/jquery.js"></script> -<script src="res/translations.js"></script> +<script> + var translation = <?php echo json_encode($translation); ?> +</script> <script src="res/sendercolor.js"></script> <script src="res/search.js"></script> </body> diff --git a/translations/de.json b/translations/de.json new file mode 100644 index 0000000..e834c6a --- /dev/null +++ b/translations/de.json @@ -0,0 +1,19 @@ +{ + "results": { + "show_more": "Show More Results", + "load_more": "Load More Results" + }, + "context": { + "load_later": "Load Later Context", + "load_earlier": "Load Earlier Context" + }, + "history": { + "error_unavailable": "No search history available" + }, + "login": { + "description": "You have to login to access this page" + }, + "search": "Suchen", + "logout": "Abmelden", + "title": "QuasselSearch" +} \ No newline at end of file diff --git a/translations/en.json b/translations/en.json new file mode 100644 index 0000000..2008ab3 --- /dev/null +++ b/translations/en.json @@ -0,0 +1,19 @@ +{ + "results": { + "show_more": "Show More Results", + "load_more": "Load More Results" + }, + "context": { + "load_later": "Load Later Context", + "load_earlier": "Load Earlier Context" + }, + "history": { + "error_unavailable": "No search history available" + }, + "login": { + "description": "You have to login to access this page" + }, + "search": "Search", + "logout": "Logout", + "title": "QuasselSearch" +} \ No newline at end of file -- GitLab