1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/i18n.ts
Sebastian Malton b1ff34879a cleanup Lens repo with tighter linting
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
2020-07-09 17:00:23 -04:00

63 lines
1.7 KiB
TypeScript

import { observable, reaction } from "mobx";
import { setupI18n } from "@lingui/core";
import { autobind, StorageHelper } from "./utils";
import orderBy from "lodash/orderBy";
import moment from "moment";
import "moment/locale/ru";
import "moment/locale/fi";
export interface Language {
code: string;
title: string;
nativeTitle: string;
}
export const _i18n = setupI18n({
missing: (message, id) => {
console.warn('Missing localization:', message, id);
return id;
}
});
@autobind()
export class LocalizationStore {
readonly defaultLocale = "en"
@observable activeLang = this.defaultLocale;
public languages: Language[] = orderBy<Language>([
{ code: "en", title: "English", nativeTitle: "English" },
{ code: "ru", title: "Russian", nativeTitle: "Русский" },
// { code: "fi", title: "Finnish", nativeTitle: "Suomi" },
], "title");
constructor() {
const storage = new StorageHelper("lang_ui", this.defaultLocale);
this.activeLang = storage.get();
reaction(() => this.activeLang, lang => storage.set(lang));
}
async init(): Promise<void> {
await this.setLocale(this.activeLang);
}
async load(locale: string): Promise<any> {
const catalog = await import(
/* webpackMode: "lazy", webpackChunkName: "locale/[request]" */
`../locales/${locale}/messages.js`
);
return _i18n.load(locale, catalog);
}
async setLocale(locale: string): Promise<void> {
await this.load(locale);
await _i18n.activate(locale);
// set moment's locale before activeLang for proper next render() in app
moment.locale(locale);
this.activeLang = locale;
}
}
export const i18nStore = new LocalizationStore();