aboutsummaryrefslogtreecommitdiff
path: root/src/lang.js
blob: 041ef3510b2d7006b3287bae1fb0247df535d2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const fs = require("fs");

const enLang = JSON.parse(fs.readFileSync(__dirname + `/lang/en.json`, "utf8"));
let lang = "";
var langObj = {};


function _loadTranslation() {
	if (fs.existsSync("viper.json")) {
		opts = JSON.parse(fs.readFileSync("viper.json", "utf8"));
		lang = opts.lang;

		if (! lang) {lang = "en"}

		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"));
}


module.exports = (string) => {
	if (lang === "")
		_loadTranslation();

	if (langObj[string]) {
		return langObj[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.
			return string;
		}
	}
}