From d7ca9d7484fd6bfd04d81a66e71917a5b1035b8e Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Wed, 19 Feb 2020 10:15:11 +0100 Subject: [PATCH] Fixes #2505 - Wrong language on login screen. --- .../javascripts/app/lib/app_post/i18n.coffee | 35 +++++++---- app/views/tests/i18n.html.erb | 14 +++++ config/routes/test.rb | 1 + public/assets/tests/i18n.js | 58 +++++++++++++++++++ spec/system/js/q_unit_spec.rb | 4 ++ 5 files changed, 100 insertions(+), 12 deletions(-) create mode 100644 app/views/tests/i18n.html.erb create mode 100644 public/assets/tests/i18n.js diff --git a/app/assets/javascripts/app/lib/app_post/i18n.coffee b/app/assets/javascripts/app/lib/app_post/i18n.coffee index c8bc27895..3d5e50476 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.coffee +++ b/app/assets/javascripts/app/lib/app_post/i18n.coffee @@ -86,22 +86,33 @@ class App.i18n _instance.mapTime @detectBrowserLocale: -> - return 'en-us' if !window.navigator.userLanguage && !window.navigator.language - if window.navigator.languages - allLocales = App.Locale.all() for browserLocale in window.navigator.languages - for localAllLocale in allLocales - if browserLocale is localAllLocale.locale - return localAllLocale.locale + if local = @findLocalLocale(browserLocale) + return local - for browserLocale in window.navigator.languages - browserLocale = browserLocale.substr(0, 2) - for localAllLocale in allLocales - if browserLocale is localAllLocale.alias - return localAllLocale.locale + if window.navigator.language + if local = @findLocalLocale(window.navigator.language) + return local - window.navigator.userLanguage || window.navigator.language || 'en-us' + if window.navigator.userLanguage + if local = @findLocalLocale(window.navigator.userLanguage) + return local + + return 'en-us' + + @findLocalLocale: (given) -> + givenLower = given.toLowerCase() + + for local in App.Locale.all() + if givenLower == local.locale.toLowerCase() + return local.locale.toLowerCase() + + givenAlias = given.substr(0, 2).toLowerCase() + + for local in App.Locale.all() + if givenAlias == local.alias.toLowerCase() + return local.locale.toLowerCase() @detectBrowserTimezone: -> return if !window.Intl diff --git a/app/views/tests/i18n.html.erb b/app/views/tests/i18n.html.erb new file mode 100644 index 000000000..6ff3e0590 --- /dev/null +++ b/app/views/tests/i18n.html.erb @@ -0,0 +1,14 @@ + + + + + + + + +
diff --git a/config/routes/test.rb b/config/routes/test.rb index 0b41a07a1..dce9d16de 100644 --- a/config/routes/test.rb +++ b/config/routes/test.rb @@ -4,6 +4,7 @@ Zammad::Application.routes.draw do match '/tests_session', to: 'tests#session', via: :get match '/tests_local_storage', to: 'tests#local_storage', via: :get match '/tests_ui', to: 'tests#ui', via: :get + match '/tests_i18n', to: 'tests#i18n', via: :get match '/tests_model', to: 'tests#model', via: :get match '/tests_model_binding', to: 'tests#model_binding', via: :get match '/tests_model_ui', to: 'tests#model_ui', via: :get diff --git a/public/assets/tests/i18n.js b/public/assets/tests/i18n.js new file mode 100644 index 000000000..a4fdb2419 --- /dev/null +++ b/public/assets/tests/i18n.js @@ -0,0 +1,58 @@ +test('i18n .detectBrowserLocale', function() { + var original_userLanguage = window.navigator.userLanguage + var original_language = window.navigator.language + var original_languages = window.navigator.languages + + var reset = function(){ + window.navigator.userLanguage = original_userLanguage + window.navigator.language = original_language + window.navigator.languages = original_languages + } + + var mock = function(languages, language, userLanguage){ + Object.defineProperty(window.navigator, 'language', {value: language, configurable: true }); + Object.defineProperty(window.navigator, 'languages', {value: languages, configurable: true }); + Object.defineProperty(window.navigator, 'userLanguage', {value: userLanguage, configurable: true }); + } + + mock(undefined, undefined, undefined) + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(undefined, undefined, 'non-existant'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(undefined, undefined, 'en'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(undefined, undefined, 'lt'); + equal(App.i18n.detectBrowserLocale(), 'lt') + + mock(undefined, 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'lt') + + mock(undefined, 'en', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(['en'], 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(['en-us'], 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(['en-US'], 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(['lt', 'en-US'], 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'lt') + + mock(['en-GB', 'en-US'], 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'en-gb') + + mock(['en-XYZ'], 'lt', 'lv'); + equal(App.i18n.detectBrowserLocale(), 'en-us') + + mock(['xyz', 'lt'], 'lv', undefined); + equal(App.i18n.detectBrowserLocale(), 'lt') + + reset() +}) diff --git a/spec/system/js/q_unit_spec.rb b/spec/system/js/q_unit_spec.rb index 7048b0308..c30117f86 100644 --- a/spec/system/js/q_unit_spec.rb +++ b/spec/system/js/q_unit_spec.rb @@ -24,6 +24,10 @@ RSpec.describe 'QUnit', type: :system, authenticated: false, set_up: true, webso async_q_unit_tests('core') end + it 'I18n' do + async_q_unit_tests('i18n') + end + context 'UI' do it 'Base' do