From e0b3d0921afd73aa7f3466c711f6816a30e4f2fb Mon Sep 17 00:00:00 2001 From: loblao <12ksit@gmail.com> Date: Fri, 2 Sep 2016 19:57:43 -0300 Subject: [PATCH 01/10] i18n: Store user culture in session --- controller/Session.class.php | 3 ++- lib/i18n.class.php | 5 +++++ web/index.php | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/controller/Session.class.php b/controller/Session.class.php index 03e38e39..d074d2c7 100644 --- a/controller/Session.class.php +++ b/controller/Session.class.php @@ -6,7 +6,8 @@ class Session KEY_DOWNLOAD_ALLOWED = 'beta_download_allowed2', KEY_PREFINERY_USER_ID = 'prefinery_user_id', KEY_PREFINER_USED_CUSTOM_CODE = 'prefinery_used_custom_code', - KEY_LIST_SUB_ERROR = 'list_error'; + KEY_LIST_SUB_ERROR = 'list_error', + KEY_USER_CULTURE = 'user_culture'; public static function init() { diff --git a/lib/i18n.class.php b/lib/i18n.class.php index 25498bd8..d76688b8 100644 --- a/lib/i18n.class.php +++ b/lib/i18n.class.php @@ -17,6 +17,11 @@ class i18n public static function register($culture = null) /*needed to trigger class include, presumably setup would happen here*/ { + if ($culture == null) + { + $culture = Session::get(Session::KEY_USER_CULTURE); + } + if ($culture === null) { $urlTokens = Request::getHost() ? explode('.', Request::getHost()) : []; diff --git a/web/index.php b/web/index.php index 1dce4aa0..3122f26d 100644 --- a/web/index.php +++ b/web/index.php @@ -25,8 +25,8 @@ if (!IS_PRODUCTION) try { - i18n::register(); Session::init(); + i18n::register(); if (!IS_PRODUCTION) { View::compileCss(); From b30a7c07087a186d8aa9f0a7c674d3e07549f955 Mon Sep 17 00:00:00 2001 From: loblao <12ksit@gmail.com> Date: Sat, 3 Sep 2016 11:47:26 -0300 Subject: [PATCH 02/10] Nav: Add a (temporary) dropdown to allow user to change language --- controller/Controller.class.php | 2 ++ controller/action/i18nActions.class.php | 33 +++++++++++++++++++++++++ view/template/nav/_globalItems.php | 12 ++++++++- web/js/global.js | 11 ++++++++- 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 controller/action/i18nActions.class.php diff --git a/controller/Controller.class.php b/controller/Controller.class.php index f0bafa9b..7872cf22 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -94,6 +94,8 @@ class Controller $router->any('/list/subscribe', 'MailActions::executeSubscribe'); $router->get('/list/confirm/{hash}', 'MailActions::executeConfirm'); + $router->post('/language', 'i18nActions::setCulture'); + $permanentRedirects = [ '/lbry-osx-latest.dmg' => '/get', '/lbry-linux-latest.deb' => '/get', diff --git a/controller/action/i18nActions.class.php b/controller/action/i18nActions.class.php new file mode 100644 index 00000000..f7fc492a --- /dev/null +++ b/controller/action/i18nActions.class.php @@ -0,0 +1,33 @@ +
GitHub -
\ No newline at end of file + +
+ +
+ + diff --git a/web/js/global.js b/web/js/global.js index 32ffcdf0..2d91ef42 100644 --- a/web/js/global.js +++ b/web/js/global.js @@ -162,4 +162,13 @@ $(document).ready(function() { }); }); } -}); \ No newline at end of file + + var langDropdown = $('#language-dropdown'); + langDropdown.val(_currentLang); + langDropdown.on('change', function(x) { + $.ajax({type: 'POST', url: '/language', data: {'culture': this.value}, + success: function (d) { + window.location.reload(); + }}); + }); +}); From 05d465905f76e873122cd907368251de071cd268 Mon Sep 17 00:00:00 2001 From: loblao <12ksit@gmail.com> Date: Sat, 3 Sep 2016 11:50:29 -0300 Subject: [PATCH 03/10] i18n: Add some comments --- lib/i18n.class.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/i18n.class.php b/lib/i18n.class.php index d76688b8..ff7c5378 100644 --- a/lib/i18n.class.php +++ b/lib/i18n.class.php @@ -17,11 +17,13 @@ class i18n public static function register($culture = null) /*needed to trigger class include, presumably setup would happen here*/ { + // Get user preference, if any if ($culture == null) { $culture = Session::get(Session::KEY_USER_CULTURE); } + // Deduce from host if ($culture === null) { $urlTokens = Request::getHost() ? explode('.', Request::getHost()) : []; From 5539860a0a3ec986ae3ae6a2cff48777a9dd9af4 Mon Sep 17 00:00:00 2001 From: loblao <12ksit@gmail.com> Date: Mon, 5 Sep 2016 10:26:18 -0300 Subject: [PATCH 04/10] i18n: Send a POST request instead of an AJAX request --- controller/Controller.class.php | 2 +- controller/action/i18nActions.class.php | 2 +- view/template/nav/_globalItems.php | 10 ++++++---- web/js/global.js | 5 +---- 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/controller/Controller.class.php b/controller/Controller.class.php index 7872cf22..0db0148d 100644 --- a/controller/Controller.class.php +++ b/controller/Controller.class.php @@ -94,7 +94,7 @@ class Controller $router->any('/list/subscribe', 'MailActions::executeSubscribe'); $router->get('/list/confirm/{hash}', 'MailActions::executeConfirm'); - $router->post('/language', 'i18nActions::setCulture'); + $router->post('/set-culture', 'i18nActions::setCulture'); $permanentRedirects = [ '/lbry-osx-latest.dmg' => '/get', diff --git a/controller/action/i18nActions.class.php b/controller/action/i18nActions.class.php index f7fc492a..a78c5f55 100644 --- a/controller/action/i18nActions.class.php +++ b/controller/action/i18nActions.class.php @@ -28,6 +28,6 @@ class i18nActions extends Actions Session::unsetKey(Session::KEY_USER_CULTURE); } - return [null, null]; + return Controller::redirect('/'); } } diff --git a/view/template/nav/_globalItems.php b/view/template/nav/_globalItems.php index 0a0ef25d..0b340c33 100644 --- a/view/template/nav/_globalItems.php +++ b/view/template/nav/_globalItems.php @@ -23,10 +23,12 @@ GitHub
- +
+ +
diff --git a/web/js/global.js b/web/js/global.js index d3590994..76d3e422 100644 --- a/web/js/global.js +++ b/web/js/global.js @@ -163,9 +163,7 @@ $(document).ready(function() { }); } - var langDropdown = $('#language-dropdown'); - langDropdown.val(_currentLang); - langDropdown.on('change', function(x) { - $('#language-form').submit(); + $('#language-dropdown').on('change', function() { + $(this).closest('form').submit(); }); }); From e50e7290193a06b80a7bc6dd82bf8689e92583df Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 20 Sep 2016 09:55:15 -0400 Subject: [PATCH 08/10] minor --- controller/action/i18nActions.class.php | 51 +++++++++++-------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/controller/action/i18nActions.class.php b/controller/action/i18nActions.class.php index f56693b6..014fea47 100644 --- a/controller/action/i18nActions.class.php +++ b/controller/action/i18nActions.class.php @@ -1,36 +1,29 @@ Date: Tue, 20 Sep 2016 10:07:20 -0400 Subject: [PATCH 09/10] fixes --- lib/i18n.class.php | 21 +++++++++++---------- view/template/nav/_globalItems.php | 4 +++- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/i18n.class.php b/lib/i18n.class.php index b02aaa52..482d0d67 100644 --- a/lib/i18n.class.php +++ b/lib/i18n.class.php @@ -22,7 +22,7 @@ class i18n list($language, $country) = explode('_', $culture); static::$language = $language; - static::$country = $country; + static::$country = $country; setlocale(LC_MONETARY, $culture . '.UTF-8'); } @@ -39,7 +39,7 @@ class i18n public static function getAllCultures() { - return static::$cultures; + return static::$cultures; } public static function formatCurrency($amount, $currency = 'USD') @@ -49,7 +49,7 @@ class i18n public static function formatCredits($amount) { - return '' . (is_numeric($amount) ? number_format($amount, 1) : $amount) . ' LBC'; + return '' . (is_numeric($amount) ? number_format($amount, 1) : $amount) . ' LBC'; } public static function translate($token, $language = null) @@ -58,10 +58,11 @@ class i18n if (!isset(static::$translations[$language])) { $path = ROOT_DIR . '/data/i18n/' . $language . '.yaml'; + static::$translations[$language] = file_exists($path) ? Spyc::YAMLLoadString(file_get_contents($path)) : []; } $scope = static::$translations[$language]; - foreach(explode('.', $token) as $level) + foreach (explode('.', $token) as $level) { if (isset($scope[$level])) { @@ -85,7 +86,7 @@ class i18n //url trumps everything $urlTokens = Request::getHost() ? explode('.', Request::getHost()) : []; - $code = $urlTokens ? reset($urlTokens) : null; + $code = $urlTokens ? reset($urlTokens) : null; if ($code !== 'www') { $candidates[] = $code; @@ -96,15 +97,15 @@ class i18n // then headers // http://www.thefutureoftheweb.com/blog/use-accept-language-header - if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) + if (Request::getHttpHeader('Accept-Language')) { // break up string into pieces (languages and q factors) - preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse); + preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', Request::getHttpHeader('Accept-Language'), $languages); - if (count($lang_parse[1])) + if (isset($languages[1]) && count($languages[1])) { // create a list like "en" => 0.8 - $langs = array_combine($lang_parse[1], $lang_parse[4]); + $langs = array_combine($languages[1], $languages[4]); // set default to 1 for any without q factor foreach ($langs as $lang => $val) @@ -121,7 +122,7 @@ class i18n } } - foreach($candidates as $candidate) + foreach ($candidates as $candidate) { foreach (static::getAllCultures() as $culture) { diff --git a/view/template/nav/_globalItems.php b/view/template/nav/_globalItems.php index 9e27de83..2ef3791b 100644 --- a/view/template/nav/_globalItems.php +++ b/view/template/nav/_globalItems.php @@ -26,7 +26,9 @@
From 247b59ea9ad2a0e70d192d3e620896eda75ee4d8 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 20 Sep 2016 10:10:02 -0400 Subject: [PATCH 10/10] hide lang switcher for now --- view/template/nav/_globalItems.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/view/template/nav/_globalItems.php b/view/template/nav/_globalItems.php index 2ef3791b..c7abb883 100644 --- a/view/template/nav/_globalItems.php +++ b/view/template/nav/_globalItems.php @@ -22,6 +22,7 @@ +
+*/ ?> \ No newline at end of file