Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
makemessages: Treat frontend translation keys and values as separate.
Anders Kaseorg0c8b66117b3c
diff --git a/tools/check-capitalization b/tools/check-capitalizationindex 789759ae669..0ab0630dcae 100755--- a/tools/check-capitalization+++ b/tools/check-capitalization@@ -38,7 +38,7 @@ if __name__ == "__main__":with open("locale/en/translations.json") as f:data = json.load(f)- frontend = check_capitalization(list(data.keys()))+ frontend = check_capitalization(list(data.values()))frontend_errors, frontend_ignored, banned_errors_front = frontendwith open("locale/en/LC_MESSAGES/django.po") as f:diff --git a/tools/check-frontend-i18n b/tools/check-frontend-i18nindex ce61c20210b..900687123fc 100755--- a/tools/check-frontend-i18n+++ b/tools/check-frontend-i18n@@ -34,7 +34,7 @@ if __name__ == "__main__":with open("locale/en/translations.json") as f:data = json.load(f)- found = find_handlebars(list(data.keys()))+ found = find_handlebars(list(data.values()))if not found:sys.exit(0)diff --git a/tools/i18n/update-for-legacy-translations b/tools/i18n/update-for-legacy-translationsindex be406fff1fc..4784096cd2a 100755--- a/tools/i18n/update-for-legacy-translations+++ b/tools/i18n/update-for-legacy-translations@@ -208,6 +208,9 @@ def get_translations(path: str) -> dict[str, str]:return translations+en_translations = get_translations(get_json_filename("en"))++def update_for_legacy_stream_translations(current: dict[str, str], legacy: dict[str, str], path: str) -> None:@@ -218,9 +221,10 @@ def update_for_legacy_stream_translations(# not currently translated (e.g. an empty string), then use# the legacy translated string (which might be an empty string).if (- line in LEGACY_STRINGS_MAP+ line in en_translations+ and en_translations[line] in LEGACY_STRINGS_MAPand translation == ""- and (legacy_string := LEGACY_STRINGS_MAP[line]) in legacy+ and (legacy_string := LEGACY_STRINGS_MAP[en_translations[line]]) in legacy):updated_translations[line] = legacy[legacy_string]number_of_updates += 1diff --git a/web/babel.config.js b/web/babel.config.jsindex f21f917f823..0b5ba1456dd 100644--- a/web/babel.config.js+++ b/web/babel.config.js@@ -7,6 +7,7 @@ import _ from "lodash";/** @type {Parameters<typeof import("babel-plugin-formatjs").default>[1]} */const formatJsOptions = {additionalFunctionNames: ["$t", "$t_html"],+ // Matches formatjs_id_pattern in makemessages.pyidInterpolationPattern: "[content]",};diff --git a/zerver/management/commands/makemessages.py b/zerver/management/commands/makemessages.pyindex 771de059918..28bda715168 100644--- a/zerver/management/commands/makemessages.py+++ b/zerver/management/commands/makemessages.py@@ -102,6 +102,10 @@ def apply_i18n_trimmed_policy(src: str, env: Environment) -> str:return trans_block_re.sub(r"\1 trimmed \2", src)+# Matches idInterpolationPattern in babel.config.js+formatjs_id_pattern = "[content]"++class Command(makemessages.Command):xgettext_options = makemessages.Command.xgettext_optionsfor func, tag in tags:@@ -197,18 +201,18 @@ class Command(makemessages.Command):template.templatize = old_templatizetemplate.constant_re = old_constant_re- def extract_strings(self, data: str) -> list[str]:- translation_strings: list[str] = []+ def extract_strings(self, data: str) -> dict[str, str]:+ translation_strings: dict[str, str] = {}for regex in frontend_compiled_regexes:for match in regex.findall(data):match = match.strip()match = " ".join(line.strip() for line in match.splitlines())- translation_strings.append(match)+ translation_strings[match] = matchreturn translation_strings- def get_translation_strings(self) -> list[str]:- translation_strings: list[str] = []+ def get_translation_strings(self) -> dict[str, str]:+ translation_strings: dict[str, str] = {}dirname = self.get_template_dir()for dirpath, dirnames, filenames in os.walk(dirname):@@ -217,7 +221,9 @@ class Command(makemessages.Command):continuewith open(os.path.join(dirpath, filename)) as reader:data = reader.read()- translation_strings.extend(self.extract_strings(data))+ for k, v in self.extract_strings(data).items():+ assert translation_strings.get(k, v) == v+ translation_strings[k] = vextracted = subprocess.check_output([@@ -225,14 +231,18 @@ class Command(makemessages.Command):"extract","--additional-function-names=$t,$t_html","--format=simple",+ f"--id-interpolation-pattern={formatjs_id_pattern}","--ignore=**/*.d.ts",+ "--throws","web/src/**/*.js","web/src/**/*.ts",])- translation_strings.extend(orjson.loads(extracted).values())+ for k, v in orjson.loads(extracted).items():+ assert translation_strings.get(k, v) == v+ translation_strings[k] = v- return list(set(translation_strings))+ return translation_stringsdef get_template_dir(self) -> str:return self.frontend_source@@ -271,23 +281,18 @@ class Command(makemessages.Command):yield os.path.join(path, self.get_namespace())def get_new_strings(- self, old_strings: Mapping[str, str], translation_strings: list[str], locale: str+ self, old_strings: Mapping[str, str], translation_strings: Mapping[str, str], locale: str) -> dict[str, str]:"""Missing strings are removed, new strings are added and already- translated strings are not touched.+ translated non-English strings are not touched."""- new_strings = {} # Dict[str, str]- for k in translation_strings:- if locale == "en":- # For English language, translation is equal to the key.- new_strings[k] = old_strings.get(k, k)- else:- new_strings[k] = old_strings.get(k, "")-- return new_strings+ if locale == "en":+ return dict(translation_strings)+ else:+ return {k: old_strings.get(k, "") for k in translation_strings}- def write_translation_strings(self, translation_strings: list[str]) -> None:+ def write_translation_strings(self, translation_strings: Mapping[str, str]) -> None:for locale, output_path in zip(self.get_locales(), self.get_output_paths(), strict=False):self.stdout.write(f"[frontend] processing locale {locale}")try:diff --git a/zerver/tests/test_i18n.py b/zerver/tests/test_i18n.pyindex 2f78ada52a5..9e07aeff0b0 100644--- a/zerver/tests/test_i18n.py+++ b/zerver/tests/test_i18n.py@@ -220,5 +220,4 @@ class FrontendRegexTestCase(ZulipTestCase):for input_text, expected in data:result = command.extract_strings(input_text)- self.assert_length(result, 1)- self.assertEqual(result[0], expected)+ self.assertEqual(result, {expected: expected})