This commit is contained in:
2022-11-29 14:17:11 +01:00
parent c9a7da688f
commit 25ef07b0b7
32 changed files with 1275 additions and 507 deletions

View File

@@ -0,0 +1,14 @@
import React from "react";
import { enUS as dateFnsEN } from "date-fns/locale/index.js";
export default class LocaleEnglish {
constructor() {
this.code = "en_US";
this.name = "American English";
this.entries = {};
}
toDateFns() {
return dateFnsEN;
}
};

View File

@@ -0,0 +1,14 @@
import React from "react";
import { de as dateFnsDE } from "date-fns/locale/index.js";
export default class LocaleGerman {
constructor() {
this.code = "de_DE";
this.name = "Deutsch Standard";
this.entries = {};
}
toDateFns() {
return dateFnsDE;
}
}

View File

@@ -0,0 +1,35 @@
import LocaleGerman from "./german";
import LocaleEnglish from "./english";
let INSTANCE = new LocaleEnglish();
function initLocale(code) {
if (!INSTANCE || INSTANCE.code !== code) {
const constructors = {
"de_DE": LocaleGerman,
"en_US": LocaleEnglish,
}
if (constructors.hasOwnProperty(code)) {
INSTANCE = new (constructors[code])();
} else {
INSTANCE = { code: code, entries: { } };
}
}
return INSTANCE;
}
function translate(key) {
return (INSTANCE.entries[key] || key);
}
function useLanguageModule(module) {
if (module[INSTANCE.code]) {
for (const [key, value] of Object.entries(module[INSTANCE.code])) {
INSTANCE.entries[key] = value;
}
}
}
export { translate as L, initLocale, useLanguageModule, INSTANCE as currentLocale };