summaryrefslogtreecommitdiff
path: root/packages/pogen
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-01-17 10:22:49 -0300
committerSebastian <sebasjm@gmail.com>2024-01-17 10:22:49 -0300
commitf5a54633dca3599dab82730fd7d550c0289f170f (patch)
tree5c4d2b25973b891e0b19cb8401d24354a1227105 /packages/pogen
parent87765a4023e33d9502cf55ad2592dabf262ddc69 (diff)
downloadwallet-core-f5a54633dca3599dab82730fd7d550c0289f170f.tar.gz
wallet-core-f5a54633dca3599dab82730fd7d550c0289f170f.tar.bz2
wallet-core-f5a54633dca3599dab82730fd7d550c0289f170f.zip
add translation completeness from pogen to the UI
Diffstat (limited to 'packages/pogen')
-rw-r--r--packages/pogen/src/po2ts.ts93
1 files changed, 83 insertions, 10 deletions
diff --git a/packages/pogen/src/po2ts.ts b/packages/pogen/src/po2ts.ts
index d37bdb902..0e2a0d6ea 100644
--- a/packages/pogen/src/po2ts.ts
+++ b/packages/pogen/src/po2ts.ts
@@ -19,12 +19,53 @@
*/
// @ts-ignore
-import * as po2json from "po2json";
+import * as po2jsonLib from "po2json";
import * as fs from "fs";
-import * as path from "path";
import glob = require("glob");
-const DEFAULT_STRING_PRELUDE = "export const strings: any = {};\n\n"
+//types defined by the po2json library
+type Header = {
+ domain: string;
+ lang: string;
+ 'plural_forms': string;
+};
+
+type MessagesType = Record<string, undefined | Array<string>> & { "": Header }
+interface pojsonType {
+ // X-Domain or 'messages'
+ domain: string;
+ locale_data: {
+ messages: MessagesType
+ }
+}
+// ----------- end pf po2json
+
+interface StringsType {
+ // X-Domain or 'messages'
+ domain: string;
+ lang: string;
+ completeness: number,
+ 'plural_forms': string;
+ locale_data: {
+ messages: Record<string, undefined | Array<string>>
+ }
+}
+
+// This prelude match the types above
+const TYPES_FOR_STRING_PRELUDE = `
+export interface StringsType {
+ domain: string;
+ lang: string;
+ completeness: number;
+ 'plural_forms': string;
+ locale_data: {
+ messages: Record<string, any>;
+ };
+};
+`;
+
+const DEFAULT_STRING_PRELUDE = `${TYPES_FOR_STRING_PRELUDE}export const strings: Record<string,StringsType> = {};\n\n`
+
export function po2ts(): void {
const files = glob.sync("src/i18n/*.po");
@@ -54,16 +95,26 @@ export function po2ts(): void {
}
const lang = m[1];
- const pojson = po2json.parseFileSync(filename, {
+ const poAsJson: pojsonType = po2jsonLib.parseFileSync(filename, {
format: "jed1.x",
fuzzy: true,
});
- const s =
- "strings['" +
- lang +
- "'] = " +
- JSON.stringify(pojson, null, " ") +
- ";\n\n";
+ const header = poAsJson.locale_data.messages[""]
+ const total = calculateTotalTranslations(poAsJson.locale_data.messages)
+ const completeness =
+ header.lang === "en"
+ ? 100 // 'en' is always complete
+ : Math.floor(total.translations * 100 / total.keys);
+
+ const strings: StringsType = {
+ locale_data: poAsJson.locale_data,
+ domain: poAsJson.domain,
+ plural_forms: header.plural_forms,
+ lang: header.lang,
+ completeness,
+ }
+ const value = JSON.stringify(strings, undefined, 2)
+ const s = `strings['${lang}'] = ${value};\n\n`
chunks.push(s);
}
@@ -71,3 +122,25 @@ export function po2ts(): void {
fs.writeFileSync("src/i18n/strings.ts", tsContents);
}
+
+function calculateTotalTranslations(msgs: MessagesType): { keys: number, translations: number } {
+ const kv = Object.entries(msgs)
+ const [keys, translations] = kv.reduce(([total, withTranslation], translation) => {
+ if (!translation || translation.length !== 2 || !translation[1]) {
+ //curent key is empty
+ return [total, withTranslation]
+ }
+ const v = translation[1]
+ if (!Array.isArray(v)) {
+ // this is not a translation
+ return [total, withTranslation]
+ }
+ if (!v.length || !v[0].length) {
+ //translation is missing
+ return [total + 1, withTranslation]
+ }
+ //current key has a translation
+ return [total + 1, withTranslation + 1]
+ }, [0, 0])
+ return { keys, translations }
+} \ No newline at end of file