Skip to content

Commit 932b076

Browse files
committed
fix: add constant strings that are dynamic to localiser dict
1 parent ed98c01 commit 932b076

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

crowdin/generate_desktop_strings.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import json
3+
import re
34
from typing import Dict, List
45
import xml.etree.ElementTree as ET
56
import sys
@@ -27,6 +28,7 @@
2728
# Add more mappings as needed
2829
}
2930

31+
3032
# Parse command-line arguments
3133
parser = argparse.ArgumentParser(description='Convert a XLIFF translation files to JSON.')
3234
parser.add_argument('--qa_build', help='Set to true to output only English strings (only used for QA)', action=argparse.BooleanOptionalAction)
@@ -41,6 +43,13 @@
4143
IS_QA_BUILD = args.qa_build
4244

4345

46+
def matches_braced_pattern(string):
47+
return re.search(r"\{(.+?)\}", string) is not None
48+
49+
def snake_to_camel(snake_str: str) -> str:
50+
parts = snake_str.split('_')
51+
return parts[0].lower() + ''.join(word.capitalize() for word in parts[1:])
52+
4453
def parse_xliff(file_path):
4554
tree = ET.parse(file_path)
4655
root = tree.getroot()
@@ -98,6 +107,12 @@ def convert_xliff_to_json(input_file, output_dir, locale, locale_two_letter_code
98107
for resname, target in sorted_translations:
99108
converted_translations[resname] = generate_icu_pattern(target, glossary_dict)
100109

110+
111+
for resname in glossary_dict:
112+
target = glossary_dict[resname]
113+
if(matches_braced_pattern(target)):
114+
converted_translations[snake_to_camel(resname)] = generate_icu_pattern(target, glossary_dict)
115+
101116
# Generate output files
102117
output_locale = LOCALE_PATH_MAPPING.get(locale, LOCALE_PATH_MAPPING.get(locale_two_letter_code, locale_two_letter_code))
103118
locale_output_dir = os.path.join(output_dir, output_locale)
@@ -126,8 +141,11 @@ def convert_non_translatable_strings_to_type_script(input_file: str, output_path
126141
file.write('export enum LOCALE_DEFAULTS {\n')
127142
for key in glossary_dict:
128143
text = glossary_dict[key]
129-
cleaned_text = clean_string(text, False, glossary_dict, {})
130-
file.write(f" {key} = '{cleaned_text}',\n")
144+
# constant strings that have braces in them are not constants. We add them to the localised strings output
145+
# for easy replacing of their variables
146+
if(not matches_braced_pattern(text)):
147+
cleaned_text = clean_string(text, False, glossary_dict, {})
148+
file.write(f" {key} = '{cleaned_text}',\n")
131149

132150
file.write('}\n')
133151
file.write('\n')

0 commit comments

Comments
 (0)