@@ -20,7 +20,7 @@ const fullDir = workDir + "/full";
20
20
const coreDir = workDir + "/core" ;
21
21
const outDir = workDir + "/output" ;
22
22
23
- String . prototype . rsplit = function ( sep , maxsplit ) {
23
+ String . prototype . rsplit = function ( sep , maxsplit ) {
24
24
var split = this . split ( sep ) ;
25
25
return maxsplit
26
26
? [ split . slice ( 0 , - maxsplit ) . join ( sep ) ] . concat ( split . slice ( - maxsplit ) )
@@ -45,7 +45,7 @@ const TRANSLATION_FRAGMENTS = [
45
45
46
46
function recursiveFlatten ( prefix , data ) {
47
47
let output = { } ;
48
- Object . keys ( data ) . forEach ( function ( key ) {
48
+ Object . keys ( data ) . forEach ( function ( key ) {
49
49
if ( typeof data [ key ] === "object" ) {
50
50
output = {
51
51
...output ,
@@ -107,7 +107,12 @@ function lokaliseTransform(data, original, file) {
107
107
output [ key ] = lokaliseTransform ( value , original , file ) ;
108
108
} else {
109
109
output [ key ] = value . replace ( re_key_reference , ( match , key ) => {
110
- const replace = key . split ( "::" ) . reduce ( ( tr , k ) => tr [ k ] , original ) ;
110
+ const replace = key . split ( "::" ) . reduce ( ( tr , k ) => {
111
+ if ( ! tr ) {
112
+ throw Error ( `Invalid key placeholder ${ key } in ${ file . path } ` ) ;
113
+ }
114
+ return tr [ k ] ;
115
+ } , original ) ;
111
116
if ( typeof replace !== "string" ) {
112
117
throw Error ( `Invalid key placeholder ${ key } in ${ file . path } ` ) ;
113
118
}
@@ -118,7 +123,7 @@ function lokaliseTransform(data, original, file) {
118
123
return output ;
119
124
}
120
125
121
- gulp . task ( "clean-translations" , function ( ) {
126
+ gulp . task ( "clean-translations" , function ( ) {
122
127
return del ( [ workDir ] ) ;
123
128
} ) ;
124
129
@@ -129,7 +134,7 @@ gulp.task("ensure-translations-build-dir", (done) => {
129
134
done ( ) ;
130
135
} ) ;
131
136
132
- gulp . task ( "create-test-metadata" , function ( cb ) {
137
+ gulp . task ( "create-test-metadata" , function ( cb ) {
133
138
fs . writeFile (
134
139
workDir + "/testMetadata.json" ,
135
140
JSON . stringify ( {
@@ -147,7 +152,7 @@ gulp.task(
147
152
return gulp
148
153
. src ( path . join ( paths . translations_src , "en.json" ) )
149
154
. pipe (
150
- transform ( function ( data , file ) {
155
+ transform ( function ( data , file ) {
151
156
return recursiveEmpty ( data ) ;
152
157
} )
153
158
)
@@ -165,28 +170,28 @@ gulp.task(
165
170
* project is buildable immediately after merging new translation keys, since
166
171
* the Lokalise update to translations/en.json will not happen immediately.
167
172
*/
168
- gulp . task ( "build-master-translation" , function ( ) {
173
+ gulp . task ( "build-master-translation" , function ( ) {
169
174
return gulp
170
175
. src ( path . join ( paths . translations_src , "en.json" ) )
171
176
. pipe (
172
- transform ( function ( data , file ) {
177
+ transform ( function ( data , file ) {
173
178
return lokaliseTransform ( data , data , file ) ;
174
179
} )
175
180
)
176
181
. pipe ( rename ( "translationMaster.json" ) )
177
182
. pipe ( gulp . dest ( workDir ) ) ;
178
183
} ) ;
179
184
180
- gulp . task ( "build-merged-translations" , function ( ) {
185
+ gulp . task ( "build-merged-translations" , function ( ) {
181
186
return gulp
182
187
. src ( [ inDir + "/*.json" , workDir + "/test.json" ] , { allowEmpty : true } )
183
188
. pipe (
184
- transform ( function ( data , file ) {
189
+ transform ( function ( data , file ) {
185
190
return lokaliseTransform ( data , data , file ) ;
186
191
} )
187
192
)
188
193
. pipe (
189
- foreach ( function ( stream , file ) {
194
+ foreach ( function ( stream , file ) {
190
195
// For each language generate a merged json file. It begins with the master
191
196
// translation as a failsafe for untranslated strings, and merges all parent
192
197
// tags into one file for each specific subtag
@@ -223,7 +228,7 @@ var taskName;
223
228
const splitTasks = [ ] ;
224
229
TRANSLATION_FRAGMENTS . forEach ( ( fragment ) => {
225
230
taskName = "build-translation-fragment-" + fragment ;
226
- gulp . task ( taskName , function ( ) {
231
+ gulp . task ( taskName , function ( ) {
227
232
// Return only the translations for this fragment.
228
233
return gulp
229
234
. src ( fullDir + "/*.json" )
@@ -242,12 +247,31 @@ TRANSLATION_FRAGMENTS.forEach((fragment) => {
242
247
} ) ;
243
248
244
249
taskName = "build-translation-core" ;
245
- gulp . task ( taskName , function ( ) {
250
+ gulp . task ( taskName , function ( ) {
246
251
// Remove the fragment translations from the core translation.
247
252
return gulp
248
253
. src ( fullDir + "/*.json" )
249
254
. pipe (
250
- transform ( ( data ) => {
255
+ transform ( ( data , file ) => {
256
+ // HACK to pull in old state translations for cast
257
+ if ( process . env . IS_CAST ) {
258
+ const legacyTranslationsPath = path . join (
259
+ "cast/src/translations" ,
260
+ file . relative
261
+ ) ;
262
+ if ( fs . existsSync ( legacyTranslationsPath ) ) {
263
+ const legacyStrings = JSON . parse (
264
+ fs . readFileSync ( legacyTranslationsPath , "utf-8" )
265
+ ) ;
266
+ // These 2 translations still exist today.
267
+ if ( legacyStrings . state && "default" in legacyStrings . state ) {
268
+ legacyStrings . default . unknown = data . state . default . unknown ;
269
+ legacyStrings . default . unavailable =
270
+ data . state . default . unavailable ;
271
+ }
272
+ data . state = legacyStrings . state ;
273
+ }
274
+ }
251
275
TRANSLATION_FRAGMENTS . forEach ( ( fragment ) => {
252
276
delete data . ui . panel [ fragment ] ;
253
277
} ) ;
@@ -259,7 +283,7 @@ gulp.task(taskName, function() {
259
283
260
284
splitTasks . push ( taskName ) ;
261
285
262
- gulp . task ( "build-flattened-translations" , function ( ) {
286
+ gulp . task ( "build-flattened-translations" , function ( ) {
263
287
// Flatten the split versions of our translations, and move them into outDir
264
288
return gulp
265
289
. src (
@@ -269,7 +293,7 @@ gulp.task("build-flattened-translations", function() {
269
293
{ base : workDir }
270
294
)
271
295
. pipe (
272
- transform ( function ( data ) {
296
+ transform ( function ( data ) {
273
297
// Polymer.AppLocalizeBehavior requires flattened json
274
298
return flatten ( data ) ;
275
299
} )
@@ -351,7 +375,7 @@ gulp.task(
351
375
)
352
376
. pipe ( merge ( { } ) )
353
377
. pipe (
354
- transform ( function ( data ) {
378
+ transform ( function ( data ) {
355
379
const newData = { } ;
356
380
Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
357
381
// Filter out translations without native name.
0 commit comments