Repository navigation
Generate and load JavaScript translation catalogs #338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
243f079
bd78daa
6b10012
fb80b82
3b83c19
4269a8e
b85739d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ coverage.xml | |
|
|
||
| # Translations | ||
| *.mo | ||
| /python_docs_theme/locale/**/*.js | ||
| *.pot | ||
|
|
||
| # Django stuff: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,10 +4,12 @@ | |
|
|
||
| import argparse | ||
| import ast | ||
| import json | ||
| import subprocess | ||
| import tomllib | ||
| from pathlib import Path | ||
|
|
||
| from babel.messages.pofile import read_po | ||
| from hatchling.builders.hooks.plugin.interface import BuildHookInterface | ||
|
|
||
| # Global variables used by pybabel below (paths relative to PROJECT_DIR) | ||
|
|
@@ -107,6 +109,49 @@ def compile_catalogs(locale: str) -> None: | |
| cmd.extend(["-l", locale]) | ||
| subprocess.run(cmd, cwd=PROJECT_DIR, check=True) | ||
|
|
||
| locale_dirs = PROJECT_DIR / LOCALES_DIR | ||
| catalogs = ( | ||
| [locale_dirs / locale / "LC_MESSAGES" / f"{DOMAIN}.po"] | ||
| if locale | ||
| else sorted(locale_dirs.glob(f"*/LC_MESSAGES/{DOMAIN}.po")) | ||
| ) | ||
| for pofile in catalogs: | ||
| compile_js_catalog(pofile) | ||
|
|
||
|
|
||
| def compile_js_catalog(pofile: Path) -> None: | ||
| """Export translated JavaScript messages from the PO catalog.""" | ||
| jsfile = pofile.with_suffix(".js") | ||
| with pofile.open("rb") as f: | ||
| source = read_po(f) | ||
| if source.fuzzy: | ||
| jsfile.unlink(missing_ok=True) | ||
| return | ||
| messages = {} | ||
| for message in source: | ||
| if not any(filename.endswith(".js") for filename, _ in message.locations): | ||
| continue | ||
| if message.context is not None or message.fuzzy or not message.string: | ||
| continue | ||
| if message.pluralizable and not all(message.string): | ||
| continue | ||
| msgid = message.id[0] if message.pluralizable else message.id | ||
| messages[msgid] = message.string | ||
| serialized = json.dumps( | ||
| { | ||
| "locale": str(source.locale), | ||
| "messages": messages, | ||
| "plural_expr": f"Number({source.plural_expr})", | ||
| }, | ||
| ensure_ascii=True, | ||
| sort_keys=True, | ||
| ) | ||
| jsfile.write_text( | ||
| f"// {DOMAIN} translations for {pofile.parents[1].name}\n" | ||
| f"Documentation.addTranslations({serialized});\n", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 says this, does it matter here?
None of our three JS strings are plural, so let's merge into # The plural rule is global and set by Sphinx's own catalog
if message.pluralizable:
continue
if message.fuzzy or not message.string:
continue
messages[message.id] = message.string
serialized = json.dumps(messages, ensure_ascii=True, sort_keys=True)
jsfile.write_text(
f"// {DOMAIN} translations for {pofile.parents[1].name}\n"
f"Object.assign(Documentation.TRANSLATIONS, {serialized});\n",
encoding="utf-8",
)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right, thank you. Potentially we could be incorrectly overriding core Sphinx plural rules for JS messages. I've applied a fix while keeping support for ngettext, for potential future use. b85739d What do you think? Regarding the undefined issue in forms I've opened sphinx-doc/sphinx#14872 in Sphinx.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The most correct fix would be to bring support for plural rules per gettext domain in JS catalogs on Sphinx side, but I think the use of it is so niche that I'm not sure if it's worth ticketing now. |
||
| encoding="utf-8", | ||
| ) | ||
|
|
||
|
|
||
| class CustomBuildHook(BuildHookInterface): | ||
| def initialize(self, version: str, build_data: dict[str, object]) -> None: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.