Dynamicaly import locales (#1793)

* dynamicly import locales

* fix tests

* reformat languages
This commit is contained in:
Kostas Bariotis 2020-06-27 12:02:54 +01:00 committed by GitHub
parent 5970bb7ee9
commit 0a3fb70ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 53 deletions

View File

@ -86,7 +86,7 @@
"test": "npm run test:app", "test": "npm run test:app",
"test:all": "npm run test:typecheck && npm run test:code && npm run test:other && npm run test:app -- --watchAll=false", "test:all": "npm run test:typecheck && npm run test:code && npm run test:other && npm run test:app -- --watchAll=false",
"test:update": "npm run test:app -- --updateSnapshot --watchAll=false", "test:update": "npm run test:app -- --updateSnapshot --watchAll=false",
"test:app": "react-scripts test --env=jsdom --passWithNoTests", "test:app": "react-scripts test --env=jsdom-fourteen --passWithNoTests",
"test:code": "eslint --max-warnings=0 --ignore-path .gitignore --ext .js,.ts,.tsx .", "test:code": "eslint --max-warnings=0 --ignore-path .gitignore --ext .js,.ts,.tsx .",
"test:debug": "react-scripts --inspect-brk test --runInBand --no-cache", "test:debug": "react-scripts --inspect-brk test --runInBand --no-cache",
"test:other": "npm run prettier -- --list-different", "test:other": "npm run prettier -- --list-different",

View File

@ -0,0 +1,24 @@
import React from "react";
import { LoadingMessage } from "./LoadingMessage";
import { setLanguageFirstTime } from "../i18n";
export class InitializeApp extends React.Component<
any,
{ isLoading: boolean }
> {
public state: { isLoading: boolean } = {
isLoading: true,
};
async componentDidMount() {
await setLanguageFirstTime();
this.setState({
isLoading: false,
});
}
public render() {
return this.state.isLoading ? <LoadingMessage /> : this.props.children;
}
}

View File

@ -255,8 +255,8 @@ const LayerUI = ({
}`} }`}
> >
<LanguageList <LanguageList
onChange={(lng) => { onChange={async (lng) => {
setLanguage(lng); await setLanguage(lng);
setAppState({}); setAppState({});
}} }}
languages={languages} languages={languages}

View File

@ -100,8 +100,8 @@ export const MobileMenu = ({
<fieldset> <fieldset>
<legend>{t("labels.language")}</legend> <legend>{t("labels.language")}</legend>
<LanguageList <LanguageList
onChange={(lng) => { onChange={async (lng) => {
setLanguage(lng); await setLanguage(lng);
setAppState({}); setAppState({});
}} }}
/> />

View File

@ -1,55 +1,58 @@
import LanguageDetector from "i18next-browser-languagedetector"; import LanguageDetector from "i18next-browser-languagedetector";
export const languages = [ export const languages = [
{ lng: "en", label: "English", data: require("./locales/en.json") }, { lng: "en", label: "English", data: "en.json" },
{ lng: "bg-BG", label: "Български", data: require("./locales/bg-BG.json") }, { lng: "bg-BG", label: "Български", data: "bg-BG.json" },
{ lng: "nb-No", label: "Bokmål", data: require("./locales/nb-NO.json") }, { lng: "de-DE", label: "Deutsch", data: "de-DE.json" },
{ lng: "de-DE", label: "Deutsch", data: require("./locales/de-DE.json") }, { lng: "nb-No", label: "Bokmål", data: "nb-NO.json" },
{ lng: "es-ES", label: "Español", data: require("./locales/es-ES.json") }, { lng: "es-ES", label: "Español", data: "es-ES.json" },
{ lng: "ca-ES", label: "Catalan", data: require("./locales/ca-ES.json") }, { lng: "ca-ES", label: "Catalan", data: "ca-ES.json" },
{ lng: "el-GR", label: "Ελληνικά", data: require("./locales/el-GR.json") }, { lng: "el-GR", label: "Ελληνικά", data: "el-GR.json" },
{ lng: "fr-FR", label: "Français", data: require("./locales/fr-FR.json") }, { lng: "fr-FR", label: "Français", data: "fr-FR.json" },
{ { lng: "id-ID", label: "Bahasa Indonesia", data: "id-ID.json" },
lng: "id-ID", { lng: "it-IT", label: "Italiano", data: "it-IT.json" },
label: "Bahasa Indonesia", { lng: "hu-HU", label: "Magyar", data: "hu-HU.json" },
data: require("./locales/id-ID.json"), { lng: "nl-NL", label: "Nederlands", data: "nl-NL.json" },
}, { lng: "pl-PL", label: "Polski", data: "pl-PL.json" },
{ lng: "it-IT", label: "Italiano", data: require("./locales/it-IT.json") }, { lng: "pt-PT", label: "Português", data: "pt-PT.json" },
{ lng: "hu-HU", label: "Magyar", data: require("./locales/hu-HU.json") }, { lng: "ru-RU", label: "Русский", data: "ru-RU.json" },
{ lng: "nl-NL", label: "Nederlands", data: require("./locales/nl-NL.json") }, { lng: "uk-UA", label: "Українська", data: "uk-UA.json" },
{ lng: "pl-PL", label: "Polski", data: require("./locales/pl-PL.json") }, { lng: "fi-FI", label: "Suomi", data: "fi-FI.json" },
{ lng: "pt-PT", label: "Português", data: require("./locales/pt-PT.json") }, { lng: "tr-TR", label: "Türkçe", data: "tr-TR.json" },
{ lng: "ru-RU", label: "Русский", data: require("./locales/ru-RU.json") }, { lng: "ja-JP", label: "日本語", data: "ja-JP.json" },
{ lng: "uk-UA", label: "Українська", data: require("./locales/uk-UA.json") }, { lng: "ko-KR", label: "한국어", data: "ko-KR.json" },
{ lng: "fi-FI", label: "Suomi", data: require("./locales/fi-FI.json") }, { lng: "zh-TW", label: "繁體中文", data: "zh-TW.json" },
{ lng: "tr-TR", label: "Türkçe", data: require("./locales/tr-TR.json") }, { lng: "zh-CN", label: "简体中文", data: "zh-CN.json" },
{ lng: "ja-JP", label: "日本語", data: require("./locales/ja-JP.json") }, { lng: "ar-SA", label: "العربية", data: "ar-SA.json", rtl: true },
{ lng: "ko-KR", label: "한국어", data: require("./locales/ko-KR.json") }, { lng: "he-IL", label: "עברית", data: "he-IL.json", rtl: true },
{ lng: "zh-TW", label: "繁體中文", data: require("./locales/zh-TW.json") },
{ lng: "zh-CN", label: "简体中文", data: require("./locales/zh-CN.json") },
{
lng: "ar-SA",
label: "العربية",
data: require("./locales/ar-SA.json"),
rtl: true,
},
{
lng: "he-IL",
label: "עברית",
data: require("./locales/he-IL.json"),
rtl: true,
},
]; ];
let currentLanguage = languages[0]; let currentLanguage = languages[0];
let currentLanguageData = {};
const fallbackLanguage = languages[0]; const fallbackLanguage = languages[0];
const fallbackLanguageData = require("./locales/en.json");
export const setLanguage = (newLng: string | undefined) => { export const setLanguage = async (newLng: string | undefined) => {
currentLanguage = currentLanguage =
languages.find((language) => language.lng === newLng) || fallbackLanguage; languages.find((language) => language.lng === newLng) || fallbackLanguage;
document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr"; document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr";
currentLanguageData = await import(`./locales/${currentLanguage.data}`);
languageDetector.cacheUserLanguage(currentLanguage.lng);
};
export const setLanguageFirstTime = async () => {
const newLng: string | undefined = languageDetector.detect();
currentLanguage =
languages.find((language) => language.lng === newLng) || fallbackLanguage;
document.documentElement.dir = currentLanguage.rtl ? "rtl" : "ltr";
currentLanguageData = await import(`./locales/${currentLanguage.data}`);
languageDetector.cacheUserLanguage(currentLanguage.lng); languageDetector.cacheUserLanguage(currentLanguage.lng);
}; };
@ -72,8 +75,8 @@ const findPartsForData = (data: any, parts: string[]) => {
export const t = (path: string, replacement?: { [key: string]: string }) => { export const t = (path: string, replacement?: { [key: string]: string }) => {
const parts = path.split("."); const parts = path.split(".");
let translation = let translation =
findPartsForData(currentLanguage.data, parts) || findPartsForData(currentLanguageData, parts) ||
findPartsForData(fallbackLanguage.data, parts); findPartsForData(fallbackLanguageData, parts);
if (translation === undefined) { if (translation === undefined) {
throw new Error(`Can't find translation for ${path}`); throw new Error(`Can't find translation for ${path}`);
} }
@ -94,5 +97,3 @@ languageDetector.init({
}, },
checkWhitelist: false, checkWhitelist: false,
}); });
setLanguage(languageDetector.detect());

View File

@ -5,6 +5,7 @@ import * as SentryIntegrations from "@sentry/integrations";
import { EVENT } from "./constants"; import { EVENT } from "./constants";
import { TopErrorBoundary } from "./components/TopErrorBoundary"; import { TopErrorBoundary } from "./components/TopErrorBoundary";
import { InitializeApp } from "./components/InitializeApp";
import { IsMobileProvider } from "./is-mobile"; import { IsMobileProvider } from "./is-mobile";
import App from "./components/App"; import App from "./components/App";
import { register as registerServiceWorker } from "./serviceWorker"; import { register as registerServiceWorker } from "./serviceWorker";
@ -67,7 +68,9 @@ const rootElement = document.getElementById("root");
ReactDOM.render( ReactDOM.render(
<TopErrorBoundary> <TopErrorBoundary>
<IsMobileProvider> <IsMobileProvider>
<App /> <InitializeApp>
<App />
</InitializeApp>
</IsMobileProvider> </IsMobileProvider>
</TopErrorBoundary>, </TopErrorBoundary>,
rootElement, rootElement,

View File

@ -2,8 +2,9 @@ import { reseed } from "../random";
import React from "react"; import React from "react";
import ReactDOM from "react-dom"; import ReactDOM from "react-dom";
import * as Renderer from "../renderer/renderScene"; import * as Renderer from "../renderer/renderScene";
import { render, screen, fireEvent } from "./test-utils"; import { waitFor, render, screen, fireEvent } from "./test-utils";
import App from "../components/App"; import App from "../components/App";
import { setLanguage } from "../i18n";
import { ToolName } from "./queries/toolQueries"; import { ToolName } from "./queries/toolQueries";
import { KEYS, Key } from "../keys"; import { KEYS, Key } from "../keys";
import { setDateTimeForTests } from "../utils"; import { setDateTimeForTests } from "../utils";
@ -227,7 +228,7 @@ const checkpoint = (name: string) => {
); );
}; };
beforeEach(() => { beforeEach(async () => {
// Unmount ReactDOM from root // Unmount ReactDOM from root
ReactDOM.unmountComponentAtNode(document.getElementById("root")!); ReactDOM.unmountComponentAtNode(document.getElementById("root")!);
@ -242,6 +243,7 @@ beforeEach(() => {
finger2.reset(); finger2.reset();
altKey = ctrlKey = shiftKey = false; altKey = ctrlKey = shiftKey = false;
await setLanguage("en.json");
const renderResult = render(<App />); const renderResult = render(<App />);
getByToolName = renderResult.getByToolName; getByToolName = renderResult.getByToolName;
@ -655,7 +657,7 @@ describe("regression tests", () => {
expect(h.state.zoom).toBe(1); expect(h.state.zoom).toBe(1);
}); });
it("rerenders UI on language change", () => { it("rerenders UI on language change", async () => {
// select rectangle tool to show properties menu // select rectangle tool to show properties menu
clickTool("rectangle"); clickTool("rectangle");
// english lang should display `hachure` label // english lang should display `hachure` label
@ -664,11 +666,13 @@ describe("regression tests", () => {
target: { value: "de-DE" }, target: { value: "de-DE" },
}); });
// switching to german, `hachure` label should no longer exist // switching to german, `hachure` label should no longer exist
expect(screen.queryByText(/hachure/i)).toBeNull(); await waitFor(() => expect(screen.queryByText(/hachure/i)).toBeNull());
// reset language // reset language
fireEvent.change(document.querySelector(".dropdown-select__language")!, { fireEvent.change(document.querySelector(".dropdown-select__language")!, {
target: { value: "en" }, target: { value: "en" },
}); });
// switching back to English
await waitFor(() => expect(screen.queryByText(/hachure/i)).not.toBeNull());
}); });
it("make a group and duplicate it", () => { it("make a group and duplicate it", () => {