aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlystrasz <contact@remyraes.com>2022-01-07 21:42:21 +0100
committerAlystrasz <contact@remyraes.com>2022-02-19 22:33:04 +0100
commit83469ae18e04c3e812c32c4e6a8dc10c9f74ee54 (patch)
tree9c13a8d063018078d85ee7644e230a6b75252ed3 /src
parent23645e411d9015b5738381323c9151fb075a062f (diff)
downloadViper-83469ae18e04c3e812c32c4e6a8dc10c9f74ee54.tar.gz
Viper-83469ae18e04c3e812c32c4e6a8dc10c9f74ee54.zip
[fix] loading up translation object each time a translation is needed
Diffstat (limited to 'src')
-rw-r--r--src/lang.js41
1 files changed, 20 insertions, 21 deletions
diff --git a/src/lang.js b/src/lang.js
index b3cbb43..3e0ca42 100644
--- a/src/lang.js
+++ b/src/lang.js
@@ -1,35 +1,34 @@
const fs = require("fs");
-var lang = "en"; // Default language
+const enLang = JSON.parse(fs.readFileSync(__dirname + `/lang/en.json`, "utf8"));
+let lang = "en";
+var langObj = {};
-// 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
+function _loadTranslation() {
+ if (fs.existsSync("viper.json")) {
+ lang = JSON.parse(fs.readFileSync("viper.json", "utf8")).lang;
+ 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";
+ }
}
}
+ 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
+ _loadTranslation();
+
+ 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.