Last active
October 30, 2022 18:50
-
-
Save TheMeanCanEHdian/f67b8f5941b5d41d5edf33311dec85e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| language_codes = ['ca', 'cs', 'da', 'de', 'el', 'es', 'fr', 'hu', 'it', 'nb', 'nl', 'pl', 'pt-BR', 'pt-PT', 'ru', 'sk', 'sl', 'sq', 'sv', 'zh'] | |
| old_translation_location = 'old_translations/' | |
| new_translation_location = 'new_translations/' | |
| # Open old and new english translations | |
| with open(f'{old_translation_location}en.json', encoding="utf8") as f: | |
| old = json.load(f) | |
| with open(f'{new_translation_location}en.json', encoding="utf8") as f: | |
| new = json.load(f) | |
| shared_translations = [] | |
| for new_key in new: | |
| for old_key in old: | |
| # Compare values between old and new english string to find any that are | |
| # identical. | |
| if new[new_key] == old[old_key]: | |
| # Add old and new keys for identical strings to shared_translations | |
| shared_translations.append((new_key, old_key)) | |
| print('----------') | |
| print(f'New translations: {len(new)}') | |
| print(f'Old translations: {len(old)}') | |
| print(f'Total reusable: {len(shared_translations)}') | |
| print('----------') | |
| # For each language code find all the old translations that matches an old | |
| # english key from shared_translations and add it to translation_dict. | |
| for code in language_codes: | |
| translation_dict = {} | |
| for keys in shared_translations: | |
| try: | |
| with open(f'{old_translation_location}{code}.json', encoding="utf8") as f: | |
| old_translations = json.load(f) | |
| try: | |
| translation_dict[keys[0]] = old_translations[keys[1]] | |
| except: | |
| continue | |
| except: | |
| continue | |
| # Process translations in new file and remove any existing keys from translation_dict | |
| new_translations = {} | |
| with open(f'{new_translation_location}{code}.json', encoding="utf8") as f: | |
| new_translations = json.load(f) | |
| for key in new_translations: | |
| if key in translation_dict: | |
| del translation_dict[key] | |
| # Combine new and old translations, sort, and save file | |
| translation_dict = translation_dict | new_translations | |
| # Sort and save translation_dict to a new file under new_translations | |
| if (translation_dict): | |
| translation_dict_sorted = {k: translation_dict[k] for k in sorted(translation_dict)} | |
| else: | |
| translation_dict_sorted = {} | |
| print(f'{code} translations: {len(translation_dict_sorted)}\n') | |
| with open(f'new_translations/{code}.json', 'w', encoding="utf8") as json_file: | |
| json.dump(translation_dict_sorted, json_file, ensure_ascii=False, indent=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment