diff --git a/app/components/Authenticated.js b/app/components/Authenticated.js
index e585eb7a1c..505c47a38e 100644
--- a/app/components/Authenticated.js
+++ b/app/components/Authenticated.js
@@ -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 ;
-});
+};
-export default inject("auth")(Authenticated);
+export default observer(Authenticated);
diff --git a/app/components/Layout.js b/app/components/Layout.js
index fdba44b462..5849669c53 100644
--- a/app/components/Layout.js
+++ b/app/components/Layout.js
@@ -91,17 +91,13 @@ class Layout extends React.Component {
}
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 ;
if (this.redirectTo) return ;
- if (auth.authenticated && user && i18n.language !== user.language) {
- i18n.changeLanguage(user.language);
- }
-
return (
@@ -138,7 +134,7 @@ class Layout extends React.Component {
diff --git a/package.json b/package.json
index 0f2a61023b..ffc94c6a93 100644
--- a/package.json
+++ b/package.json
@@ -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"
-}
\ No newline at end of file
+}
diff --git a/server/routes.js b/server/routes.js
index 537b5712cb..a22f3a8af8 100644
--- a/server/routes.js
+++ b/server/routes.js
@@ -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);
});
diff --git a/shared/translations/default.json b/shared/translations/default.json
index 159c7b77ac..d8c629dea6 100644
--- a/shared/translations/default.json
+++ b/shared/translations/default.json
@@ -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",
diff --git a/shared/translations/i18n.js b/shared/translations/i18n.js
index 3151079a38..4ccd2b5df0 100644
--- a/shared/translations/i18n.js
+++ b/shared/translations/i18n.js
@@ -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 };
diff --git a/yarn.lock b/yarn.lock
index bf76881edc..729461fdbd 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -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"