From 3938b7883fe2b532d812ec0aa79043bd76afcaa5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 17 Jan 2018 13:24:54 -0700 Subject: [PATCH 1/2] introduced some locale fixes --- client/coral-framework/services/bootstrap.js | 6 +++- client/coral-framework/services/i18n.js | 35 ++++++++++++-------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/client/coral-framework/services/bootstrap.js b/client/coral-framework/services/bootstrap.js index b1014f2db..912c6c532 100644 --- a/client/coral-framework/services/bootstrap.js +++ b/client/coral-framework/services/bootstrap.js @@ -5,7 +5,7 @@ import EventEmitter from 'eventemitter2'; import { createReduxEmitter } from './events'; import { createRestClient } from './rest'; import thunk from 'redux-thunk'; -import { loadTranslations } from './i18n'; +import { setupTranslations, loadTranslations } from './i18n'; import bowser from 'bowser'; import noop from 'lodash/noop'; import { BASE_PATH } from 'coral-framework/constants/url'; @@ -88,6 +88,10 @@ export async function createContext({ const pymStorage = createPymStorage(pym); const history = createHistory(BASE_PATH); const introspection = createIntrospection(introspectionData); + + // Setup the translation framework with the storage. + setupTranslations(storage); + let store = null; const token = () => { // Try to get the token from localStorage. If it isn't here, it may diff --git a/client/coral-framework/services/i18n.js b/client/coral-framework/services/i18n.js index 6c4aa00a9..4c387a0ca 100644 --- a/client/coral-framework/services/i18n.js +++ b/client/coral-framework/services/i18n.js @@ -3,6 +3,8 @@ import has from 'lodash/has'; import get from 'lodash/get'; import merge from 'lodash/merge'; +import { createStorage } from 'coral-framework/services/storage'; + import moment from 'moment'; import 'moment/locale/da'; import 'moment/locale/es'; @@ -38,25 +40,34 @@ const translations = { let lang; let timeagoInstance; -function setLocale(locale) { +function setLocale(storage, locale) { try { - localStorage.setItem('locale', locale); + if (storage) { + storage.setItem('locale', locale); + } } catch (err) { console.error(err); } } -function getLocale() { - return ( - localStorage.getItem('locale') || - navigator.language || - defaultLanguage - ).split('-')[0]; +function getLocale(storage) { + try { + const locale = ( + (storage && storage.getItem('locale')) || + navigator.language || + defaultLanguage + ).split('-')[0]; + + return locale; + } catch (err) { + console.error(err); + return null; + } } -function init() { - const locale = getLocale(); - setLocale(locale); +export function setupTranslations(storage) { + const locale = getLocale(storage); + setLocale(storage, locale); // Setting moment moment.locale(locale); @@ -113,5 +124,3 @@ export function t(key, ...replacements) { } export default t; - -init(); From c612902779d8ac890726f443d5fbb74de5b53f0a Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 17 Jan 2018 13:41:02 -0700 Subject: [PATCH 2/2] fixed styles --- client/coral-framework/services/bootstrap.js | 6 +----- client/coral-framework/services/i18n.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/client/coral-framework/services/bootstrap.js b/client/coral-framework/services/bootstrap.js index 912c6c532..b1014f2db 100644 --- a/client/coral-framework/services/bootstrap.js +++ b/client/coral-framework/services/bootstrap.js @@ -5,7 +5,7 @@ import EventEmitter from 'eventemitter2'; import { createReduxEmitter } from './events'; import { createRestClient } from './rest'; import thunk from 'redux-thunk'; -import { setupTranslations, loadTranslations } from './i18n'; +import { loadTranslations } from './i18n'; import bowser from 'bowser'; import noop from 'lodash/noop'; import { BASE_PATH } from 'coral-framework/constants/url'; @@ -88,10 +88,6 @@ export async function createContext({ const pymStorage = createPymStorage(pym); const history = createHistory(BASE_PATH); const introspection = createIntrospection(introspectionData); - - // Setup the translation framework with the storage. - setupTranslations(storage); - let store = null; const token = () => { // Try to get the token from localStorage. If it isn't here, it may diff --git a/client/coral-framework/services/i18n.js b/client/coral-framework/services/i18n.js index 4c387a0ca..91f8560cc 100644 --- a/client/coral-framework/services/i18n.js +++ b/client/coral-framework/services/i18n.js @@ -3,14 +3,14 @@ import has from 'lodash/has'; import get from 'lodash/get'; import merge from 'lodash/merge'; -import { createStorage } from 'coral-framework/services/storage'; - import moment from 'moment'; import 'moment/locale/da'; import 'moment/locale/es'; import 'moment/locale/fr'; import 'moment/locale/pt-br'; +import { createStorage } from 'coral-framework/services/storage'; + import daTA from 'timeago.js/locales/da'; import esTA from 'timeago.js/locales/es'; import frTA from 'timeago.js/locales/fr'; @@ -52,20 +52,21 @@ function setLocale(storage, locale) { function getLocale(storage) { try { - const locale = ( + return ( (storage && storage.getItem('locale')) || navigator.language || defaultLanguage ).split('-')[0]; - - return locale; } catch (err) { console.error(err); return null; } } -export function setupTranslations(storage) { +export function setupTranslations() { + // Setup the translation framework with the storage. + const storage = createStorage(); + const locale = getLocale(storage); setLocale(storage, locale); @@ -124,3 +125,6 @@ export function t(key, ...replacements) { } export default t; + +// Setup the translations globally as soon as this module runs. +setupTranslations();