99
1010from moz .l10n .formats import Format
1111from moz .l10n .formats .xliff import xliff_is_xcode
12- from moz .l10n .message import message_from_json , parse_message , serialize_message
12+ from moz .l10n .message import message_from_json , serialize_message
1313from moz .l10n .model import (
1414 CatchallKey ,
1515 Entry ,
2222 SelectMessage ,
2323)
2424from moz .l10n .paths import L10nConfigPaths , L10nDiscoverPaths
25- from moz .l10n .resource import parse_resource , serialize_resource
25+ from moz .l10n .resource import serialize_resource
2626
2727from django .conf import settings
2828from django .db .models import Q
@@ -236,8 +236,11 @@ def update_changed_resources(
236236 )
237237 res = build_moz_l10n_resource (db_res )
238238 for locale in locales :
239- lc_scope = f"[{ project .slug } :{ path } , { locale .code } ]"
240- lc_translations = [tx for tx in translations if tx .locale_id == locale .pk ]
239+ lc_translations = {
240+ tuple (tx .entity .key ): tx
241+ for tx in translations
242+ if tx .locale_id == locale .pk
243+ }
241244 target_path = paths .format_target_path (target , locale .code )
242245 if not lc_translations and not isfile (target_path ):
243246 continue
@@ -249,12 +252,14 @@ def update_changed_resources(
249252 for line in serialize_resource (tr_res , gettext_plurals = lc_plurals ):
250253 file .write (line )
251254 updated_locales .add (locale )
252- for tx in lc_translations :
255+ for tx in lc_translations . values () :
253256 if tx .approved and tx .entity in changed_entities and tx .user :
254257 translators [tx .user ].add (locale .code )
255258 count += 1
256259 except Exception as error :
257- log .error (f"{ lc_scope } Update failed: { error } " )
260+ log .error (
261+ f"[{ project .slug } :{ path } , { locale .code } ] Update failed: { error } "
262+ )
258263 continue
259264 return count , updated_locales , translators
260265
@@ -324,60 +329,26 @@ def _entry_from_entity(format: Format, e: Entity) -> Entry[Message]:
324329
325330
326331def build_translated_resource (
327- locale : Locale , translations : list [ Translation ], res : Resource
328- ) -> Resource :
332+ locale : Locale , translations : dict [ Id , Translation ], res : Resource [ Message ]
333+ ) -> Resource [ Message ] :
329334 res = deepcopy (res )
330- if res .format == Format .fluent :
331- trans_res = parse_resource (
332- Format .fluent , "" .join (tx .string for tx in translations )
333- )
334- trans_entries : dict [Id , Entry | None ] = {
335- entry .id : entry
336- for section in trans_res .sections
337- for entry in section .entries
338- if isinstance (entry , Entry )
339- }
340- for section in res .sections :
341- rm : list [Entry ] = []
342- for entry in section .entries :
343- if isinstance (entry , Entry ):
344- te = trans_entries .get (entry .id , None )
345- if te is None :
346- rm .append (entry )
347- else :
348- entry .value = te .value
349- entry .properties = (
350- te .properties
351- if entry .id [0 ].startswith ("-" )
352- else {
353- name : tp
354- for name , tp in te .properties .items ()
355- if name in entry .properties
356- }
357- )
358- if rm :
359- section .entries = [e for e in section .entries if e not in rm ]
360- else :
361- # The iOS locale remapping is a hacky workaround for Xcode projects only,
362- # so don't apply it to other XLIFF projects.
363- is_xcode = res .format == Format .xliff and xliff_is_xcode (res )
364- for section in res .sections :
365- if (
366- res .format == Format .xliff
367- and section .get_meta ("@source-language" ) is not None
368- ):
369- lc = str (locale .code )
370- if is_xcode and lc in ios_locale_map :
371- lc = ios_locale_map [lc ]
372- section .set_meta ("@target-language" , lc )
373-
374- rm = []
375- for entry in section .entries :
376- if isinstance (entry , Entry ):
377- if not _set_translation (translations , res .format , section , entry ):
335+ for section in res .sections :
336+ rm = []
337+ for entry in section .entries :
338+ assert isinstance (entry , Entry )
339+ tx = translations .get (section .id + entry .id , None )
340+ if tx is not None :
341+ _set_translation (res .format , entry , tx )
342+ else :
343+ match res .format :
344+ case Format .gettext if isinstance (entry .value , SelectMessage ):
345+ entry .value .variants = {(CatchallKey (),): []}
346+ case Format .gettext | Format .xliff :
347+ entry .value = PatternMessage ([])
348+ case _:
378349 rm .append (entry )
379- if rm and res . format not in ( Format . gettext , Format . xliff ) :
380- section .entries = [e for e in section .entries if e not in rm ]
350+ if rm :
351+ section .entries = [e for e in section .entries if e not in rm ]
381352
382353 match res .format :
383354 case Format .gettext :
@@ -392,8 +363,15 @@ def build_translated_resource(
392363 for key , value in header .items ()
393364 if key not in gettext_trim_headers
394365 ]
366+
395367 case Format .xliff :
396- pass
368+ lc = str (locale .code )
369+ if xliff_is_xcode (res ):
370+ lc = ios_locale_map .get (lc , lc )
371+ for section in res .sections :
372+ if section .get_meta ("@source-language" ) is not None :
373+ section .set_meta ("@target-language" , lc )
374+
397375 case _:
398376 res .sections = [
399377 section
@@ -403,27 +381,22 @@ def build_translated_resource(
403381 return res
404382
405383
406- def _set_translation (
407- translations : list [Translation ],
408- format : Format | None ,
409- section : Section ,
410- entry : Entry ,
411- ) -> bool :
412- key = list (section .id + entry .id )
413- tx = next ((tx for tx in translations if tx .entity .key == key ), None )
414- if tx is None :
415- if format == Format .gettext :
416- if isinstance (entry .value , SelectMessage ):
417- entry .value .variants = {(CatchallKey (),): []}
418- else :
419- entry .value = PatternMessage ([])
420- return True
421- else :
422- return False
423-
384+ def _set_translation (format : Format | None , entry : Entry , tx : Translation ) -> None :
424385 match format :
386+ case Format .fluent :
387+ entry .value = message_from_json (tx .value )
388+ is_term = entry .id [0 ].startswith ("-" )
389+ entry .properties = (
390+ {
391+ name : message_from_json (pv )
392+ for name , pv in tx .properties .items ()
393+ if is_term or name in entry .properties
394+ }
395+ if tx .properties
396+ else {}
397+ )
425398 case Format .android | Format .gettext | Format .webext | Format .xliff :
426- msg = parse_message ( Format . mf2 , tx .string )
399+ msg = message_from_json ( tx .value )
427400 if isinstance (entry .value , SelectMessage ):
428401 entry .value .variants = (
429402 {(CatchallKey (),): msg .pattern }
@@ -443,6 +416,4 @@ def _set_translation(
443416 entry .meta = [m for m in entry .meta if m != fuzzy_flag ]
444417
445418 case _:
446- entry .value = tx .string
447-
448- return True
419+ entry .value = message_from_json (tx .value )
0 commit comments