Skip to content

Instantly share code, notes, and snippets.

@alexrintt
Created June 13, 2023 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexrintt/714a1bedafa89fae5150316693d1056a to your computer and use it in GitHub Desktop.
Save alexrintt/714a1bedafa89fae5150316693d1056a to your computer and use it in GitHub Desktop.
import os
CONFIG_FILE_NAME = 'l10n.yaml'
def find_config_file_path(top: str = os.path.dirname(__file__)) -> str | None:
for dirpath, dirnames, filenames in os.walk(topdown=True, top=top):
for filename in filenames:
if filename == CONFIG_FILE_NAME:
return os.path.join(dirpath, filename)
parent = os.path.dirname(top)
if os.path.exists(parent):
return find_config_file_path(parent)
return None
config_file_path = find_config_file_path()
import yaml
with open(config_file_path, 'r', encoding='utf8') as config_file:
config = yaml.safe_load(config_file)
template_arb_file_name = config['template-arb-file']
arb_dir_path = os.path.realpath(os.path.join(os.path.dirname(config_file_path), config['arb-dir']))
arb_template_file_path = os.path.join(arb_dir_path, template_arb_file_name)
import json
from typing import Any
from gtranslate import translate
from hashlib import md5
def read_file_as_json(file_path: str) -> dict[str, Any]:
with open(file_path, 'r', encoding='utf8') as file:
return json.loads(file.read())
def write_file_as_json(file_path: str, data: dict[str, Any]) -> None:
with open(file_path, 'w', encoding='utf8') as file:
json.dump(data, file, indent=2, ensure_ascii=False)
base_i18n_data = read_file_as_json(arb_template_file_path)
def get_nested_value(nested_dict: dict[str, Any], *keys) -> Any:
if len(keys) == 1:
return nested_dict.get(keys[0])
else:
key = keys[0]
if key in nested_dict:
return get_nested_value(nested_dict[key], *keys[1:])
else:
return None
def set_nested_value(nested_dict, value, *keys):
key = keys[0]
if len(keys) == 1:
nested_dict[key] = value
else:
if key not in nested_dict:
nested_dict[key] = {}
set_nested_value(nested_dict[key], value, *keys[1:])
for dirpath, dirnames, filenames in os.walk(arb_dir_path):
cached = 0
noncached = 0
for filename in filenames:
if filename == template_arb_file_name:
continue
target_lang_file_path = os.path.join(dirpath, filename)
target_lang_file_data = read_file_as_json(target_lang_file_path)
target_locale = target_lang_file_data['@@locale']
for k, v in base_i18n_data.items():
def is_reserved_key() -> bool:
RESERVED_PREFIXES = '_', '@'
for prefix in RESERVED_PREFIXES:
if k.startswith(prefix):
# Not a translation key
# pass
return True
return False
if is_reserved_key():
continue
print(f'Key: {k}')
print(f'Lang: {target_locale}')
print(f'Original: {v}')
def is_same_source_value():
has_translation = k in target_lang_file_data and target_lang_file_data[k] is not None
translation_source_value = get_nested_value(target_lang_file_data, f'@{k}', 'info', 'source')
same_source_value = has_translation and translation_source_value == v
return same_source_value
try:
if is_same_source_value():
cached += 1
print(f'Translated (cached): {translated}')
else:
noncached += 1
translated = translate(v, translate_to=target_locale)
print(f'Translated: {translated}')
target_lang_file_data[k] = translated
set_nested_value(target_lang_file_data, v, f'@{k}', 'info', 'source')
write_file_as_json(target_lang_file_path, target_lang_file_data)
except:
print('Translated: [Translation not available, skipping...]')
continue
finally:
print('-' * 30)
print('*' * 30)
print(f'Finished translating to {target_locale}')
print('*' * 30)
print('=' * 30)
print(f'Loaded {noncached} non-cached results')
print(f'Loaded {cached} cached results')
print('=' * 30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment