forked from abner/for-legacy-web
Use tabWidth 4 without actual tabs.
This commit is contained in:
@@ -3,50 +3,50 @@ import { Language } from "../../context/Locale";
|
||||
import type { SyncUpdateAction } from "./sync";
|
||||
|
||||
export type LocaleAction =
|
||||
| { type: undefined }
|
||||
| {
|
||||
type: "SET_LOCALE";
|
||||
locale: Language;
|
||||
}
|
||||
| SyncUpdateAction;
|
||||
| { type: undefined }
|
||||
| {
|
||||
type: "SET_LOCALE";
|
||||
locale: Language;
|
||||
}
|
||||
| SyncUpdateAction;
|
||||
|
||||
export function findLanguage(lang?: string): Language {
|
||||
if (!lang) {
|
||||
if (typeof navigator === "undefined") {
|
||||
lang = Language.ENGLISH;
|
||||
} else {
|
||||
lang = navigator.language;
|
||||
}
|
||||
}
|
||||
if (!lang) {
|
||||
if (typeof navigator === "undefined") {
|
||||
lang = Language.ENGLISH;
|
||||
} else {
|
||||
lang = navigator.language;
|
||||
}
|
||||
}
|
||||
|
||||
const code = lang.replace("-", "_");
|
||||
const short = code.split("_")[0];
|
||||
const code = lang.replace("-", "_");
|
||||
const short = code.split("_")[0];
|
||||
|
||||
const values = [];
|
||||
for (const key in Language) {
|
||||
const value = Language[key as keyof typeof Language];
|
||||
values.push(value);
|
||||
if (value.startsWith(code)) {
|
||||
return value as Language;
|
||||
}
|
||||
}
|
||||
const values = [];
|
||||
for (const key in Language) {
|
||||
const value = Language[key as keyof typeof Language];
|
||||
values.push(value);
|
||||
if (value.startsWith(code)) {
|
||||
return value as Language;
|
||||
}
|
||||
}
|
||||
|
||||
for (const value of values.reverse()) {
|
||||
if (value.startsWith(short)) {
|
||||
return value as Language;
|
||||
}
|
||||
}
|
||||
for (const value of values.reverse()) {
|
||||
if (value.startsWith(short)) {
|
||||
return value as Language;
|
||||
}
|
||||
}
|
||||
|
||||
return Language.ENGLISH;
|
||||
return Language.ENGLISH;
|
||||
}
|
||||
|
||||
export function locale(state = findLanguage(), action: LocaleAction): Language {
|
||||
switch (action.type) {
|
||||
case "SET_LOCALE":
|
||||
return action.locale;
|
||||
case "SYNC_UPDATE":
|
||||
return (action.update.locale?.[1] ?? state) as Language;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
switch (action.type) {
|
||||
case "SET_LOCALE":
|
||||
return action.locale;
|
||||
case "SYNC_UPDATE":
|
||||
return (action.update.locale?.[1] ?? state) as Language;
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user