From 173d335319a0faec15387bb539db8be4ef5d23a6 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Tue, 15 Feb 2022 13:23:41 +0100 Subject: [PATCH] review fixes --- src/client/index.tsx | 4 ++-- src/server/index.tsx | 16 ++++++++-------- src/shared/components/navbar.tsx | 6 +++--- src/shared/i18next.ts | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/client/index.tsx b/src/client/index.tsx index 96e3b95..71a3ec4 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -1,12 +1,12 @@ import { hydrate } from "inferno-hydrate"; import { BrowserRouter } from "inferno-router"; import { App } from "../shared/components/app"; -import { i18n } from "../shared/i18next"; +import { getLanguageFromCookie, i18n } from "../shared/i18next"; // Setting the language for js browsers // If query param is set, server updates cookie automatically, // so no need to check the query here -const languageCookie = document.cookie.split("=")[1]; +const languageCookie = getLanguageFromCookie(document.cookie); if (languageCookie != null) { i18n.changeLanguage(languageCookie); } else { diff --git a/src/server/index.tsx b/src/server/index.tsx index 50a20d3..39c35bb 100644 --- a/src/server/index.tsx +++ b/src/server/index.tsx @@ -7,7 +7,7 @@ import { App } from "../shared/components/app"; // import { routes } from "../shared/routes"; import process from "process"; import { Helmet } from "inferno-helmet"; -import { i18n } from "../shared/i18next"; +import { getLanguageFromCookie, i18n } from "../shared/i18next"; const server = express(); const port = 1234; @@ -23,21 +23,21 @@ server.get("/*", async (req, res) => { const context = {} as any; // Setting the language for non-js browsers - const cookieLang = req.headers.cookie?.split("=")[1]; + const cookieLang = getLanguageFromCookie(req.headers.cookie); + var language: string; if (req.query["lang"] != null) { - const lang = req.query["lang"].toString(); - res.cookie("lang", lang, { + language = req.query["lang"].toString(); + res.cookie("lang", language, { expires: new Date(Date.now() + 60 * 60 * 24 * 7), }); - i18n.changeLanguage(lang); } else if (cookieLang != null) { - i18n.changeLanguage(cookieLang); + language = cookieLang; } else { - const lang = req.headers["accept-language"] + language = req.headers["accept-language"] ? req.headers["accept-language"].split(",")[0] : "en"; - i18n.changeLanguage(lang); } + i18n.changeLanguage(language); const wrapper = ( diff --git a/src/shared/components/navbar.tsx b/src/shared/components/navbar.tsx index 81cddd1..9de3af0 100644 --- a/src/shared/components/navbar.tsx +++ b/src/shared/components/navbar.tsx @@ -1,4 +1,4 @@ -import { Component, ChangeEvent } from "inferno"; +import { Component, ChangeEvent, linkEvent } from "inferno"; import { Link } from "inferno-router"; import { LinkLine } from "./link-line"; import { Icon } from "./icon"; @@ -9,7 +9,7 @@ export class Navbar extends Component { super(props, context); } - handleLanguageChange(event: ChangeEvent) { + handleLanguageChange(_: any, event: ChangeEvent) { location.href = `/?lang=${event.target.value}`; } @@ -34,7 +34,7 @@ export class Navbar extends Component {