Created
March 11, 2026 05:22
-
-
Save flodolo/8f625e2acef37c043a3c6e6944c4da5d to your computer and use it in GitHub Desktop.
Fix DOH labels in fx150
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| """ | |
| Remove trailing punctuation from specific Fluent message attributes in | |
| browser/browser/preferences/preferences.ftl across all locales. | |
| - preferences-doh-radio-default/custom/off: fix .description | |
| - preferences-doh-fallback-label: fix .label | |
| """ | |
| import os | |
| import re | |
| import unicodedata | |
| ROOT = os.path.dirname(os.path.abspath(__file__)) | |
| # Map each message ID to the attribute that should have trailing punctuation removed | |
| MESSAGE_ATTR = { | |
| "preferences-doh-radio-default": "description", | |
| "preferences-doh-radio-custom": "description", | |
| "preferences-doh-radio-off": "description", | |
| "preferences-doh-fallback-label": "label", | |
| } | |
| ATTR_RE = re.compile(r"^(\s+\.(label|description)\s*=\s*)(.+)$") | |
| def strip_trailing_punctuation(text): | |
| """Strip all trailing Unicode punctuation characters.""" | |
| v = text.rstrip() | |
| while v and unicodedata.category(v[-1]).startswith("P"): | |
| v = v[:-1] | |
| return v | |
| def fix_file(filepath): | |
| with open(filepath, encoding="utf-8") as f: | |
| lines = f.readlines() | |
| new_lines = [] | |
| current_target_attr = None # attribute name to fix for the current message | |
| attr_consumed = False | |
| changed = False | |
| for line in lines: | |
| stripped = line.rstrip("\n") | |
| # Check if this line starts one of the target messages | |
| matched_msg = next( | |
| (msg for msg in MESSAGE_ATTR if stripped == f"{msg} =" or stripped.startswith(f"{msg} =")), | |
| None, | |
| ) | |
| if matched_msg: | |
| current_target_attr = MESSAGE_ATTR[matched_msg] | |
| attr_consumed = False | |
| new_lines.append(line) | |
| continue | |
| if current_target_attr: | |
| # A non-indented, non-empty line means we've left the message block | |
| if stripped and not stripped[0] in (" ", "\t"): | |
| current_target_attr = None | |
| attr_consumed = False | |
| new_lines.append(line) | |
| continue | |
| # Check for the target attribute (only the first occurrence per message) | |
| if not attr_consumed: | |
| m = ATTR_RE.match(stripped) | |
| if m and m.group(2) == current_target_attr: | |
| prefix = m.group(1) | |
| value = m.group(3) | |
| fixed = strip_trailing_punctuation(value) | |
| if fixed != value.rstrip(): | |
| trailing = value[len(value.rstrip()):] | |
| new_line = line.replace(prefix + value, prefix + fixed + trailing, 1) | |
| print(f" {os.path.relpath(filepath, ROOT)}: {value!r} -> {fixed!r}") | |
| line = new_line | |
| changed = True | |
| attr_consumed = True | |
| new_lines.append(line) | |
| if changed: | |
| with open(filepath, "w", encoding="utf-8") as f: | |
| f.writelines(new_lines) | |
| return changed | |
| def main(): | |
| total_changed = 0 | |
| for entry in sorted(os.listdir(ROOT)): | |
| if entry.startswith("."): | |
| continue | |
| locale_dir = os.path.join(ROOT, entry) | |
| if not os.path.isdir(locale_dir): | |
| continue | |
| ftl_path = os.path.join( | |
| locale_dir, "browser", "browser", "preferences", "preferences.ftl" | |
| ) | |
| if not os.path.isfile(ftl_path): | |
| continue | |
| if fix_file(ftl_path): | |
| total_changed += 1 | |
| print(f"\nDone. Modified files: {total_changed}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment