Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
57 changes: 57 additions & 0 deletions test_utils/pre_commit/add_translations.py
Original file line number Diff line number Diff line change
@@ -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)
Loading