|
1 | 1 | import os
|
2 | 2 | import json
|
| 3 | +import re |
3 | 4 | from typing import Dict, List
|
4 | 5 | import xml.etree.ElementTree as ET
|
5 | 6 | import sys
|
|
27 | 28 | # Add more mappings as needed
|
28 | 29 | }
|
29 | 30 |
|
| 31 | + |
30 | 32 | # Parse command-line arguments
|
31 | 33 | parser = argparse.ArgumentParser(description='Convert a XLIFF translation files to JSON.')
|
32 | 34 | parser.add_argument('--qa_build', help='Set to true to output only English strings (only used for QA)', action=argparse.BooleanOptionalAction)
|
|
41 | 43 | IS_QA_BUILD = args.qa_build
|
42 | 44 |
|
43 | 45 |
|
| 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] + ''.join(word.capitalize() for word in parts[1:]) |
| 52 | + |
44 | 53 | def parse_xliff(file_path):
|
45 | 54 | tree = ET.parse(file_path)
|
46 | 55 | root = tree.getroot()
|
@@ -98,6 +107,12 @@ def convert_xliff_to_json(input_file, output_dir, locale, locale_two_letter_code
|
98 | 107 | for resname, target in sorted_translations:
|
99 | 108 | converted_translations[resname] = generate_icu_pattern(target, glossary_dict)
|
100 | 109 |
|
| 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 | + |
101 | 116 | # Generate output files
|
102 | 117 | output_locale = LOCALE_PATH_MAPPING.get(locale, LOCALE_PATH_MAPPING.get(locale_two_letter_code, locale_two_letter_code))
|
103 | 118 | 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
|
126 | 141 | file.write('export enum LOCALE_DEFAULTS {\n')
|
127 | 142 | for key in glossary_dict:
|
128 | 143 | 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") |
131 | 149 |
|
132 | 150 | file.write('}\n')
|
133 | 151 | file.write('\n')
|
|
0 commit comments