diff options
author | 0neGal <mail@0negal.com> | 2022-05-02 21:49:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-02 21:49:20 +0200 |
commit | c40e331bdc4b2375d3802a515c9b7a032118dea7 (patch) | |
tree | 77c8d84c0fc9d0fbb742cfeeb80efa4762784a9d /src/lang.js | |
parent | 9f2f77558238c28ceb8ff4fca2096602671779e5 (diff) | |
parent | 847a2178e7823749e3096daf24dfcd3df8b236cb (diff) | |
download | Viper-c40e331bdc4b2375d3802a515c9b7a032118dea7.tar.gz Viper-c40e331bdc4b2375d3802a515c9b7a032118dea7.zip |
Merge branch 'main' into enabledmods
Diffstat (limited to 'src/lang.js')
-rw-r--r-- | src/lang.js | 75 |
1 files changed, 51 insertions, 24 deletions
diff --git a/src/lang.js b/src/lang.js index b3cbb43..f2fab3a 100644 --- a/src/lang.js +++ b/src/lang.js @@ -1,35 +1,62 @@ const fs = require("fs"); -var lang = "en"; // Default language - -// Loads fallback/default language strings -var langDef = JSON.parse(fs.readFileSync(__dirname + `/lang/en.json`, "utf8")); - -// If settins are set it'll try to set the language to that instead of -// the default, however if it can't find it, it'll still fallback to the -// default language. This might happen as the default language is -// retrieved from the renderer's navigator.language, which may have -// languages we don't support yet. -if (fs.existsSync("viper.json")) { - lang = JSON.parse(fs.readFileSync("viper.json", "utf8")).lang; - if (! lang) {lang = "en"} // Uses fallback, if language isn't set - if (! fs.existsSync(__dirname + `/lang/${lang}.json`)) { - if (fs.existsSync(__dirname + `/lang/${lang.replace(/-.*$/, "")}.json`)) { - lang = lang.replace(/-.*$/, ""); - } else { - lang = "en"; // Uses fallback if language doesn't exist +const enLang = JSON.parse(fs.readFileSync(__dirname + `/lang/en.json`, "utf8")); +let lang = ""; +var langObj = {}; + + +function _loadTranslation(forcedlang) { + if (fs.existsSync("viper.json")) { + // Validate viper.json + let opts = { + lang: "en", + autolang: true, } + + try { + opts = JSON.parse(fs.readFileSync("viper.json", "utf8")); + }catch (e) {} + + lang = opts.lang; + + if (! lang) {lang = "en"} + + if (forcedlang) {lang = forcedlang} + + if (opts.autolang == false) { + lang = opts.forcedlang; + if (! lang) {lang = "en"} + } + + if (! fs.existsSync(__dirname + `/lang/${lang}.json`)) { + if (fs.existsSync(__dirname + `/lang/${lang.replace(/-.*$/, "")}.json`)) { + lang = lang.replace(/-.*$/, ""); + } else { + lang = "en"; + } + } + } else { + lang = "en"; } + + langObj = JSON.parse(fs.readFileSync(__dirname + `/lang/${lang}.json`, "utf8")); } -var langObj = JSON.parse(fs.readFileSync(__dirname + `/lang/${lang}.json`, "utf8")); -module.exports = (string) => { - if (langObj[string]) { // Returns string from language +module.exports = (string, forcedlang) => { + if (lang === "") { + _loadTranslation(); + } + + if (forcedlang) { + _loadTranslation(forcedlang); + } + + if (langObj[string]) { return langObj[string]; - } else { // If string doesn't exist - if (langDef[string]) { // Retrieves from default lang instead - return langDef[string]; + } else { + if (enLang[string]) { + return enLang[string]; } else { // If it's not in the default lang either, it returns the // string, this is absolute fallback. |