From 4cffaf26dab29b5722a2d5ed8e5000a4ef2460ef Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gr=C3=A9goire=20Aubert?= Date: Thu, 11 May 2017 15:00:46 +0200 Subject: [PATCH] SONAR-8596 Fix bad moment translation --- server/sonar-web/src/main/js/app/index.js | 2 -- .../src/main/js/app/utils/configureLocale.js | 29 ---------------- server/sonar-web/src/main/js/helpers/l10n.js | 33 ++++++++++--------- 3 files changed, 18 insertions(+), 46 deletions(-) delete mode 100644 server/sonar-web/src/main/js/app/utils/configureLocale.js diff --git a/server/sonar-web/src/main/js/app/index.js b/server/sonar-web/src/main/js/app/index.js index 0ece6b84f95..7f47d67b34d 100644 --- a/server/sonar-web/src/main/js/app/index.js +++ b/server/sonar-web/src/main/js/app/index.js @@ -17,7 +17,6 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import configureLocale from './utils/configureLocale'; import exposeLibraries from './utils/exposeLibraries'; import startAjaxMonitoring from './utils/startAjaxMonitoring'; import startReactApp from './utils/startReactApp'; @@ -30,7 +29,6 @@ require('script!../libs/inputs.js'); require('script!../libs/jquery-isolated-scroll.js'); require('script!../libs/application.js'); -configureLocale(); startAjaxMonitoring(); installGlobal(); startReactApp(); diff --git a/server/sonar-web/src/main/js/app/utils/configureLocale.js b/server/sonar-web/src/main/js/app/utils/configureLocale.js deleted file mode 100644 index fb001d49965..00000000000 --- a/server/sonar-web/src/main/js/app/utils/configureLocale.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2017 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -import moment from 'moment'; - -const getPreferredLanguage = () => - (window.navigator.languages ? window.navigator.languages[0] : window.navigator.language); - -const configureLocale = () => { - moment.locale(getPreferredLanguage()); -}; - -export default configureLocale; diff --git a/server/sonar-web/src/main/js/helpers/l10n.js b/server/sonar-web/src/main/js/helpers/l10n.js index 74a35b57f60..40f3da8a7ad 100644 --- a/server/sonar-web/src/main/js/helpers/l10n.js +++ b/server/sonar-web/src/main/js/helpers/l10n.js @@ -44,7 +44,11 @@ export function hasMessage(...keys: string[]) { return messages[messageKey] != null; } -function getCurrentLocale() { +export function configureMoment(language?: string) { + moment.locale(language || getPreferredLanguage()); +} + +function getPreferredLanguage() { return window.navigator.languages ? window.navigator.languages[0] : window.navigator.language; } @@ -89,33 +93,32 @@ function checkCachedBundle() { } export function requestMessages() { - const currentLocale = getCurrentLocale(); + const currentLocale = getPreferredLanguage(); const cachedLocale = localStorage.getItem('l10n.locale'); - - if (cachedLocale !== currentLocale) { - localStorage.removeItem('l10n.timestamp'); - } - - const bundleTimestamp = localStorage.getItem('l10n.timestamp'); const params = {}; + if (currentLocale) { params.locale = currentLocale; } - if (bundleTimestamp !== null && checkCachedBundle()) { - params.ts = bundleTimestamp; + + if (cachedLocale === currentLocale) { + const bundleTimestamp = localStorage.getItem('l10n.timestamp'); + if (bundleTimestamp !== null && checkCachedBundle()) { + params.ts = bundleTimestamp; + } } - return makeRequest(params).then(bundle => { + return makeRequest(params).then(response => { try { const currentTimestamp = moment().format('YYYY-MM-DDTHH:mm:ssZZ'); localStorage.setItem('l10n.timestamp', currentTimestamp); - localStorage.setItem('l10n.locale', currentLocale); - localStorage.setItem('l10n.bundle', JSON.stringify(bundle)); + localStorage.setItem('l10n.locale', response.effectiveLocale); + localStorage.setItem('l10n.bundle', JSON.stringify(response.messages)); } catch (e) { // do nothing } - - messages = bundle; + configureMoment(response.effectiveLocale); + resetBundle(response.messages); }); } -- 2.39.5