Skip to content
Permalink
Browse files

UI updates: Initial language now taken from OS locale and language se…

…tter added to manager object which also stores preference in local storage
  • Loading branch information...
richturner committed Nov 7, 2019
1 parent 76914b3 commit bfe86e395c1f3d7bedb036f44ea099938bc13b0d
Showing with 48 additions and 31 deletions.
  1. +25 −26 ui/component/core/src/console.ts
  2. +22 −4 ui/component/core/src/index.ts
  3. +1 −1 ui/demo/demo-core/src/index.ts
@@ -331,18 +331,16 @@ export class Console {
}


public async retrieveData(key: string): Promise<string | null> {
public async retrieveData(key: string): Promise<string | undefined> {
let response = await this.sendProviderMessage({
provider: "storage",
action: "RETRIEVE",
key: key
}, true);

if (response && response.value) {
return JSON.parse(response.value);
return response.value;
}

return null;
}

protected _postNativeShellMessage(jsonMessage: any) {
@@ -421,35 +419,36 @@ export class Console {
};
this._handleProviderResponse(JSON.stringify(enableResponse));
break;
case "STORE":
let keyValue = msg.key ? msg.key.trim() : null;
case "STORE": {
let keyValue = msg.key ? msg.key.trim() : null;

if (!keyValue || keyValue.length === 0) {
throw new Error("Storage provider 'store' action requires a `key`");
}
if (!keyValue || keyValue.length === 0) {
throw new Error("Storage provider 'store' action requires a `key`");
}

if (msg.value) {
if (msg.value === null) {
window.localStorage.removeItem(keyValue);
} else {
window.localStorage.setItem(keyValue, msg.value);
if (msg.value) {
if (msg.value === null) {
window.localStorage.removeItem(keyValue);
} else {
window.localStorage.setItem(keyValue, msg.value);
}
}
}

break;
case "RETRIEVE":
keyValue = msg.key ? msg.key.trim() : null;
case "RETRIEVE": {
let keyValue = msg.key ? msg.key.trim() : null;

if (!keyValue || keyValue.length === 0) {
throw new Error("Storage provider 'retrieve' action requires a `key`");
}
if (!keyValue || keyValue.length === 0) {
throw new Error("Storage provider 'retrieve' action requires a `key`");
}

this._handleProviderResponse(JSON.stringify({
action: "RETRIEVE",
provider: "storage",
key: keyValue,
value: window.localStorage.getItem(keyValue)
}));
this._handleProviderResponse(JSON.stringify({
action: "RETRIEVE",
provider: "storage",
key: keyValue,
value: window.localStorage.getItem(keyValue)
}));
}
break;
default:
throw new Error("Unsupported provider '" + msg.provider + "' and action '" + msg.action + "'");
@@ -6,6 +6,7 @@ import {AxiosRequestConfig} from "axios";
import {EventProvider, EventProviderFactory, EventProviderStatus, WebSocketEventProvider} from "./event";
import i18next from "i18next";
import i18nextXhr from "i18next-xhr-backend";
import moment from "moment";
import {
AssetDescriptor,
Attribute,
@@ -325,6 +326,11 @@ export class Manager implements EventProviderFactory {
return i18next.language;
}

set language(lang: string) {
i18next.changeLanguage(lang);
this.console.storeData("LANGUAGE", lang);
}

getEventProvider(): EventProvider | undefined {
return this.events;
}
@@ -510,16 +516,29 @@ export class Manager implements EventProviderFactory {
this._emitEvent(OREvent.TRANSLATE_INIT);
});

i18next.on("languageChanged", () => {
i18next.on("languageChanged", (lng) => {
moment.locale(lng);
this._emitEvent(OREvent.TRANSLATE_LANGUAGE_CHANGED);
});

// Look for language preference in local storage
const language = await this.console.retrieveData("LANGUAGE");

const initOptions: i18next.InitOptions = {
lng: "en",
lng: language,
fallbackLng: "en",
defaultNS: "app",
fallbackNS: "or",
ns: this.config.loadTranslations,
interpolation: {
format: function(value, format, lng) {
if (format === 'uppercase') return value.toUpperCase();
if(value instanceof Date) {
return moment(value).format(format);
}
return value;
}
},
backend: {
loadPath: (langs: string[], namespaces: string[]) => {
if (namespaces.length === 1 && namespaces[0] === "or") {
@@ -885,11 +904,10 @@ export class Manager implements EventProviderFactory {
});
}

protected async _getNativeOfflineRefreshToken(): Promise<string | null> {
protected async _getNativeOfflineRefreshToken(): Promise<string | undefined> {
if (this.console && this.console.isMobile) {
return await this.console.retrieveData("REFRESH_TOKEN");
}
return null;
}

protected _emitEvent(event: OREvent) {
@@ -77,7 +77,7 @@ class OrDemo extends subscribe(manager)(LitElement) {
}

public toggleLanguage() {
i18next.changeLanguage(i18next.language === "en" ? "nl" : "en");
manager.language = i18next.language === "en" ? "nl" : "en";
this.requestUpdate();
}

0 comments on commit bfe86e3

Please sign in to comment.
You can’t perform that action at this time.