Fixes #2505 - Wrong language on login screen.

This commit is contained in:
Mantas Masalskis 2020-02-19 10:15:11 +01:00 committed by Thorsten Eckel
parent 95e5a46d4b
commit d7ca9d7484
5 changed files with 100 additions and 12 deletions

View file

@ -86,22 +86,33 @@ class App.i18n
_instance.mapTime _instance.mapTime
@detectBrowserLocale: -> @detectBrowserLocale: ->
return 'en-us' if !window.navigator.userLanguage && !window.navigator.language
if window.navigator.languages if window.navigator.languages
allLocales = App.Locale.all()
for browserLocale in window.navigator.languages for browserLocale in window.navigator.languages
for localAllLocale in allLocales if local = @findLocalLocale(browserLocale)
if browserLocale is localAllLocale.locale return local
return localAllLocale.locale
for browserLocale in window.navigator.languages if window.navigator.language
browserLocale = browserLocale.substr(0, 2) if local = @findLocalLocale(window.navigator.language)
for localAllLocale in allLocales return local
if browserLocale is localAllLocale.alias
return localAllLocale.locale
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: -> @detectBrowserTimezone: ->
return if !window.Intl return if !window.Intl

View file

@ -0,0 +1,14 @@
<link rel="stylesheet" href="/assets/tests/qunit-1.21.0.css">
<script src="/assets/tests/qunit-1.21.0.js"></script>
<script src="/assets/tests/i18n.js"></script>
<style type="text/css">
body {
padding-top: 0px;
}
</style>
<script type="text/javascript">
</script>
<div id="qunit" class="u-dontfold"></div>

View file

@ -4,6 +4,7 @@ Zammad::Application.routes.draw do
match '/tests_session', to: 'tests#session', via: :get match '/tests_session', to: 'tests#session', via: :get
match '/tests_local_storage', to: 'tests#local_storage', via: :get match '/tests_local_storage', to: 'tests#local_storage', via: :get
match '/tests_ui', to: 'tests#ui', 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', to: 'tests#model', via: :get
match '/tests_model_binding', to: 'tests#model_binding', via: :get match '/tests_model_binding', to: 'tests#model_binding', via: :get
match '/tests_model_ui', to: 'tests#model_ui', via: :get match '/tests_model_ui', to: 'tests#model_ui', via: :get

View file

@ -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()
})

View file

@ -24,6 +24,10 @@ RSpec.describe 'QUnit', type: :system, authenticated: false, set_up: true, webso
async_q_unit_tests('core') async_q_unit_tests('core')
end end
it 'I18n' do
async_q_unit_tests('i18n')
end
context 'UI' do context 'UI' do
it 'Base' do it 'Base' do