@@ -64,9 +64,9 @@ class FilePartInfo(NamedTuple):
6464 line_number : int
6565 translatable : bool
6666 context : str | None
67- text : str | None = None
68- tag : str | None = None
69- tag_parts : Sequence [str ] | None = None
67+ text : str = ""
68+ tag : str = ""
69+ tag_parts : Sequence [str ] = ()
7070
7171
7272def parse_raw_file (file : Iterable [str ]) -> Iterator [FilePartInfo ]:
@@ -75,47 +75,50 @@ def parse_raw_file(file: Iterable[str]) -> Iterator[FilePartInfo]:
7575 for token in tokenize_raw_file (file ):
7676 if not token .is_tag :
7777 yield FilePartInfo (line_number = token .line_number , translatable = False , context = context , text = token .text )
78+ continue
79+
80+ tag = token .text
81+ tag_parts = split_tag (tag )
82+
83+ if tag_parts [0 ] == "OBJECT" :
84+ object_name = tag_parts [1 ]
85+ yield FilePartInfo (line_number = token .line_number , translatable = False , context = context , tag = tag )
86+ elif object_name and (
87+ tag_parts [0 ] == object_name
88+ or (object_name in {"ITEM" , "BUILDING" } and tag_parts [0 ].startswith (object_name ))
89+ or object_name .endswith ("_" + tag_parts [0 ])
90+ ):
91+ context = ":" .join (tag_parts )
92+ yield FilePartInfo (token .line_number , translatable = False , context = context , tag = tag )
7893 else :
79- tag = token .text
80- tag_parts = split_tag (tag )
81-
82- if tag_parts [0 ] == "OBJECT" :
83- object_name = tag_parts [1 ]
84- yield FilePartInfo (line_number = token .line_number , translatable = False , context = context , tag = tag )
85- elif object_name and (
86- tag_parts [0 ] == object_name
87- or (object_name in {"ITEM" , "BUILDING" } and tag_parts [0 ].startswith (object_name ))
88- or object_name .endswith ("_" + tag_parts [0 ])
89- ):
90- context = ":" .join (tag_parts )
91- yield FilePartInfo (token .line_number , translatable = False , context = context , tag = tag )
92- else :
93- yield FilePartInfo (token .line_number , translatable = True , context = context , tag = tag , tag_parts = tag_parts )
94+ yield FilePartInfo (token .line_number , translatable = True , context = context , tag = tag , tag_parts = tag_parts )
9495
9596
9697def extract_translatables_from_raws (file : Iterable [str ]) -> Iterator [TranslationItem ]:
97- translation_keys : set [tuple [str , tuple [str , ...]]] = set ()
98+ translation_keys : set [tuple [str | None , tuple [str , ...]]] = set ()
9899
99100 for item in parse_raw_file (file ):
100- if item .translatable :
101- tag_parts = item .tag_parts
102- if (
103- "TILE" not in tag_parts [0 ]
104- and any (is_translatable (s ) for s in tag_parts [1 :])
105- and (item .context , tuple (tag_parts )) not in translation_keys # Don't add duplicate items to translate
106- ):
107- if not is_translatable (tag_parts [- 1 ]):
108- last = last_suitable (tag_parts , is_translatable )
109- tag_parts = tag_parts [:last ]
110- tag_parts .append ("" ) # Add an empty element to the tag to mark the tag as not completed
111- translation_keys .add ((item .context , tuple (tag_parts )))
112- yield TranslationItem (context = item .context , text = join_tag (tag_parts ), line_number = item .line_number )
101+ if not item .translatable :
102+ continue
103+
104+ tag_parts = item .tag_parts
105+ if (
106+ "TILE" not in tag_parts [0 ]
107+ and any (is_translatable (s ) for s in tag_parts [1 :])
108+ and (item .context , tuple (tag_parts )) not in translation_keys # Don't add duplicate items to translate
109+ ):
110+ if not is_translatable (tag_parts [- 1 ]):
111+ last = last_suitable (tag_parts , is_translatable )
112+ tag_parts = list (tag_parts [:last ])
113+ tag_parts .append ("" ) # Add an empty element to the tag to mark the tag as not completed
114+ translation_keys .add ((item .context , tuple (tag_parts )))
115+ yield TranslationItem (context = item .context , text = join_tag (tag_parts ), line_number = item .line_number )
113116
114117
115118def get_from_dict_with_context (
116119 dictionary : Mapping [tuple [str , str | None ], str ],
117120 key : str ,
118- context : str ,
121+ context : str | None ,
119122) -> str | None :
120123 if (key , context ) in dictionary :
121124 return dictionary [(key , context )]
@@ -128,7 +131,7 @@ def get_from_dict_with_context(
128131
129132def get_tag_translation (dictionary : Mapping [tuple [str , str | None ], str ], item : FilePartInfo ) -> str :
130133 tag = item .tag
131- tag_parts = item .tag_parts
134+ tag_parts = list ( item .tag_parts )
132135 context = item .context
133136 new_tag = get_from_dict_with_context (dictionary , tag , context )
134137
@@ -160,13 +163,13 @@ def translate_raw_file(file: Iterable[str], dictionary: Mapping[tuple[str, str |
160163 modified_line_parts = []
161164 prev_line_number = item .line_number
162165
163- if item .text is not None :
166+ if item .text :
164167 modified_line_parts .append (item .text )
165- elif not item .translatable or not any (is_translatable (s ) for s in item .tag_parts [1 :]):
166- modified_line_parts .append (item .tag )
167- else :
168+ elif item .translatable and any (is_translatable (s ) for s in item .tag_parts [1 :]):
168169 translation = get_tag_translation (dictionary , item )
169170 modified_line_parts .append (translation )
171+ else :
172+ modified_line_parts .append (item .tag )
170173
171174 if modified_line_parts :
172175 yield "" .join (modified_line_parts )
0 commit comments