cobalt/src/localization/manager.js
wukko 2212ea7dc8 just a clean up
- moved pattern testers out of match module (thanks you @radkii)
- button borders are now easier to see

the reason this commit exists is because i tried to implement a language picker and failed to do it properly so here we are
2022-08-04 17:22:40 +06:00

44 lines
1.3 KiB
JavaScript

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}!!`
}
}