From c7d3e7480395fa7e5cb0104c8c8efd87ecb6a1a4 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Tue, 28 Dec 2021 18:03:06 +0100 Subject: 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. --- src/app/index.html | 15 ++++++++------- src/app/lang.js | 10 ++++++++++ src/app/main.js | 4 +++- src/cli.js | 14 ++++++++------ src/lang.js | 19 +++++++++++++++++++ src/lang/en-US.json | 21 +++++++++++++++++++++ 6 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/app/lang.js create mode 100644 src/lang.js create mode 100644 src/lang/en-US.json (limited to 'src') 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 @@
-
Welcome to Viper!
+
%%gui.welcome%%
- - - + + +
-
Launch:
+
%%gui.launch%%:
- - + +
+ 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(); } diff --git a/src/cli.js b/src/cli.js index e5fe042..8593117 100644 --- a/src/cli.js +++ b/src/cli.js @@ -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!" +} -- cgit v1.2.3 From 8b90dc59f005ccb394f3392a80dcc28cc0ac73e7 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Tue, 28 Dec 2021 18:16:46 +0100 Subject: if no lang is set, use "en" By error I forgot to include this check... I also added in the ability for it to look for a lang file without the extra locale info on the end, i.e if "en-GB" is not found it'll try "en" --- src/lang.js | 9 +++++++-- src/lang/en-US.json | 21 --------------------- src/lang/en.json | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 23 deletions(-) delete mode 100644 src/lang/en-US.json create mode 100644 src/lang/en.json (limited to 'src') diff --git a/src/lang.js b/src/lang.js index 4e4e481..266fb8b 100644 --- a/src/lang.js +++ b/src/lang.js @@ -1,10 +1,15 @@ const fs = require("fs"); -var lang = "en-US"; +var lang = "en"; 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`)) { - lang = "en-US"; + if (fs.existsSync(__dirname + `/lang/${lang.replace(/-.*$/, "")}.json`)) { + lang = lang.replace(/-.*$/, ""); + } else { + lang = "en"; + } } } diff --git a/src/lang/en-US.json b/src/lang/en-US.json deleted file mode 100644 index 2322369..0000000 --- a/src/lang/en-US.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "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!" -} diff --git a/src/lang/en.json b/src/lang/en.json new file mode 100644 index 0000000..2322369 --- /dev/null +++ b/src/lang/en.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!" +} -- cgit v1.2.3 From aefef2040280ae9e4292c39df65f2b378128aa07 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Wed, 29 Dec 2021 00:39:52 +0100 Subject: uses navigator.language to determine language --- src/app/main.js | 3 +++ src/index.js | 1 + src/utils.js | 6 +++++- 3 files changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/app/main.js b/src/app/main.js index a3a5676..579f785 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -7,12 +7,15 @@ const lang = require("../lang"); var settings = { gamepath: "", zip: "/northstar.zip", + lang: navigator.language, excludes: [ "ns_startup_args.txt", "ns_startup_args_dedi.txt" ] } +ipcRenderer.send("setlang", settings.lang); + if (fs.existsSync("viper.json")) { settings = {...settings, ...JSON.parse(fs.readFileSync("viper.json", "utf8"))}; settings.zip = path.join(settings.gamepath + "/northstar.zip"); diff --git a/src/index.js b/src/index.js index 4e64414..fead15b 100644 --- a/src/index.js +++ b/src/index.js @@ -33,6 +33,7 @@ function start() { } ipcMain.on("launch", (event) => {utils.launch()}) +ipcMain.on("setlang", (event, lang) => {utils.setlang(lang)}) ipcMain.on("launchVanilla", (event) => {utils.launch("vanilla")}) ipcMain.on("update", (event) => {utils.update()}) diff --git a/src/utils.js b/src/utils.js index 85ce5ce..ec15554 100644 --- a/src/utils.js +++ b/src/utils.js @@ -16,6 +16,7 @@ process.chdir(app.getPath("appData")); var settings = { gamepath: "", + lang: "en-US", zip: "/northstar.zip", excludes: [ "ns_startup_args.txt", @@ -30,7 +31,6 @@ if (fs.existsSync("viper.json")) { console.log("Game path is not set! Please select the path."); } - function setpath(win) { if (! win) { settings.gamepath = cli.param("setpath"); @@ -110,4 +110,8 @@ module.exports = { update, setpath, settings, + setlang: (lang) => { + settings.lang = lang; + saveSettings(); + }, } -- cgit v1.2.3