-
-
Notifications
You must be signed in to change notification settings - Fork 408
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
Changes from 1 commit
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,108 @@ | ||||||
| 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" | ||||||
Wurschdhaud marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| LANGUAGE_KEY = "chinese_s" | ||||||
| SOURCE_LANG = "en_us" | ||||||
| # ------------------------ | ||||||
|
|
||||||
| 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.
Outdated
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,90 @@ | ||
| 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" | ||
| OUTPUT_CSV = r"C:\temp\translations.csv" | ||
|
||
| 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.