Skip to content

Commit 8165fb9

Browse files
committed
feat(i18n): check if german text has changed, if yes update all other languages
1 parent 0ae640f commit 8165fb9

File tree

3 files changed

+112
-40
lines changed

3 files changed

+112
-40
lines changed

package-lock.json

Lines changed: 32 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"ionicons": "^7.2.2",
4141
"lodash": "^4.17.21",
4242
"rxjs": "~7.8.1",
43+
"simple-git": "^3.27.0",
4344
"swissparl": "^0.6.2",
4445
"tslib": "^2.2.0",
4546
"zone.js": "~0.14.10"

translate.ts

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const fs = require('node:fs');
22

33
const { Translate } = require('@google-cloud/translate').v2;
4+
const simpleGit = require('simple-git');
5+
6+
const git = simpleGit.default();
47

58
const texts = {
69
de: require('./src/assets/i18n/de.json'),
@@ -22,26 +25,32 @@ const targetLanguages = Object.keys(texts).filter(
2225
const replaceAll = process.argv.includes('--replace-all');
2326
const withoutCapitalization = process.argv.includes('--without-capitalization');
2427

28+
translateTexts();
29+
2530
/**
2631
*
2732
*/
2833
async function translateTexts() {
2934
if (!process.env.CLOUD_TRANSLATE_API_KEY) {
3035
console.log('No API key defined. Please define CLOUD_TRANSLATE_API_KEY');
3136
}
37+
3238
console.log('Translating texts...');
3339
const originalTexts = texts[originalLanguage];
3440

41+
const updatedKeys = await getUpdatedKeys(originalTexts);
42+
3543
for (const key in originalTexts) {
3644
const text = originalTexts[key];
3745

38-
const translationsNeeded = replaceAll
39-
? targetLanguages
40-
: targetLanguages.filter(
41-
(lang) =>
42-
texts[lang][key] === undefined ||
43-
texts[lang][key].startsWith('Missing value')
44-
);
46+
const translationsNeeded =
47+
replaceAll || updatedKeys.includes(key)
48+
? targetLanguages
49+
: targetLanguages.filter(
50+
(lang) =>
51+
texts[lang][key] === undefined ||
52+
texts[lang][key].startsWith('Missing value')
53+
);
4554
const translatedTexts = await translateText(text, translationsNeeded);
4655

4756
console.log(`Translated "${key}"`);
@@ -62,33 +71,7 @@ async function translateTexts() {
6271
}
6372
}
6473

65-
/**
66-
*
67-
* @param text Text to translate
68-
* @param targetLanguages Array of languages to translate the text to
69-
* @returns Object with requested languages as keys and translated text as values
70-
*/
71-
async function translateText(
72-
text: string,
73-
targetLanguages: string[]
74-
): Promise<Partial<{ [key in Language]: string }>> {
75-
const variables = map_variables(text);
76-
const textWithReplacedVariables = replace_variables(text, variables);
77-
const translations = {};
78-
for (const targetLanguage of targetLanguages) {
79-
const [translation] = await translate.translate(textWithReplacedVariables, {
80-
from: originalLanguage,
81-
to: targetLanguage
82-
});
83-
translations[targetLanguage] = reverse_replace_variables(
84-
translation,
85-
variables
86-
);
87-
}
88-
return translations;
89-
}
90-
91-
const map_variables = (text: string): { [key: string]: string } => {
74+
const mapVariablse = (text: string): { [key: string]: string } => {
9275
const variables = text.match(/{{\s*[\w.]+\s*}}/g);
9376
if (!variables) {
9477
return {};
@@ -98,7 +81,7 @@ const map_variables = (text: string): { [key: string]: string } => {
9881
}, {});
9982
};
10083

101-
const replace_variables = (
84+
const replaceVariables = (
10285
text: string,
10386
mappedVariables: { [key: string]: string }
10487
) => {
@@ -107,7 +90,7 @@ const replace_variables = (
10790
}, text);
10891
};
10992

110-
const reverse_replace_variables = (
93+
const reverseReplaceVariables = (
11194
text: string,
11295
mappedVariables: { [key: string]: string }
11396
) => {
@@ -116,4 +99,63 @@ const reverse_replace_variables = (
11699
}, text);
117100
};
118101

119-
translateTexts();
102+
/**
103+
*
104+
* @param text Text to translate
105+
* @param targetLanguages Array of languages to translate the text to
106+
* @returns Object with requested languages as keys and translated text as values
107+
*/
108+
async function translateText(
109+
text: string,
110+
targetLanguages: string[]
111+
): Promise<Partial<{ [key in Language]: string }>> {
112+
const variables = mapVariablse(text);
113+
const textWithReplacedVariables = replaceVariables(text, variables);
114+
const translations = {};
115+
for (const targetLanguage of targetLanguages) {
116+
const [translation] = await translate.translate(textWithReplacedVariables, {
117+
from: originalLanguage,
118+
to: targetLanguage
119+
});
120+
translations[targetLanguage] = reverseReplaceVariables(
121+
translation,
122+
variables
123+
);
124+
}
125+
return translations;
126+
}
127+
128+
/**
129+
* @returns Current texts in original language on main branch
130+
*/
131+
async function getCurrentOriginTextsFromMain(): Promise<{
132+
[key: string]: string;
133+
} | null> {
134+
try {
135+
const fileContent = await git.show(
136+
`main:src/assets/i18n/${originalLanguage}.json`
137+
);
138+
const originalTextsFromMain = JSON.parse(fileContent);
139+
console.log('Original texts:', originalTextsFromMain);
140+
return originalTextsFromMain;
141+
} catch (error) {
142+
console.error('Error getting current origin texts from main');
143+
return null;
144+
}
145+
}
146+
147+
/**
148+
* @param currentOriginalTexts Object with keys and texts in original language
149+
* @returns Array of keys that have changed compared to current state on main branch
150+
*/
151+
async function getUpdatedKeys(currentOriginalTexts: {
152+
[key: string]: string;
153+
}): Promise<string[]> {
154+
const originalTextsFromMain = await getCurrentOriginTextsFromMain();
155+
if (!originalTextsFromMain) {
156+
return Object.keys(currentOriginalTexts);
157+
}
158+
return Object.keys(currentOriginalTexts).filter(
159+
(key) => currentOriginalTexts[key] !== originalTextsFromMain[key]
160+
);
161+
}

0 commit comments

Comments
 (0)