async translations loading

This commit is contained in:
Tom Moor committed 2020-11-23 09:33:47 -08:00
1 parent 247585eed2
commit cfefb51f0c
7 files changed
+78 -52

No files matched your search

+19 -7
View File
@@ -1,18 +1,30 @@
// @flow
import { observer, inject } from "mobx-react";
import { observer } from "mobx-react";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { Redirect } from "react-router-dom";
import { isCustomSubdomain } from "shared/utils/domains";
import AuthStore from "stores/AuthStore";
import LoadingIndicator from "components/LoadingIndicator";
import useStores from "../hooks/useStores";
import env from "env";
type Props = {
auth: AuthStore,
children?: React.Node,
children: React.Node,
};
const Authenticated = observer(({ auth, children }: Props) => {
const Authenticated = ({ children }: Props) => {
const { auth } = useStores();
const { i18n } = useTranslation();
const language = auth.user && auth.user.language;
// Watching for language changes here as this is the earliest point we have
// the user available and means we can start loading translations faster
React.useEffect(() => {
if (i18n.language !== language) {
i18n.changeLanguage(language);
}
}, [i18n, language]);
if (auth.authenticated) {
const { user, team } = auth;
const { hostname } = window.location;
@@ -43,6 +55,6 @@ const Authenticated = observer(({ auth, children }: Props) => {
auth.logout(true);
return <Redirect to="/" />;
});
};
export default inject("auth")(Authenticated);
export default observer(Authenticated);
+2 -6
View File
@@ -91,17 +91,13 @@ class Layout extends React.Component<Props> {
}
render() {
const { auth, ui, i18n } = this.props;
const { auth, t, ui } = this.props;
const { user, team } = auth;
const showSidebar = auth.authenticated && user && team;
if (auth.isSuspended) return <ErrorSuspended />;
if (this.redirectTo) return <Redirect to={this.redirectTo} push />;
if (auth.authenticated && user && i18n.language !== user.language) {
i18n.changeLanguage(user.language);
}
return (
<Container column auto>
<Helmet>
@@ -138,7 +134,7 @@ class Layout extends React.Component<Props> {
<Modal
isOpen={this.keyboardShortcutsOpen}
onRequestClose={this.handleCloseKeyboardShortcuts}
title="Keyboard shortcuts"
title={t("Keyboard shortcuts")}
>
<KeyboardShortcuts />
</Modal>
+2 -1
View File
@@ -94,6 +94,7 @@
"google-auth-library": "^5.5.1",
"http-errors": "1.4.0",
"i18next": "^19.8.3",
"i18next-http-backend": "^1.0.21",
"immutable": "^3.8.2",
"imports-loader": "0.6.5",
"invariant": "^2.2.2",
@@ -204,4 +205,4 @@
"js-yaml": "^3.13.1"
},
"version": "0.50.0"
}
}
+19
View File
@@ -6,7 +6,9 @@ import Koa from "koa";
import Router from "koa-router";
import sendfile from "koa-sendfile";
import serve from "koa-static";
import { languages } from "../shared/translations/i18n";
import environment from "./env";
import { NotFoundError } from "./errors";
import apexRedirect from "./middlewares/apexRedirect";
import { opensearchResponse } from "./utils/opensearch";
import { robotsResponse } from "./utils/robots";
@@ -72,6 +74,23 @@ if (process.env.NODE_ENV === "production") {
});
}
router.get("/locales/:lng.json", async (ctx) => {
let { lng } = ctx.params;
if (!languages.includes(lng)) {
throw new NotFoundError();
}
if (lng === "en_US") {
lng = "default";
}
await sendfile(
ctx,
path.join(__dirname, "../shared/translations/", `${lng}.json`)
);
});
router.get("/robots.txt", (ctx) => {
ctx.body = robotsResponse(ctx);
});
+1 -1
View File
@@ -14,6 +14,7 @@
"New doc": "New doc",
"More options": "More options",
"Search…": "Search…",
"Keyboard shortcuts": "Keyboard shortcuts",
"New collection…": "New collection…",
"Collections": "Collections",
"Untitled": "Untitled",
@@ -35,7 +36,6 @@
"Export Data": "Export Data",
"Integrations": "Integrations",
"Installation": "Installation",
"Keyboard shortcuts": "Keyboard shortcuts",
"Settings": "Settings",
"API documentation": "API documentation",
"Changelog": "Changelog",
+23 -32
View File
@@ -1,41 +1,32 @@
// @flow
import i18n from "i18next";
import backend from "i18next-http-backend";
import { initReactI18next } from "react-i18next";
import de_DE from "./de_DE.json";
import en_US from "./default.json";
import pt_PT from "./pt_PT.json";
const resources = {
en_US: {
translation: en_US,
},
de_DE: {
translation: de_DE,
},
pt_PT: {
translation: pt_PT,
},
};
const lng =
"DEFAULT_LANGUAGE" in process.env ? process.env.DEFAULT_LANGUAGE : "en_US";
const initI18n = () => {
i18n.use(initReactI18next).init({
interpolation: {
escapeValue: false,
},
react: {
useSuspense: false,
},
lng:
"DEFAULT_LANGUAGE" in process.env
? process.env.DEFAULT_LANGUAGE
: "en_US",
debug: process.env.NODE_ENV !== "production",
keySeparator: false,
resources,
});
i18n
.use(backend)
.use(initReactI18next)
.init({
backend: {
loadPath: "/locales/{{lng}}.json",
},
interpolation: {
escapeValue: false,
},
react: {
useSuspense: false,
},
lng,
fallbackLng: lng,
debug: process.env.NODE_ENV !== "production",
keySeparator: false,
});
};
const languages: string[] = Object.keys(resources);
const languages = ["en_US", "de_DE", "pt_PT"];
export { initI18n, languages, i18n, en_US, de_DE, pt_PT };
export { initI18n, languages, i18n };
+12 -5
View File
@@ -5926,6 +5926,13 @@ humanize-number@0.0.2:
resolved "https://registry.yarnpkg.com/humanize-number/-/humanize-number-0.0.2.tgz#11c0af6a471643633588588048f1799541489c18"
integrity sha1-EcCvakcWQ2M1iFiASPF5lUFInBg=
i18next-http-backend@^1.0.21:
version "1.0.21"
resolved "https://registry.yarnpkg.com/i18next-http-backend/-/i18next-http-backend-1.0.21.tgz#cee901b3527dad5165fa91de973b6aa6404c1c37"
integrity sha512-UDeHoV2B+31Gr++0KFAVjM5l+SEwePpF6sfDyaDq5ennM9QNJ78PBEMPStwkreEm4h5C8sT7M1JdNQrLcU1Wdg==
dependencies:
node-fetch "2.6.1"
i18next-parser@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/i18next-parser/-/i18next-parser-3.3.0.tgz#5cd907aff5e791c043137a4902ab42631cde9656"
@@ -8404,6 +8411,11 @@ nocache@2.1.0:
resolved "https://registry.yarnpkg.com/nocache/-/nocache-2.1.0.tgz#120c9ffec43b5729b1d5de88cd71aa75a0ba491f"
integrity sha512-0L9FvHG3nfnnmaEQPjT9xhfN4ISk0A8/2j4M37Np4mcDesJjHgEUfgPhdCyZuFI954tjokaIj/A3NdpFNdEh4Q==
node-fetch@2.6.1, node-fetch@^2.1.2, node-fetch@^2.3.0, node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
node-fetch@^1.0.1:
version "1.7.3"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-1.7.3.tgz#980f6f72d85211a5347c6b2bc18c5b84c3eb47ef"
@@ -8412,11 +8424,6 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"
node-fetch@^2.1.2, node-fetch@^2.3.0, node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
node-forge@^0.9.0:
version "0.9.2"
resolved "https://registry.yarnpkg.com/node-forge/-/node-forge-0.9.2.tgz#b35a44c28889b2ea55cabf8c79e3563f9676190a"