diff --git a/devutils/i18n_generate.py b/devutils/i18n_generate.py index b8fa6c71..928fe02c 100644 --- a/devutils/i18n_generate.py +++ b/devutils/i18n_generate.py @@ -7,6 +7,7 @@ String extraction from Helium patches for translation. import subprocess import json +import re from pathlib import Path from third_party import unidiff @@ -17,6 +18,12 @@ PLATFORMS = ("windows", "macos", "linux") REPO_URL = "https://github.com/imputnet/helium-{platform}.git" +def get_xml_attr(text, attr): + """Extract an XML attribute from an opening tag.""" + match = re.search(rf'{attr}="([^"]*)"', text) + return match.group(1) if match else None + + def prep_platform_repos(platforms_dir): """Clone and update platform repos into the given directory.""" platforms_dir.mkdir(parents=True, exist_ok=True) @@ -71,7 +78,7 @@ def extract_strings_from_hunk(hunk): - broken chunks of untouched, pre-existing units - units that were added in a different patch (avoiding duplication) """ - name, message, desc = None, '', None + name, message, desc, meaning = None, '', None, None meta_acc = '' had_any_additive = False @@ -87,15 +94,16 @@ def extract_strings_from_hunk(hunk): meta_acc += line elif line.startswith(''): if name and message and had_any_additive: - yield name, desc, message - name, message, desc = None, '', None + yield name, desc, meaning, message + name, message, desc, meaning = None, '', None, None had_any_additive = False elif name: message += line if meta_acc and line.endswith('>'): - name = meta_acc.split('name="')[1].split('"')[0] - desc = meta_acc.split('desc="')[1].split('"')[0] + name = get_xml_attr(meta_acc, 'name') + desc = get_xml_attr(meta_acc, 'desc') + meaning = get_xml_attr(meta_acc, 'meaning') meta_acc = '' had_any_additive |= bool(name) and is_additive @@ -105,16 +113,19 @@ def extract_strings(repo_root, platforms_dir): """Generate strings to be translated for all grit strings in patches.""" for patch in get_relevant_patches(repo_root, platforms_dir): for hunk in patch: - for name, desc, message in extract_strings_from_hunk(hunk): + for name, desc, meaning, message in extract_strings_from_hunk(hunk): context = namesub.replace_text(desc)[0] message = namesub.replace_text(message)[0] - yield { + entry = { 'name': name, 'source': patch.path, 'context': context, - 'message': message, } + if meaning: + entry['meaning'] = namesub.replace_text(meaning)[0] + entry['message'] = message + yield entry def run(args, repo_root): diff --git a/i18n/source.gen.json b/i18n/source.gen.json index 2fafb99b..b48cfcbf 100644 --- a/i18n/source.gen.json +++ b/i18n/source.gen.json @@ -633,6 +633,7 @@ "name": "IDS_SETTINGS_SHORTCUTS_ADD_DIALOG_TITLE", "source": "chrome/app/settings_strings.grdp", "context": "Title of the dialog for recording a new browser keyboard shortcut.", + "meaning": "Dialog title for adding a keyboard shortcut in Settings.", "message": "Add shortcut" }, { diff --git a/patches/helium/settings/custom-keyboard-shortcuts-page.patch b/patches/helium/settings/custom-keyboard-shortcuts-page.patch index 081d483a..017a6138 100644 --- a/patches/helium/settings/custom-keyboard-shortcuts-page.patch +++ b/patches/helium/settings/custom-keyboard-shortcuts-page.patch @@ -16,7 +16,7 @@ + + Reset all to defaults + -+ ++ + Add shortcut + + diff --git a/utils/i18n_apply.py b/utils/i18n_apply.py index 24514617..5488be48 100755 --- a/utils/i18n_apply.py +++ b/utils/i18n_apply.py @@ -25,9 +25,13 @@ SOURCE_PATH = I18N_DIR / 'source.gen.json' TRANSLATIONS_DIR = I18N_DIR / 'translations' -def get_id(name, context, text): +def get_id(name, context, text, meaning): """Compute the fingerprint ID for a GRD message element.""" - message = xml.fromstring(f'{text}') + message = xml.fromstring(f'{text}') + message.set('name', name) + message.set('desc', context) + if meaning: + message.set('meaning', meaning) return namesub.compute_fp(message) @@ -142,7 +146,7 @@ def merge_into_xtb(xtb_path, source_entries, trans_by_key): trans = trans_by_key.get(key) if not trans: continue - msg_id = get_id(src['name'], src['context'], src['message']) + msg_id = get_id(src['name'], src['context'], src['message'], src.get('meaning')) if msg_id in seen_ids: continue seen_ids.add(msg_id)