|
| 1 | +"""Download translated backend strings from GP and save as locale JSON files. |
| 2 | +
|
| 3 | +Downloads from the "langflow-backend" GP bundle (GP_BACKEND_BUNDLE env var) |
| 4 | +into src/backend/base/langflow/locales/ — separate from the frontend locales. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + python download_backend_translations.py |
| 8 | + python download_backend_translations.py --output path/to/locales/ |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import argparse |
| 14 | +import json |
| 15 | +import os |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import requests |
| 19 | +from gp_client import BASE_URL, GP_INSTANCE, TARGET_LANGS, get_headers |
| 20 | + |
| 21 | +DEFAULT_OUTPUT = Path(__file__).parent.parent.parent / "src/backend/base/langflow/locales" |
| 22 | +GP_BACKEND_BUNDLE = os.getenv("GP_BACKEND_BUNDLE", "langflow-backend") |
| 23 | +REQUEST_TIMEOUT = 30 |
| 24 | + |
| 25 | + |
| 26 | +def get_backend_strings(lang: str) -> dict: |
| 27 | + url = f"{BASE_URL}/{GP_INSTANCE}/v2/bundles/{GP_BACKEND_BUNDLE}/{lang}" |
| 28 | + response = requests.get( |
| 29 | + url, |
| 30 | + headers=get_headers(url, "GET"), |
| 31 | + verify=False, # noqa: S501 |
| 32 | + timeout=REQUEST_TIMEOUT, |
| 33 | + ) |
| 34 | + response.raise_for_status() |
| 35 | + return response.json() |
| 36 | + |
| 37 | + |
| 38 | +def main() -> None: |
| 39 | + parser = argparse.ArgumentParser(description="Download backend translations from GP") |
| 40 | + parser.add_argument("--output", default=str(DEFAULT_OUTPUT), help="Directory to save translated JSON files") |
| 41 | + args = parser.parse_args() |
| 42 | + |
| 43 | + output_dir = Path(args.output) |
| 44 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 45 | + |
| 46 | + for lang in TARGET_LANGS: |
| 47 | + print(f"Downloading '{lang}' translations...") |
| 48 | + try: |
| 49 | + result = get_backend_strings(lang) |
| 50 | + |
| 51 | + strings = { |
| 52 | + key: entry.get("value", "") if isinstance(entry, dict) else entry |
| 53 | + for key, entry in result.get("resourceStrings", {}).items() |
| 54 | + } |
| 55 | + |
| 56 | + if not strings: |
| 57 | + print(f" No strings yet for '{lang}' (translation may still be in progress)") |
| 58 | + continue |
| 59 | + |
| 60 | + output_file = output_dir / f"{lang}.json" |
| 61 | + output_file.write_text( |
| 62 | + json.dumps(strings, ensure_ascii=False, indent=2), |
| 63 | + encoding="utf-8", |
| 64 | + ) |
| 65 | + print(f" Saved {len(strings)} strings to {output_file}") |
| 66 | + |
| 67 | + except Exception as e: # noqa: BLE001 |
| 68 | + print(f" Error downloading '{lang}': {e}") |
| 69 | + |
| 70 | + print("\nDone.") |
| 71 | + |
| 72 | + |
| 73 | +if __name__ == "__main__": |
| 74 | + main() |
0 commit comments