Cleanup
This commit is contained in:
14
react/shared/locale/english.js
Normal file
14
react/shared/locale/english.js
Normal 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;
|
||||
}
|
||||
};
|
||||
14
react/shared/locale/german.js
Normal file
14
react/shared/locale/german.js
Normal 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;
|
||||
}
|
||||
}
|
||||
35
react/shared/locale/locale.js
Normal file
35
react/shared/locale/locale.js
Normal 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 };
|
||||
Reference in New Issue
Block a user