diff options
author | 0neGal <mail@0negal.com> | 2021-12-28 18:03:06 +0100 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2021-12-28 18:03:06 +0100 |
commit | c7d3e7480395fa7e5cb0104c8c8efd87ecb6a1a4 (patch) | |
tree | a0c716fd71655f66fa971360a6c483afa92dcbda | |
parent | fccb5815decce6257b8f04cb842a40d6d02110e7 (diff) | |
download | Viper-c7d3e7480395fa7e5cb0104c8c8efd87ecb6a1a4.tar.gz Viper-c7d3e7480395fa7e5cb0104c8c8efd87ecb6a1a4.zip |
attempt at making localization
This may or may not be how we actually do localization in the future,
however for now this seems doable. I will obviously need to look at how
we detect the language, as I think instead of relying on names like
"en-US" just have "en", so we don't have to symlink various editions of
English to the same file. But for now this is a draft, and the important
part of this is rather how the underlying localization works.
-rw-r--r-- | src/app/index.html | 15 | ||||
-rw-r--r-- | src/app/lang.js | 10 | ||||
-rw-r--r-- | src/app/main.js | 4 | ||||
-rw-r--r-- | src/cli.js | 14 | ||||
-rw-r--r-- | src/lang.js | 19 | ||||
-rw-r--r-- | src/lang/en-US.json | 21 |
6 files changed, 69 insertions, 14 deletions
diff --git a/src/app/index.html b/src/app/index.html index 67d2e5c..1c34360 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -6,22 +6,23 @@ <body> <div class="lines"> <div class="line"> - <div class="text">Welcome to Viper!</div> + <div class="text">%%gui.welcome%%</div> <div class="buttons"> - <button id="exit" onclick="exit()">Exit</button> - <button id="update" onclick="update()">Update</button> - <button id="setpath" onclick="setpath()">Game Path</button> + <button id="exit" onclick="exit()">%%gui.exit%%</button> + <button id="update" onclick="update()">%%gui.update%%</button> + <button id="setpath" onclick="setpath()">%%gui.setpath%%</button> </div> </div> <div class="line"> - <div class="text">Launch:</div> + <div class="text">%%gui.launch%%:</div> <div class="buttons"> - <button id="vanilla" onclick="launchVanilla()">Vanilla</button> - <button id="northstar" onclick="launch()">Northstar</button> + <button id="vanilla" onclick="launchVanilla()">%%gui.launchvanilla%%</button> + <button id="northstar" onclick="launch()">%%gui.launchnorthstar%%</button> </div> </div> </div> <script src="main.js"></script> + <script src="lang.js"></script> </body> </html> diff --git a/src/app/lang.js b/src/app/lang.js new file mode 100644 index 0000000..8cf3d4b --- /dev/null +++ b/src/app/lang.js @@ -0,0 +1,10 @@ +html = document.body.innerHTML.split("%%"); + +for (let i = 0; i < html.length; i++) { + if (html[i][0] != " " && + html[i][html[i].length - 1] != " ") { + html[i] = lang(html[i]) + } +} + +document.body.innerHTML = html.join(""); diff --git a/src/app/main.js b/src/app/main.js index d390962..a3a5676 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -2,6 +2,8 @@ const fs = require("fs"); const path = require("path"); const { ipcRenderer } = require("electron"); +const lang = require("../lang"); + var settings = { gamepath: "", zip: "/northstar.zip", @@ -15,7 +17,7 @@ if (fs.existsSync("viper.json")) { settings = {...settings, ...JSON.parse(fs.readFileSync("viper.json", "utf8"))}; settings.zip = path.join(settings.gamepath + "/northstar.zip"); } else { - alert("Game path is not set! Please select the path!"); + alert(lang("gui.missinggamepath")); setpath(); } @@ -5,6 +5,7 @@ const Emitter = require("events"); const events = new Emitter(); const cli = app.commandLine; +const lang = require("./lang"); function hasArgs() { if (cli.hasSwitch("cli") || @@ -24,12 +25,12 @@ function exit(code) { async function init() { if (cli.hasSwitch("help")) { console.log(`options: - --help shows this help message - --debug opens the dev/debug tools + --help ${lang("cli.help.help")} + --debug ${lang("cli.help.debug")} - --cli forces the CLI to enable - --update updates Northstar from your set game path - --setpath sets your game path`) + --cli ${lang("cli.help.cli")} + --update ${lang("cli.help.update")} + --setpath ${lang("cli.help.setpath")}`) // In the future --setpath should be able to understand // relative paths, instead of just absolute ones. exit(); @@ -43,7 +44,8 @@ async function init() { if (cli.getSwitchValue("setpath") != "") { ipcMain.emit("setpathcli", cli.getSwitchValue("setpath")); } else { - console.error("error: No argumment provided for --setpath"); + console.error(`error: ${lang("cli.setpath.noarg")}`); + exit(1); } } diff --git a/src/lang.js b/src/lang.js new file mode 100644 index 0000000..4e4e481 --- /dev/null +++ b/src/lang.js @@ -0,0 +1,19 @@ +const fs = require("fs"); + +var lang = "en-US"; +if (fs.existsSync("viper.json")) { + lang = JSON.parse(fs.readFileSync("viper.json", "utf8")).lang; + if (! fs.existsSync(__dirname + `/lang/${lang}.json`)) { + lang = "en-US"; + } +} + +var langObj = JSON.parse(fs.readFileSync(__dirname + `/lang/${lang}.json`, "utf8")); + +module.exports = (string) => { + if (langObj[string]) { + return langObj[string]; + } else { + return string + } +} diff --git a/src/lang/en-US.json b/src/lang/en-US.json new file mode 100644 index 0000000..2322369 --- /dev/null +++ b/src/lang/en-US.json @@ -0,0 +1,21 @@ +{ + "cli.help.help": "shows this help message", + "cli.help.debug": "opens the dev/debug tools", + "cli.help.cli": "forces the CLI to enable", + "cli.help.update": "updates Northstar from your set game path", + "cli.help.setpath": "sets your game path", + + "cli.setpath.noarg": "No argument provided for --setpath", + + + "gui.welcome": "Welcome to Viper!", + "gui.exit": "Exit", + "gui.update": "Update", + "gui.setpath": "Game Path", + + "gui.launch": "Launch", + "gui.launchvanilla": "Vanilla", + "gui.launchnorthstar": "Northstar", + + "gui.missinggamepath": "Game path is not set! Please select the path!" +} |