Skip to content

Commit b504b02

Browse files
committed
fix: share setup that can be shared on all platforms
1 parent 89cee16 commit b504b02

File tree

4 files changed

+47
-69
lines changed

4 files changed

+47
-69
lines changed

crowdin/generate_android_strings.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import re
77
from pathlib import Path
88
from colorama import Fore, Style
9-
from generate_shared import load_glossary_dict, clean_string
9+
from generate_shared import load_glossary_dict, clean_string, setup_generation
1010

1111
# Variables that should be treated as numeric (using %d)
1212
NUMERIC_VARIABLES = ['count', 'found_count', 'total_count']
@@ -144,26 +144,9 @@ def convert_non_translatable_strings_to_kotlin(input_file, output_path):
144144
return app_name
145145

146146
def convert_all_files(input_directory):
147-
# Extract the project information
148-
print(f"\033[2K{Fore.WHITE}⏳ Processing project info...{Style.RESET_ALL}", end='\r')
149-
project_info_file = os.path.join(input_directory, "_project_info.json")
150-
if not os.path.exists(project_info_file):
151-
raise FileNotFoundError(f"Could not find '{project_info_file}' in raw translations directory")
152-
153-
project_details = {}
154-
with open(project_info_file, 'r', encoding="utf-8") as file:
155-
project_details = json.load(file)
156-
157-
# Extract the language info and sort the target languages alphabetically by locale
158-
source_language = project_details['data']['sourceLanguage']
159-
target_languages = project_details['data']['targetLanguages']
160-
target_languages.sort(key=lambda x: x['locale'])
161-
num_languages = len(target_languages)
162-
print(f"\033[2K{Fore.GREEN}✅ Project info processed, {num_languages} languages will be converted{Style.RESET_ALL}")
163-
164-
# Convert the non-translatable strings to the desired format
165-
print(f"\033[2K{Fore.WHITE}⏳ Generating static strings file...{Style.RESET_ALL}", end='\r')
166-
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
147+
setup_values = setup_generation(input_directory)
148+
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
149+
167150
app_name = convert_non_translatable_strings_to_kotlin(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH)
168151
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")
169152

crowdin/generate_desktop_strings.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import argparse
66
from pathlib import Path
77
from colorama import Fore, Style
8-
from generate_shared import clean_string, load_glossary_dict
8+
from generate_shared import clean_string, load_glossary_dict, setup_generation
99

1010
# Customizable mapping for output folder hierarchy
1111
# Add entries here to customize the output path for specific locales
@@ -93,7 +93,6 @@ def convert_xliff_to_json(input_file, output_dir, locale, locale_two_letter_code
9393
sorted_translations = sorted(translations.items())
9494
converted_translations = {}
9595

96-
9796
for resname, target in sorted_translations:
9897
converted_translations[resname] = generate_icu_pattern(target, glossary_dict)
9998

@@ -138,24 +137,8 @@ def convert_non_translatable_strings_to_type_script(input_file, output_path, exp
138137

139138

140139
def convert_all_files(input_directory):
141-
# Extract the project information
142-
print(f"\033[2K{Fore.WHITE}⏳ Processing project info...{Style.RESET_ALL}", end='\r')
143-
project_info_file = os.path.join(input_directory, "_project_info.json")
144-
if not os.path.exists(project_info_file):
145-
raise FileNotFoundError(f"Could not find '{project_info_file}' in raw translations directory")
146-
147-
project_details = {}
148-
with open(project_info_file, 'r', encoding="utf-8") as file:
149-
project_details = json.load(file)
150-
151-
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
152-
153-
# Extract the language info and sort the target languages alphabetically by locale
154-
source_language = project_details['data']['sourceLanguage']
155-
target_languages = project_details['data']['targetLanguages']
156-
target_languages.sort(key=lambda x: x['locale'])
157-
num_languages = len(target_languages)
158-
print(f"\033[2K{Fore.GREEN}✅ Project info processed, {num_languages} languages will be converted{Style.RESET_ALL}")
140+
setup_values = setup_generation(input_directory)
141+
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
159142

160143
# Convert the XLIFF data to the desired format
161144
print(f"\033[2K{Fore.WHITE}⏳ Converting translations to target format...{Style.RESET_ALL}", end='\r')
@@ -174,7 +157,6 @@ def convert_all_files(input_directory):
174157
# Convert the non-translatable strings to the desired format
175158
print(f"\033[2K{Fore.WHITE}⏳ Generating static strings file...{Style.RESET_ALL}", end='\r')
176159

177-
rtl_languages = [lang for lang in target_languages if lang["textDirection"] == "rtl"]
178160
convert_non_translatable_strings_to_type_script(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH, exported_locales, rtl_languages)
179161
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")
180162

crowdin/generate_ios_strings.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from colorama import Fore, Style
99
from datetime import datetime
10-
from generate_shared import load_glossary_dict, clean_string
10+
from generate_shared import load_glossary_dict, clean_string, setup_generation
1111

1212

1313
# It seems that Xcode uses different language codes and doesn't support all of the languages we get from Crowdin
@@ -109,7 +109,7 @@ def convert_placeholders_for_plurals(translations):
109109
# Replace {count} with %lld for iOS
110110
converted_translations = {}
111111
for form, value in translations.items():
112-
converted_translations[form] = clean_string(value.replace('{count}', '%lld'), False, {}, {})
112+
converted_translations[form] = clean_string(value, False, {}, {'{count}': '%lld'})
113113

114114
return converted_translations
115115

@@ -240,26 +240,10 @@ def convert_non_translatable_strings_to_swift(input_file, output_path):
240240
file.write('}\n')
241241

242242
def convert_all_files(input_directory):
243-
# Extract the project information
244-
print(f"\033[2K{Fore.WHITE}⏳ Processing project info...{Style.RESET_ALL}", end='\r')
245-
project_info_file = os.path.join(input_directory, "_project_info.json")
246-
if not os.path.exists(project_info_file):
247-
raise FileNotFoundError(f"Could not find '{project_info_file}' in raw translations directory")
248-
249-
project_details = {}
250-
with open(project_info_file, 'r', encoding="utf-8") as file:
251-
project_details = json.load(file)
252-
253-
# Extract the language info and sort the target languages alphabetically by locale
254-
source_language = project_details['data']['sourceLanguage']
255-
target_languages = project_details['data']['targetLanguages']
256-
target_languages.sort(key=lambda x: x['locale'])
257-
num_languages = len(target_languages)
258-
print(f"\033[2K{Fore.GREEN}✅ Project info processed, {num_languages} languages will be converted{Style.RESET_ALL}")
259-
260-
# Convert the non-translatable strings to the desired format
243+
setup_values = setup_generation(input_directory)
244+
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
261245
print(f"\033[2K{Fore.WHITE}⏳ Generating static strings file...{Style.RESET_ALL}", end='\r')
262-
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
246+
263247
convert_non_translatable_strings_to_swift(non_translatable_strings_file, NON_TRANSLATABLE_STRINGS_OUTPUT_PATH)
264248
print(f"\033[2K{Fore.GREEN}✅ Static string generation complete{Style.RESET_ALL}")
265249

crowdin/generate_shared.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import html
22
import json
33
import os
4+
from colorama import Fore, Style
45

56
def clean_string(text, is_android, glossary_dict, extra_replace_dict):
6-
to_ret = text
7-
if(is_android):
7+
if is_android:
88
# Note: any changes done for all platforms needs most likely to be done on crowdin side.
99
# So we don't want to replace -> with → for instance, we want the crowdin strings to not have those at all.
1010
# We can use standard XML escaped characters for most things (since XLIFF is an XML format) but
@@ -20,16 +20,16 @@ def clean_string(text, is_android, glossary_dict, extra_replace_dict):
2020
else:
2121
text = html.unescape(text) # Unescape any HTML escaping
2222

23-
stripped = to_ret.strip() # Strip whitespace
23+
text = text.strip() # Strip whitespace
2424

2525
# replace all the defined constants (from crowdin's glossary) in the string
2626
for glossary_key in glossary_dict:
27-
stripped = stripped.replace("{" + glossary_key + "}", glossary_dict[glossary_key])
27+
text = text.replace("{" + glossary_key + "}", glossary_dict[glossary_key])
2828

2929
# if extra_replace_dict has keys, replace those too
3030
for extra_key in extra_replace_dict:
31-
stripped = stripped.replace(extra_key, extra_replace_dict[extra_key])
32-
return stripped
31+
text = text.replace(extra_key, extra_replace_dict[extra_key])
32+
return text
3333

3434

3535
def load_glossary_dict(input_file):
@@ -48,3 +48,32 @@ def load_glossary_dict(input_file):
4848
}
4949

5050
return glossary_dict
51+
52+
53+
def setup_generation(input_directory):
54+
# Extract the project information
55+
print(f"\033[2K{Fore.WHITE}⏳ Processing project info...{Style.RESET_ALL}", end='\r')
56+
project_info_file = os.path.join(input_directory, "_project_info.json")
57+
if not os.path.exists(project_info_file):
58+
raise FileNotFoundError(f"Could not find '{project_info_file}' in raw translations directory")
59+
60+
project_details = {}
61+
with open(project_info_file, 'r', encoding="utf-8") as file:
62+
project_details = json.load(file)
63+
64+
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
65+
66+
# Extract the language info and sort the target languages alphabetically by locale
67+
source_language = project_details['data']['sourceLanguage']
68+
target_languages = project_details['data']['targetLanguages']
69+
target_languages.sort(key=lambda x: x['locale'])
70+
num_languages = len(target_languages)
71+
print(f"\033[2K{Fore.GREEN}✅ Project info processed, {num_languages} languages will be converted{Style.RESET_ALL}")
72+
73+
# Convert the non-translatable strings to the desired format
74+
rtl_languages = [lang for lang in target_languages if lang["textDirection"] == "rtl"]
75+
76+
return {"source_language":source_language,
77+
"rtl_languages": rtl_languages,
78+
"non_translatable_strings_file":non_translatable_strings_file,
79+
"target_languages": target_languages}

0 commit comments

Comments
 (0)