remade localization system once again
- new localization system: fast, dynamic, way more organized - localization strings are WAY more descriptive - it's now easier to add support for other languages (just one loc file instead of five) - localization now falls back to english if localized string isnt available - got rid of all static language selectors (probably) - slightly updated english and russian strings - miscellaneous settings items have been bundled together and moved to the bottom, cause they're used the least - bottom links should no longer touch the popup border on overflow - rearranged popup order in the rendered page - bumped version up to 2.2.5 if you see strings that are like this: !!EXAMPLE!! or withoutspace please file an issue on github
This commit is contained in:
42
src/localization/manager.js
Normal file
42
src/localization/manager.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import * as fs from "fs";
|
||||
import { appName, repo } from "../modules/config.js";
|
||||
import loadJson from "../modules/sub/loadJSON.js";
|
||||
|
||||
const locPath = './src/localization/languages'
|
||||
let loc = {}
|
||||
|
||||
export async function loadLoc() {
|
||||
fs.readdir(locPath, (err, files) => {
|
||||
files.forEach(file => {
|
||||
loc[file.split('.')[0]] = loadJson(`${locPath}/${file}`)
|
||||
});
|
||||
})
|
||||
}
|
||||
await loadLoc();
|
||||
export function replaceBase(s) {
|
||||
return s.replace(/\n/g, '<br/>').replace(/{appName}/g, appName).replace(/{repo}/g, repo)
|
||||
}
|
||||
export function replaceAll(lang, str, string, replacement) {
|
||||
let s = replaceBase(str[string])
|
||||
if (replacement) s = s.replace(/{s}/g, replacement);
|
||||
if (s.match('{')) {
|
||||
Object.keys(loc[lang]["substrings"]).forEach(sub => {
|
||||
s = replaceBase(s.replace(`{${sub}}`, loc[lang]["substrings"][sub]))
|
||||
});
|
||||
}
|
||||
return s
|
||||
}
|
||||
export default function(lang, string, replacement) {
|
||||
try {
|
||||
if (!Object.keys(loc).includes(lang)) lang = 'en';
|
||||
let str = loc[lang]["strings"];
|
||||
if (str && str[string]) {
|
||||
return replaceAll(lang, str, string, replacement)
|
||||
} else {
|
||||
str = loc["en"]["strings"];
|
||||
return replaceAll(lang, str, string, replacement)
|
||||
}
|
||||
} catch (e) {
|
||||
return `!!${string}!!`
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user