@@ -288,26 +288,34 @@ export class DataType extends TypedEmitter {
288
288
const { language, region } = parseBCP47 ( lang )
289
289
if ( ! language ) return doc
290
290
291
- const value = {
291
+ const translations = await this . #getTranslations ( {
292
292
languageCode : language ,
293
293
docRef : {
294
294
docId : doc . docId ,
295
295
versionId : doc . versionId ,
296
296
} ,
297
297
docRefType : doc . schemaName ,
298
- regionCode : region !== null ? region : undefined ,
299
- }
300
- let translations = await this . #getTranslations( value )
301
- // if passing a region code returns no matches,
302
- // fallback to matching only languageCode
303
- if ( translations . length === 0 && value . regionCode ) {
304
- value . regionCode = undefined
305
- translations = await this . #getTranslations( value )
306
- }
298
+ } )
307
299
300
+ /** @type {Set<string> } */
301
+ const translationsWithMatchingRegion = new Set ( )
308
302
for ( const translation of translations ) {
309
303
if ( typeof getProperty ( doc , translation . propertyRef ) === 'string' ) {
310
- setProperty ( doc , translation . propertyRef , translation . message )
304
+ const isMatchingRegion = region
305
+ ? translation . regionCode === region
306
+ : false
307
+ // Prefer translations with a matching region code, but fall back to
308
+ // translations without a region code if no matching region code has
309
+ // been found yet for this propertyRef
310
+ if (
311
+ isMatchingRegion ||
312
+ ! translationsWithMatchingRegion . has ( translation . propertyRef )
313
+ ) {
314
+ setProperty ( doc , translation . propertyRef , translation . message )
315
+ }
316
+ if ( isMatchingRegion ) {
317
+ translationsWithMatchingRegion . add ( translation . propertyRef )
318
+ }
311
319
}
312
320
}
313
321
0 commit comments