Skip to content

Commit

Permalink
Merge pull request #24 from oxen-io/export-list-locales-desktop
Browse files Browse the repository at this point in the history
fix: [desktop] export list of supported locales
  • Loading branch information
Bilb authored Sep 16, 2024
2 parents bf296d6 + c2fc7eb commit b608d0f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions crowdin/generate_desktop_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ def convert_xliff_to_json(input_file, output_dir, locale, locale_two_letter_code
json.dump(converted_translations, file, ensure_ascii=False, indent=2)
file.write('\n')
file.write('\n')
return output_locale

def convert_non_translatable_strings_to_type_script(input_file, output_path, rtl_languages):
def convert_non_translatable_strings_to_type_script(input_file, output_path, exported_locales, rtl_languages):
if not os.path.exists(input_file):
raise FileNotFoundError(f"Could not find '{input_file}' in raw translations directory")

Expand All @@ -118,6 +119,7 @@ def convert_non_translatable_strings_to_type_script(input_file, output_path, rtl
# Output the file in the desired format
Path(output_path).parent.mkdir(parents=True, exist_ok=True)

joined_exported_locales = ", ".join(f"\n '{locale}'" for locale in exported_locales)
joined_rtl_locales = ", ".join(f"'{locale}'" for locale in rtl_locales)

with open(output_path, 'w', encoding='utf-8') as file:
Expand All @@ -131,6 +133,11 @@ def convert_non_translatable_strings_to_type_script(input_file, output_path, rtl
file.write('\n')
file.write(f"export const rtlLocales = [{joined_rtl_locales}];\n")
file.write('\n')
file.write(f"export const crowdinLocales = [{joined_exported_locales},\n] as const;\n")
file.write('\n')
file.write(f"export type CrowdinLocale = (typeof crowdinLocales)[number];\n")
file.write('\n')


def convert_all_files(input_directory):
# Extract the project information
Expand All @@ -150,23 +157,25 @@ def convert_all_files(input_directory):
num_languages = len(target_languages)
print(f"\033[2K{Fore.GREEN}✅ Project info processed, {num_languages} languages will be converted{Style.RESET_ALL}")

# Convert the non-translatable strings to the desired format
print(f"\033[2K{Fore.WHITE}⏳ Generating static strings file...{Style.RESET_ALL}", end='\r')
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
rtl_languages = [lang for lang in target_languages if lang["textDirection"] == "rtl"]
convert_non_translatable_strings_to_type_script(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH, rtl_languages)
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")

# Convert the XLIFF data to the desired format
print(f"\033[2K{Fore.WHITE}⏳ Converting translations to target format...{Style.RESET_ALL}", end='\r')
exported_locales = []
for language in [source_language] + target_languages:
lang_locale = language['locale']
lang_two_letter_code = language['twoLettersCode']
print(f"\033[2K{Fore.WHITE}⏳ Converting translations for {lang_locale} to target format...{Style.RESET_ALL}", end='\r')
input_file = os.path.join(input_directory, f"{lang_locale}.xliff")
convert_xliff_to_json(input_file, TRANSLATIONS_OUTPUT_DIRECTORY, lang_locale, lang_two_letter_code)
exported_as = convert_xliff_to_json(input_file, TRANSLATIONS_OUTPUT_DIRECTORY, lang_locale, lang_two_letter_code)
exported_locales.append(exported_as)
print(f"\033[2K{Fore.GREEN}✅ All conversions complete{Style.RESET_ALL}")

# Convert the non-translatable strings to the desired format
print(f"\033[2K{Fore.WHITE}⏳ Generating static strings file...{Style.RESET_ALL}", end='\r')
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
rtl_languages = [lang for lang in target_languages if lang["textDirection"] == "rtl"]
convert_non_translatable_strings_to_type_script(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH, exported_locales, rtl_languages)
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")

if __name__ == "__main__":
try:
convert_all_files(INPUT_DIRECTORY)
Expand Down

0 comments on commit b608d0f

Please sign in to comment.