11const fs = require ( 'node:fs' ) ;
22
33const { Translate } = require ( '@google-cloud/translate' ) . v2 ;
4+ const simpleGit = require ( 'simple-git' ) ;
5+
6+ const git = simpleGit . default ( ) ;
47
58const texts = {
69 de : require ( './src/assets/i18n/de.json' ) ,
@@ -22,26 +25,32 @@ const targetLanguages = Object.keys(texts).filter(
2225const replaceAll = process . argv . includes ( '--replace-all' ) ;
2326const withoutCapitalization = process . argv . includes ( '--without-capitalization' ) ;
2427
28+ translateTexts ( ) ;
29+
2530/**
2631 *
2732 */
2833async 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