mirror of
https://github.com/LemmyNet/joinlemmy-site.git
synced 2024-11-22 04:11:15 +00:00
review fixes
This commit is contained in:
parent
91706b4be6
commit
173d335319
4 changed files with 29 additions and 13 deletions
|
@ -1,12 +1,12 @@
|
||||||
import { hydrate } from "inferno-hydrate";
|
import { hydrate } from "inferno-hydrate";
|
||||||
import { BrowserRouter } from "inferno-router";
|
import { BrowserRouter } from "inferno-router";
|
||||||
import { App } from "../shared/components/app";
|
import { App } from "../shared/components/app";
|
||||||
import { i18n } from "../shared/i18next";
|
import { getLanguageFromCookie, i18n } from "../shared/i18next";
|
||||||
|
|
||||||
// Setting the language for js browsers
|
// Setting the language for js browsers
|
||||||
// If query param is set, server updates cookie automatically,
|
// If query param is set, server updates cookie automatically,
|
||||||
// so no need to check the query here
|
// so no need to check the query here
|
||||||
const languageCookie = document.cookie.split("=")[1];
|
const languageCookie = getLanguageFromCookie(document.cookie);
|
||||||
if (languageCookie != null) {
|
if (languageCookie != null) {
|
||||||
i18n.changeLanguage(languageCookie);
|
i18n.changeLanguage(languageCookie);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import { App } from "../shared/components/app";
|
||||||
// import { routes } from "../shared/routes";
|
// import { routes } from "../shared/routes";
|
||||||
import process from "process";
|
import process from "process";
|
||||||
import { Helmet } from "inferno-helmet";
|
import { Helmet } from "inferno-helmet";
|
||||||
import { i18n } from "../shared/i18next";
|
import { getLanguageFromCookie, i18n } from "../shared/i18next";
|
||||||
|
|
||||||
const server = express();
|
const server = express();
|
||||||
const port = 1234;
|
const port = 1234;
|
||||||
|
@ -23,21 +23,21 @@ server.get("/*", async (req, res) => {
|
||||||
const context = {} as any;
|
const context = {} as any;
|
||||||
|
|
||||||
// Setting the language for non-js browsers
|
// 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) {
|
if (req.query["lang"] != null) {
|
||||||
const lang = req.query["lang"].toString();
|
language = req.query["lang"].toString();
|
||||||
res.cookie("lang", lang, {
|
res.cookie("lang", language, {
|
||||||
expires: new Date(Date.now() + 60 * 60 * 24 * 7),
|
expires: new Date(Date.now() + 60 * 60 * 24 * 7),
|
||||||
});
|
});
|
||||||
i18n.changeLanguage(lang);
|
|
||||||
} else if (cookieLang != null) {
|
} else if (cookieLang != null) {
|
||||||
i18n.changeLanguage(cookieLang);
|
language = cookieLang;
|
||||||
} else {
|
} else {
|
||||||
const lang = req.headers["accept-language"]
|
language = req.headers["accept-language"]
|
||||||
? req.headers["accept-language"].split(",")[0]
|
? req.headers["accept-language"].split(",")[0]
|
||||||
: "en";
|
: "en";
|
||||||
i18n.changeLanguage(lang);
|
|
||||||
}
|
}
|
||||||
|
i18n.changeLanguage(language);
|
||||||
|
|
||||||
const wrapper = (
|
const wrapper = (
|
||||||
<StaticRouter location={req.url} context={context}>
|
<StaticRouter location={req.url} context={context}>
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Component, ChangeEvent } from "inferno";
|
import { Component, ChangeEvent, linkEvent } from "inferno";
|
||||||
import { Link } from "inferno-router";
|
import { Link } from "inferno-router";
|
||||||
import { LinkLine } from "./link-line";
|
import { LinkLine } from "./link-line";
|
||||||
import { Icon } from "./icon";
|
import { Icon } from "./icon";
|
||||||
|
@ -9,7 +9,7 @@ export class Navbar extends Component<any, any> {
|
||||||
super(props, context);
|
super(props, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleLanguageChange(event: ChangeEvent<HTMLSelectElement>) {
|
handleLanguageChange(_: any, event: ChangeEvent<HTMLSelectElement>) {
|
||||||
location.href = `/?lang=${event.target.value}`;
|
location.href = `/?lang=${event.target.value}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ export class Navbar extends Component<any, any> {
|
||||||
<div class="nav-right">
|
<div class="nav-right">
|
||||||
<div>
|
<div>
|
||||||
<select
|
<select
|
||||||
onChange={this.handleLanguageChange}
|
onChange={linkEvent(this, this.handleLanguageChange)}
|
||||||
class="text-light bd-dark language-selector"
|
class="text-light bd-dark language-selector"
|
||||||
>
|
>
|
||||||
{this.languageList().map((language, i) => (
|
{this.languageList().map((language, i) => (
|
||||||
|
|
|
@ -110,3 +110,19 @@ export { resources };
|
||||||
export function getLanguageName(key: string): string {
|
export function getLanguageName(key: string): string {
|
||||||
return languageNames[key];
|
return languageNames[key];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://gist.github.com/hunan-rostomyan/28e8702c1cecff41f7fe64345b76f2ca
|
||||||
|
export function getLanguageFromCookie(cookies?: string): string | null {
|
||||||
|
if (cookies == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const key = "lang=";
|
||||||
|
return (
|
||||||
|
cookies
|
||||||
|
.split(";")
|
||||||
|
.map(c => c.trim())
|
||||||
|
.filter(cookie => cookie.substring(0, key.length) === key)
|
||||||
|
.map(cookie => cookie.substring(key.length))[0] || null
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue