From b9d5fd37f29366d99afb6bcd4ce180e915475988 Mon Sep 17 00:00:00 2001 From: Ishwarya Date: Thu, 14 Aug 2025 13:38:20 +0530 Subject: [PATCH] Added pre-commit for translations --- .pre-commit-hooks.yaml | 7 +++ pyproject.toml | 1 + test_utils/pre_commit/add_translations.py | 57 +++++++++++++++++++++++ 3 files changed, 65 insertions(+) create mode 100644 test_utils/pre_commit/add_translations.py diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 8a044c6..4b8ce6e 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -55,4 +55,11 @@ description: "Track overrides" entry: track_overrides language: python + require_serial: true + +- id: add_translations + name: add_translations + description: "Get untranslated strings in app and translate them to the language specified" + entry: add_translations + language: python require_serial: true \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 8b156dc..21951c9 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,7 @@ validate_copyright = "test_utils.pre_commit.validate_copyright:main" validate_customizations = "test_utils.pre_commit.validate_customizations:main" validate_javascript_dependencies = "test_utils.pre_commit.validate_javascript_dependencies:main" validate_python_dependencies = "test_utils.pre_commit.validate_python_dependencies:main" +add_translations = "test_utils.pre_commit.add_translations:main" [build-system] requires = ["poetry-core>=2.0.0,<3.0.0"] diff --git a/test_utils/pre_commit/add_translations.py b/test_utils/pre_commit/add_translations.py new file mode 100644 index 0000000..953356b --- /dev/null +++ b/test_utils/pre_commit/add_translations.py @@ -0,0 +1,57 @@ +import argparse +import sys +from collections.abc import Sequence + + +def is_frappe_environment(): + """ + Check if the current environment is a Frappe environment. + + Returns: + bool: True if Frappe is available, False otherwise + """ + try: + import frappe + + return True + except ImportError: + return False + + +def add_translations(lang, app): + try: + from frappe.translate import get_untranslated, update_translations + + untranslated_file = "untranslated_strings" + translated_file = "translated_strings" + get_untranslated(lang=lang, untranslated_file=untranslated_file, app=app) + update_translations( + lang=lang, + untranslated_file=untranslated_file, + translated_file=translated_file, + app=app, + ) + print(f"Successfully processed translations for language '{lang}' and app '{app}'") + except Exception as e: + print(f"An error occurred while translating for lang '{lang}' and app '{app}': {e}") + sys.exit(1) + + +def main(argv: Sequence[str] = None): + parser = argparse.ArgumentParser() + parser.add_argument("filenames", nargs="*") + parser.add_argument("--lang", action="append", help="Language to translate strings") + parser.add_argument( + "--app", action="append", help="App to get untranslated string and translate them" + ) + args = parser.parse_args(argv) + + lang = args.lang[0] + app = args.app[0] + + if not is_frappe_environment(): + print("Error: Frappe environment not detected.") + print("This script must be run from within a Frappe bench environment.") + sys.exit(1) + + add_translations(lang, app)