Skip to content

Commit 91758e2

Browse files
committed
chore: added some typings to functions
1 parent edcc64f commit 91758e2

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

crowdin/download_translations_from_crowdin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def check_error(response):
4343
print(f"{Fore.BLUE}Response: {json.dumps(response.json(), indent=2)}{Style.RESET_ALL}")
4444
sys.exit(1)
4545

46-
def download_file(url, output_path):
46+
def download_file(url: str, output_path: str):
4747
"""
4848
Function to download a file from Crowdin
4949
"""
@@ -102,7 +102,7 @@ def main():
102102
print(f"\n{Fore.BLUE}Response: {json.dumps(source_export_response.json(), indent=2)}{Style.RESET_ALL}")
103103

104104
# Download the exported file
105-
source_download_url = source_export_response.json()['data']['url']
105+
source_download_url: str = source_export_response.json()['data']['url']
106106
source_download_path = os.path.join(DOWNLOAD_DIRECTORY, f"{source_lang_locale}.xliff")
107107
print(f"\033[2K{Fore.WHITE}⏳ Downloading translations for {source_lang_locale}...{Style.RESET_ALL}", end='\r')
108108
try:

crowdin/generate_android_strings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import json
32
import xml.etree.ElementTree as ET
43
import sys
54
import argparse
@@ -143,7 +142,7 @@ def convert_non_translatable_strings_to_kotlin(input_file, output_path):
143142
if not app_name:
144143
raise ValueError("could not find app_name in glossary_dict")
145144

146-
def convert_all_files(input_directory):
145+
def convert_all_files(input_directory: str ):
147146
setup_values = setup_generation(input_directory)
148147
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
149148

crowdin/generate_desktop_strings.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import json
3+
from typing import Dict, List
34
import xml.etree.ElementTree as ET
45
import sys
56
import argparse
@@ -37,7 +38,6 @@
3738
TRANSLATIONS_OUTPUT_DIRECTORY = args.translations_output_directory
3839
NON_TRANSLATABLE_STRINGS_OUTPUT_PATH = args.non_translatable_strings_output_path
3940

40-
clean_string_extra_dict = {'{count}': '#'}
4141

4242
def parse_xliff(file_path):
4343
tree = ET.parse(file_path)
@@ -72,17 +72,17 @@ def parse_xliff(file_path):
7272
return translations
7373

7474

75-
def generate_icu_pattern(target, glossary_dict):
75+
def generate_icu_pattern(target, glossary_dict : Dict[str,str]):
7676
if isinstance(target, dict): # It's a plural group
7777
pattern_parts = []
7878
for form, value in target.items():
7979
if form in ['zero', 'one', 'two', 'few', 'many', 'other', 'exact', 'fractional']:
80-
value = clean_string(value, False, glossary_dict, clean_string_extra_dict)
80+
value = clean_string(value, False, glossary_dict, {})
8181
pattern_parts.append(f"{form} [{value}]")
8282

8383
return "{{count, plural, {0}}}".format(" ".join(pattern_parts))
8484
else: # It's a regular string
85-
return clean_string(target, False, glossary_dict, clean_string_extra_dict)
85+
return clean_string(target, False, glossary_dict, {})
8686

8787
def convert_xliff_to_json(input_file, output_dir, locale, locale_two_letter_code, glossary_dict):
8888
if not os.path.exists(input_file):
@@ -110,7 +110,7 @@ def convert_xliff_to_json(input_file, output_dir, locale, locale_two_letter_code
110110

111111

112112

113-
def convert_non_translatable_strings_to_type_script(input_file, output_path, exported_locales, rtl_languages):
113+
def convert_non_translatable_strings_to_type_script(input_file: str, output_path: str, exported_locales: List[str], rtl_languages: List[str]):
114114
glossary_dict = load_glossary_dict(input_file)
115115
rtl_locales = sorted([lang["twoLettersCode"] for lang in rtl_languages])
116116

@@ -140,7 +140,7 @@ def convert_non_translatable_strings_to_type_script(input_file, output_path, exp
140140
file.write('\n')
141141

142142

143-
def convert_all_files(input_directory):
143+
def convert_all_files(input_directory: str):
144144
setup_values = setup_generation(input_directory)
145145
source_language, rtl_languages, non_translatable_strings_file, target_languages = setup_values.values()
146146

crowdin/generate_ios_strings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from colorama import Fore, Style
99
from datetime import datetime
1010
from generate_shared import load_glossary_dict, clean_string, setup_generation
11+
from typing import Dict
1112

1213

1314
# It seems that Xcode uses different language codes and doesn't support all of the languages we get from Crowdin
@@ -105,7 +106,7 @@ def parse_xliff(file_path):
105106

106107
return translations, target_language
107108

108-
def convert_placeholders_for_plurals(translations, glossary_dict):
109+
def convert_placeholders_for_plurals(translations: Dict[str,str], glossary_dict: Dict[str,str]):
109110
# Replace {count} with %lld for iOS
110111
converted_translations = {}
111112
for form, value in translations.items():

crowdin/generate_shared.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from typing import Dict, List
12
import html
23
import json
34
import os
45
from colorama import Fore, Style
56

6-
def clean_string(text, is_android, glossary_dict, extra_replace_dict):
7+
def clean_string(text: str, is_android: bool, glossary_dict: Dict[str, str], extra_replace_dict :Dict[str,str]):
78
if is_android:
89
# Note: any changes done for all platforms needs most likely to be done on crowdin side.
910
# So we don't want to replace -> with → for instance, we want the crowdin strings to not have those at all.
@@ -32,7 +33,7 @@ def clean_string(text, is_android, glossary_dict, extra_replace_dict):
3233
return text
3334

3435

35-
def load_glossary_dict(input_file):
36+
def load_glossary_dict(input_file: str) -> Dict[str, str]:
3637
if not os.path.exists(input_file):
3738
raise FileNotFoundError(f"Could not find '{input_file}' in raw translations directory")
3839

@@ -50,7 +51,7 @@ def load_glossary_dict(input_file):
5051
return glossary_dict
5152

5253

53-
def setup_generation(input_directory):
54+
def setup_generation(input_directory: str):
5455
# Extract the project information
5556
print(f"\033[2K{Fore.WHITE}⏳ Processing project info...{Style.RESET_ALL}", end='\r')
5657
project_info_file = os.path.join(input_directory, "_project_info.json")
@@ -64,16 +65,16 @@ def setup_generation(input_directory):
6465
non_translatable_strings_file = os.path.join(input_directory, "_non_translatable_strings.json")
6566

6667
# 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']
68+
source_language: str = project_details['data']['sourceLanguage']
69+
target_languages: List[str] = project_details['data']['targetLanguages']
6970
target_languages.sort(key=lambda x: x['locale'])
7071
num_languages = len(target_languages)
7172
print(f"\033[2K{Fore.GREEN}✅ Project info processed, {num_languages} languages will be converted{Style.RESET_ALL}")
7273

7374
# Convert the non-translatable strings to the desired format
74-
rtl_languages = [lang for lang in target_languages if lang["textDirection"] == "rtl"]
75+
rtl_languages: List[str] = [lang for lang in target_languages if lang["textDirection"] == "rtl"]
7576

7677
return {"source_language":source_language,
7778
"rtl_languages": rtl_languages,
7879
"non_translatable_strings_file":non_translatable_strings_file,
79-
"target_languages": target_languages}
80+
"target_languages": target_languages}

0 commit comments

Comments
 (0)