Skip to content

Commit

Permalink
Merge pull request #9 from matrix-org/t3chguy/localazy-1
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Aug 18, 2023
2 parents 4f514e8 + e5d73f0 commit bd9b1e4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"dependencies": {
"@babel/parser": "^7.18.5",
"@babel/traverse": "^7.18.5",
"lodash": "^4.17.21",
"walk": "^2.3.15"
},
"devDependencies": {
"@types/babel__traverse": "^7.17.1",
"@types/lodash": "^4.14.197",
"@types/node": "^18.0.0",
"@types/walk": "^2.3.1",
"typescript": "^4.7.4"
Expand Down
37 changes: 22 additions & 15 deletions scripts/gen-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
Node,
} from "@babel/types";
import { ParserPlugin } from "@babel/parser";
import _ from "lodash";

// Find the package.json for the project we're running gen-18n against
const projectPackageJsonPath = path.join(process.cwd(), 'package.json');
Expand All @@ -52,8 +53,9 @@ const TRANSLATIONS_FUNCS = ['_t', '_td', '_tDom']
// "matrix_i18n_extra_translation_funcs" key
.concat(projectPackageJson.matrix_i18n_extra_translation_funcs || []);

const INPUT_TRANSLATIONS_FILE = 'src/i18n/strings/en_EN.json';
const OUTPUT_FILE = 'src/i18n/strings/en_EN.json';
const NESTING_KEY = process.env["NESTING_KEY"] || "|";
const INPUT_TRANSLATIONS_FILE = process.env["INPUT_FILE"] || 'src/i18n/strings/en_EN.json';
const OUTPUT_FILE = process.env["OUTPUT_FILE"] || 'src/i18n/strings/en_EN.json';

// NB. The sync version of walk is broken for single files,
// so we walk all of res rather than just res/home.html.
Expand Down Expand Up @@ -238,11 +240,14 @@ function getTranslationsOther(file: string): Set<string> {
return trs;
}

type Translations = Record<string, string | {
type Translation = string | {
one?: string;
other: string;
zero?: string;
}>;
};

interface Translations {
[key: string]: Translation | Translations;
}

const inputTranslationsRaw: Readonly<Translations> = JSON.parse(fs.readFileSync(INPUT_TRANSLATIONS_FILE, { encoding: 'utf8' }));
const translatables = new Set<string>();
Expand Down Expand Up @@ -292,19 +297,21 @@ for (const path of SEARCH_PATHS) {
}
}

function getPath(key: string): string[] {
return key.split(NESTING_KEY);
}

const trObj: Translations = {};
for (const tr of translatables) {
if (!plurals.has(tr)) {
trObj[tr] = tr;
} else if (inputTranslationsRaw[tr]) {
trObj[tr] = inputTranslationsRaw[tr];
const path = getPath(tr);
if (_.get(inputTranslationsRaw, path)) {
_.set(trObj, path, _.get(inputTranslationsRaw, path));
} else if (!plurals.has(tr)) {
_.set(trObj, path, tr);
} else {
// Migration path for legacy flat file format
trObj[tr] = {
"other": inputTranslationsRaw[tr + "|other"] as string || tr,
"one": inputTranslationsRaw[tr + "|one"] as string,
"zero": inputTranslationsRaw[tr + "|zero"] as string,
};
_.set(trObj, path, {
"other": tr,
})
}
}

Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
dependencies:
"@babel/types" "^7.3.0"

"@types/lodash@^4.14.197":
version "4.14.197"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.197.tgz#e95c5ddcc814ec3e84c891910a01e0c8a378c54b"
integrity sha512-BMVOiWs0uNxHVlHBgzTIqJYmj+PgCo4euloGF+5m4okL3rEYzM2EEv78mw8zWSMM57dM7kVIgJ2QDvwHSoCI5g==

"@types/node@*", "@types/node@^18.0.0":
version "18.0.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-18.0.0.tgz#67c7b724e1bcdd7a8821ce0d5ee184d3b4dd525a"
Expand Down Expand Up @@ -213,6 +218,11 @@ jsesc@^2.5.1:
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==

lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

ms@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
Expand Down

0 comments on commit bd9b1e4

Please sign in to comment.