From 3938b7883fe2b532d812ec0aa79043bd76afcaa5 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 17 Jan 2018 13:24:54 -0700 Subject: [PATCH] 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();