Skip to content

Commit

Permalink
Use js for extract_locales
Browse files Browse the repository at this point in the history
  • Loading branch information
luboslenco committed Jan 20, 2024
1 parent c5aaac9 commit 4688496
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 107 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ git submodule update --init --recursive

**Generating a locale file**
```bash
pip install typing_extensions -t base/tools
python ./base/tools/extract_locales.py <locale code>
export ARM_LOCALE=<locale code>
./armorcore/Kinc/make --from base/Tools --kfile extract_locales.js
# Generates a `base/Assets/locale/<locale code>.json` file
```

Expand Down
64 changes: 64 additions & 0 deletions base/Tools/extract_locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Extracts localizable strings from a set of source files and writes them to JSON files.
// This script can create new translations or update existing ones.
// Usage:
// `export ARM_LOCALE=<locale code>`
// `../../armorcore/Kinc/make --kfile extract_locales.js`
// Generates a `base/Assets/locale/<locale code>.json` file

const fs = require('fs');

if (!process.env.ARM_LOCALE) {
console.log("ARM_LOCALE env variable not set!");
}

let locale = process.env.ARM_LOCALE;
let locale_path = "./base/Assets/locale/" + locale + ".json";

let out = {};
let old = {};
if (fs.existsSync(locale_path)) {
old = JSON.parse(fs.readFileSync(locale_path).toString());
}

let source_paths = [
"base/Sources", "base/Sources/nodes",
"armorpaint/Sources", "armorpaint/Sources/nodes",
"armorlab/Sources", "armorlab/Sources/nodes",
"armorsculpt/Sources", "armorsculpt/Sources/nodes",
"armorforge/Sources", "armorforge/Sources/nodes"
];

for (let path of source_paths) {
if (!fs.existsSync(path)) continue;

let files = fs.readdirSync(path);
for (let file of files) {
if (!file.endsWith(".ts")) continue;

let data = fs.readFileSync(path + "/" + file).toString();
let start = 0;
while (true) {
start = data.indexOf('tr("', start);
if (start == -1) break;
start += 4; // tr("

let end_a = data.indexOf('")', start);
let end_b = data.indexOf('",', start);
if (end_a == -1) end_a = end_b;
if (end_b == -1) end_b = end_a;
let end = end_a < end_b ? end_a : end_b;

let val = data.substring(start, end);
val = val.replaceAll("\\n", "\n");
if (old.hasOwnProperty(val)) {
out[val] = old[val];
}
else {
out[val] = "";
}
start = end;
}
}
}

fs.writeFileSync(locale_path, JSON.stringify(out, Object.keys(out).sort(), 4));
105 changes: 0 additions & 105 deletions base/Tools/extract_locales.py

This file was deleted.

0 comments on commit 4688496

Please sign in to comment.