From 5302d33b7433c68947083ef57b6bb784bd02124f Mon Sep 17 00:00:00 2001 From: 0neGal Date: Thu, 28 Apr 2022 00:23:34 +0200 Subject: added: error if config file isn't valid Essentially just validates the config file and then prompts you about it, it allows you to reset it directly or just to exit and let yourself fix it. And because the error message appears directly in the renderer we have access to navigator.language, and can therefore still localize the string. However! We can't actually care if the user has disabled auto detection of their language, since... y'know, the config file where that's stored isn't able to be read properly. And so I added an argument to lang(), which allows you to force it to use a specific language if that language is available, if not it defaults back to English. --- src/lang.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'src/lang.js') diff --git a/src/lang.js b/src/lang.js index 041ef35..f2fab3a 100644 --- a/src/lang.js +++ b/src/lang.js @@ -5,13 +5,24 @@ let lang = ""; var langObj = {}; -function _loadTranslation() { +function _loadTranslation(forcedlang) { if (fs.existsSync("viper.json")) { - opts = JSON.parse(fs.readFileSync("viper.json", "utf8")); + // 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"} @@ -32,9 +43,14 @@ function _loadTranslation() { } -module.exports = (string) => { - if (lang === "") +module.exports = (string, forcedlang) => { + if (lang === "") { _loadTranslation(); + } + + if (forcedlang) { + _loadTranslation(forcedlang); + } if (langObj[string]) { return langObj[string]; -- cgit v1.2.3