-
-
Notifications
You must be signed in to change notification settings - Fork 405
New helper script for translations #2948
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: develop
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,119 @@ | ||||||
| """Apply translations from CSV back to pyRevit bundle YAML files. | ||||||
|
|
||||||
| This script reads a CSV file containing translations (generated by | ||||||
| extract_translations.py) and applies them to the corresponding bundle.yaml | ||||||
| files, updating or creating multilingual field dictionaries. | ||||||
|
|
||||||
| Configuration: | ||||||
| TRANSLATION_CSV: Path to the CSV file containing translations | ||||||
| LANGUAGE_KEY: Translation language key to apply (e.g., 'chinese_s') | ||||||
| SOURCE_LANG: Source language key (default: 'en_us') | ||||||
| """ | ||||||
| import csv | ||||||
| from pathlib import Path | ||||||
| from ruamel.yaml import YAML # pip install ruamel.yaml | ||||||
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| # -------- CONFIG -------- | ||||||
| TRANSLATION_CSV = r"C:\temp\translations.csv" # same path as in other script | ||||||
| LANGUAGE_KEY = "chinese_s" # same as in other script | ||||||
| SOURCE_LANG = "en_us" # same as in other script | ||||||
| # ------------------------ | ||||||
|
|
||||||
| yaml = YAML() | ||||||
|
|
||||||
|
|
||||||
| def build_lookup(csv_path): | ||||||
| """Build dictionary: {yaml_file: {key_type: {"en_us":..., "translation":...}}}""" | ||||||
| lookup = {} | ||||||
|
|
||||||
| with open(csv_path, encoding="utf-8") as f: | ||||||
|
||||||
| with open(csv_path, encoding="utf-8") as f: | |
| with open(csv_path, encoding="utf-8", newline="") as f: |
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """Extract translation strings from pyRevit bundle YAML files. | ||
|
|
||
| This script scans bundle.yaml files for title and tooltip fields, | ||
| extracting English source text and any existing translations to a CSV file | ||
| for easier translation workflow. | ||
|
|
||
| Configuration: | ||
| BASE_DIR: Root directory containing bundle YAML files | ||
| OUTPUT_CSV: Path where the CSV file will be written | ||
| LANGUAGE_KEY: Translation language key (e.g., 'chinese_s') | ||
| SOURCE_LANG: Source language key (default: 'en_us') | ||
| """ | ||
| import os | ||
| from pathlib import Path | ||
| from ruamel.yaml import YAML # pip install ruamel.yaml | ||
|
||
| import csv | ||
|
|
||
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # -------- CONFIG -------- | ||
| BASE_DIR = r"C:\Program Files\pyRevit-Master\extensions\pyRevitTools.extension" # adjust for custom installation | ||
| OUTPUT_CSV = r"C:\temp\translations.csv" # same path as in other script | ||
| LANGUAGE_KEY = "chinese_s" # translation key to extract/merge | ||
| SOURCE_LANG = "en_us" # main source language | ||
| # ------------------------ | ||
|
|
||
| yaml = YAML() | ||
|
|
||
|
|
||
| def find_yaml_files(base_dir): | ||
| for root, _, files in os.walk(base_dir): | ||
| for f in files: | ||
| if f.endswith(".yaml"): | ||
| yield Path(root) / f | ||
|
|
||
|
|
||
| def extract_field(path, field_name, value, results): | ||
| """Extracts English + existing translated value from dict or scalar.""" | ||
| # CASE 1: multilingual dict | ||
| if isinstance(value, dict): | ||
| # English (preferred) | ||
| en = value.get(SOURCE_LANG) | ||
|
|
||
| # fallback to first language if no en_us exists | ||
| if not en and len(value): | ||
| first_key = next(iter(value.keys())) | ||
| en = value[first_key] | ||
|
|
||
| # Existing translation to preserve | ||
| tr = value.get(LANGUAGE_KEY, "") | ||
|
|
||
| if en: | ||
| results.append([path, field_name, en, tr]) | ||
| return | ||
|
|
||
| # CASE 2: scalar string | ||
| if isinstance(value, str): | ||
| print( | ||
| f"[Scalar detected] File: {path} | Field: {field_name} | Value: '{value}'" | ||
| ) | ||
|
Comment on lines
+56
to
+58
|
||
| results.append([path, field_name, value, ""]) | ||
| return | ||
|
|
||
|
|
||
| def extract_values(data, path, results): | ||
| """Recursively walk through structure and extract fields.""" | ||
| if not isinstance(data, dict): | ||
| return | ||
|
|
||
| for field_name in ("title", "tooltip"): | ||
| if field_name in data: | ||
| extract_field(path, field_name, data[field_name], results) | ||
|
|
||
| # recurse into children | ||
| for k, v in data.items(): | ||
| child_path = f"{path}/{k}" | ||
| extract_values(v, child_path, results) | ||
|
|
||
|
|
||
| def main(): | ||
| results = [] | ||
|
|
||
| for yaml_file in find_yaml_files(BASE_DIR): | ||
| try: | ||
| with open(yaml_file, "r", encoding="utf-8") as f: | ||
| data = yaml.load(f) | ||
|
|
||
| extract_values(data, str(yaml_file), results) | ||
|
|
||
| except Exception as e: | ||
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| print(f"ERROR reading {yaml_file}: {e}") | ||
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # write CSV | ||
| with open(OUTPUT_CSV, "w", newline="", encoding="utf-8") as f: | ||
| writer = csv.writer(f) | ||
| writer.writerow(["yaml_file", "key_type", SOURCE_LANG, LANGUAGE_KEY]) | ||
Wurschdhaud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for row in results: | ||
| writer.writerow(row) | ||
|
|
||
| print(f"\nExtracted {len(results)} records to {OUTPUT_CSV}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Uh oh!
There was an error while loading. Please reload this page.