Repo created
This commit is contained in:
parent
4af19165ec
commit
68073add76
12458 changed files with 12350765 additions and 2 deletions
114
tools/python/categories/consistency.py
Executable file
114
tools/python/categories/consistency.py
Executable file
|
|
@ -0,0 +1,114 @@
|
|||
#!/usr/bin/env python3
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import json
|
||||
from string import digits
|
||||
|
||||
CONTENT_REGEX = re.compile(r'/\*.*?\*/', re.DOTALL)
|
||||
TYPE_ENTRIES_REGEX = re.compile(r'"(.*?)"\s*=\s*"(.*?)"')
|
||||
SINGLE_REPLACE = False
|
||||
|
||||
def main(lang, data_en):
|
||||
strings_file_path = os.path.join('iphone', 'Maps', 'LocalizedStrings', f'{lang}.lproj', 'LocalizableTypes.strings')
|
||||
json_file_path = os.path.join('data', 'categories-strings', f'{lang}.json', 'localize.json')
|
||||
|
||||
with open(strings_file_path, 'r', encoding='utf-8') as f:
|
||||
content = f.read()
|
||||
|
||||
# Remove comments
|
||||
content = re.sub(CONTENT_REGEX, '', content)
|
||||
|
||||
type_entries = {key[5:]: value for key, value in re.findall(TYPE_ENTRIES_REGEX, content)}
|
||||
|
||||
with open(json_file_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
for type_name, localized_value in type_entries.items():
|
||||
key_matched = False
|
||||
for json_key in data.keys():
|
||||
json_key_split = json_key.split('|')
|
||||
for key in json_key_split:
|
||||
already_there = False
|
||||
_key_matched = False
|
||||
|
||||
if type_name == key.replace('-', '.').replace('_', '.'):
|
||||
key_matched = True
|
||||
data_split = data[json_key].split('|')
|
||||
|
||||
try:
|
||||
data_split.extend([
|
||||
value
|
||||
for category in
|
||||
[a for a in json_key_split
|
||||
if a.startswith('@')]
|
||||
for value in
|
||||
data[category].split('|')
|
||||
])
|
||||
except KeyError:
|
||||
pass
|
||||
|
||||
for value in data_split:
|
||||
if value and value[0] in digits:
|
||||
value = value[1:]
|
||||
|
||||
value = value.lower()
|
||||
localized_value_lower = localized_value.lower()
|
||||
|
||||
# Prevents adding duplicates that differ only by the word "shop"
|
||||
if value in localized_value_lower:
|
||||
already_there = True
|
||||
break
|
||||
|
||||
if localized_value_lower == value:
|
||||
_key_matched = True
|
||||
break
|
||||
|
||||
if already_there:
|
||||
break
|
||||
|
||||
if not _key_matched:
|
||||
if SINGLE_REPLACE and len(data_split) == 1:
|
||||
data[json_key] = localized_value
|
||||
print(f'Replaced "{data[json_key]}" with "{localized_value}" in "{json_key}"')
|
||||
|
||||
else:
|
||||
data[json_key] = localized_value+'|'+data[json_key]
|
||||
print(f'Appended "{localized_value}" to "{json_key}"')
|
||||
|
||||
if not key_matched:
|
||||
for json_key in data.keys():
|
||||
for key in json_key.split('|'):
|
||||
if type_name == key.replace('-', '.').replace('_', '.'):
|
||||
print(f'Created "{localized_value}" for "{json_key}"')
|
||||
data.update({json_key: localized_value})
|
||||
|
||||
res = json.dumps(data, ensure_ascii=False, separators=(",\n", ": ")
|
||||
).replace('{', '{\n').replace('}', '\n}')
|
||||
|
||||
with open(json_file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(res)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
print(f"Usage: {sys.argv[0]} [-r] <language_codes>")
|
||||
sys.exit(1)
|
||||
|
||||
if sys.argv[1] == '-r':
|
||||
SINGLE_REPLACE = True
|
||||
del sys.argv[1]
|
||||
if len(sys.argv) < 2:
|
||||
print("No languages specified")
|
||||
sys.exit(1)
|
||||
|
||||
with open('data/categories-strings/en.json/localize.json', 'r', encoding='utf-8') as f:
|
||||
data_en = json.load(f)
|
||||
|
||||
if len(sys.argv) > 2:
|
||||
for lang in sys.argv[1:]:
|
||||
print(f'{lang}:')
|
||||
main(lang, data_en)
|
||||
print('\n')
|
||||
else:
|
||||
main(sys.argv[1], data_en)
|
||||
77
tools/python/categories/json_to_txt.py
Executable file
77
tools/python/categories/json_to_txt.py
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import json
|
||||
import sys
|
||||
|
||||
LANGUAGES = (
|
||||
'af', 'ar', 'be', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'en-AU',
|
||||
'en-GB', 'en-US', 'es', 'es-MX', 'et', 'eu', 'fa', 'fi', 'fr', 'fr-CA',
|
||||
'he', 'hi', 'hu', 'id', 'it', 'ja', 'ko', 'lt', 'lv', 'mr', 'nb', 'nl',
|
||||
'pl', 'pt', 'pt-BR', 'ro', 'ru', 'sk', 'sr', 'sv', 'sw', 'th', 'tr', 'uk',
|
||||
'vi', 'zh-Hans', 'zh-Hant'
|
||||
)
|
||||
|
||||
|
||||
def load_localize_json(lang_dir):
|
||||
file_path = os.path.join(lang_dir, 'localize.json')
|
||||
if not os.path.isfile(file_path):
|
||||
return {}
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
print(f"Error decoding JSON from {file_path}: {e}")
|
||||
return {}
|
||||
|
||||
|
||||
def collect_all_keys(base_dir):
|
||||
all_data = {}
|
||||
lang_dirs = [d for d in os.listdir(base_dir) if d.endswith('.json')]
|
||||
|
||||
for lang_dir in lang_dirs:
|
||||
lang = lang_dir.replace('.json', '')
|
||||
if lang not in LANGUAGES:
|
||||
print(f"Skipping unsupported language directory: {lang_dir}")
|
||||
continue
|
||||
full_path = os.path.join(base_dir, lang_dir)
|
||||
if os.path.isdir(full_path):
|
||||
data = load_localize_json(full_path)
|
||||
for key, value in data.items():
|
||||
if key not in all_data:
|
||||
all_data[key] = {}
|
||||
all_data[key][lang] = value
|
||||
|
||||
return all_data
|
||||
|
||||
|
||||
def write_category_file(all_data, output_file):
|
||||
with open(output_file, 'w', encoding='utf-8') as f:
|
||||
for i, (key, translations) in enumerate(all_data.items()):
|
||||
f.write(key + '\n')
|
||||
for lang in LANGUAGES:
|
||||
if lang in translations and translations[lang]:
|
||||
f.write(f"{lang}:{translations[lang]}\n")
|
||||
elif lang == 'en' and key in translations:
|
||||
f.write('\n')
|
||||
if i < len(all_data) - 1:
|
||||
f.write('\n')
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(f"Usage: {sys.argv[0]} <json_directory> [categories.txt]")
|
||||
sys.exit(1)
|
||||
|
||||
base_dir = sys.argv[1]
|
||||
output_file = sys.argv[2] if len(sys.argv) > 2 else "categories.txt"
|
||||
|
||||
if not os.path.isdir(base_dir):
|
||||
print(f"Directory not found: {base_dir}")
|
||||
sys.exit(1)
|
||||
|
||||
all_data = collect_all_keys(base_dir)
|
||||
write_category_file(all_data, output_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
83
tools/python/categories/txt_to_json.py
Executable file
83
tools/python/categories/txt_to_json.py
Executable file
|
|
@ -0,0 +1,83 @@
|
|||
#!/usr/bin/env python3
|
||||
import os
|
||||
import json
|
||||
import sys
|
||||
|
||||
LANGUAGES = (
|
||||
'af', 'ar', 'be', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'en-AU',
|
||||
'en-GB', 'en-US', 'es', 'es-MX', 'et', 'eu', 'fa', 'fi', 'fr', 'fr-CA',
|
||||
'he', 'hi', 'hu', 'id', 'it', 'ja', 'ko', 'lt', 'lv', 'mr', 'nb', 'nl',
|
||||
'pl', 'pt', 'pt-BR', 'ro', 'ru', 'sk', 'sr', 'sv', 'sw', 'th', 'tr', 'uk',
|
||||
'vi', 'zh-Hans', 'zh-Hant'
|
||||
)
|
||||
|
||||
# TODO: respect the order of key/values in the JSON when converting back and forth
|
||||
|
||||
def parse_translations(input_file):
|
||||
"""
|
||||
Parses a translation file and generates a JSON file per language.
|
||||
"""
|
||||
# Read the input file line by line
|
||||
with open(input_file, 'r', encoding='utf-8') as f:
|
||||
lines = [line.rstrip('\n') for line in f]
|
||||
|
||||
# Split the file into blocks separated by empty lines
|
||||
blocks = []
|
||||
current_block = []
|
||||
for line in lines:
|
||||
stripped_line = line.strip()
|
||||
if stripped_line.startswith('#'):
|
||||
continue
|
||||
if not stripped_line:
|
||||
if current_block:
|
||||
blocks.append(current_block)
|
||||
current_block = []
|
||||
else:
|
||||
current_block.append(line)
|
||||
if current_block:
|
||||
blocks.append(current_block)
|
||||
|
||||
# Initialize dictionaries for each language
|
||||
lang_data = {lang: {} for lang in LANGUAGES}
|
||||
|
||||
# Process each block
|
||||
for block in blocks:
|
||||
key_line = block[0]
|
||||
has_translation = False
|
||||
for line in block[1:]:
|
||||
if ':' not in line:
|
||||
print(f"Skipping invalid line: {line}")
|
||||
continue
|
||||
lang, translation = line.split(':', 1)
|
||||
lang = lang.strip()
|
||||
translation = translation.strip()
|
||||
if lang in LANGUAGES:
|
||||
lang_data[lang][key_line] = translation
|
||||
has_translation = True
|
||||
else:
|
||||
print(f"Warning: Unsupported language {lang} in line: {line}")
|
||||
|
||||
if not has_translation:
|
||||
lang_data['en'][key_line] = ""
|
||||
|
||||
# Write JSON files
|
||||
for lang, data in lang_data.items():
|
||||
if not data:
|
||||
continue
|
||||
dir_name = f"{lang}.json"
|
||||
os.makedirs(dir_name, exist_ok=True)
|
||||
file_path = os.path.join(dir_name, 'localize.json')
|
||||
with open(file_path, 'w', encoding='utf-8') as f:
|
||||
f.write(json.dumps(data, ensure_ascii=False, separators=(",\n", ": ")).replace('{', '{\n').replace('}', '\n}'))
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
print(f"Usage: {sys.argv[0]} <categories.txt>")
|
||||
sys.exit(1)
|
||||
input_file = sys.argv[1]
|
||||
parse_translations(input_file)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue