|
1 | 1 | '''
|
2 | 2 | Script: translate-en-messages.py
|
3 |
| -Version: 2024.11.24.1 |
| 3 | +Version: 2025.2.7 |
4 | 4 | Description: Translate msg's from en/messages.json to [[output_langs]/messages.json]
|
5 | 5 | Author: Adam Lui
|
6 | 6 | Homepage: https://github.com/adamlui/python-utils
|
|
22 | 22 |
|
23 | 23 | # UI initializations
|
24 | 24 | terminal_width = os.get_terminal_size()[0]
|
25 |
| -def print_trunc(msg, end='\n') : print(msg if len(msg) < terminal_width else msg[0:terminal_width-4] + '...', end=end) |
| 25 | +def print_trunc(msg, end='\n'): |
| 26 | + truncated_lines = [ |
| 27 | + line if len(line) < terminal_width else line[:terminal_width - 4] + '...' for line in msg.splitlines() ] |
| 28 | + print('\n'.join(truncated_lines), end=end) |
26 | 29 | def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:terminal_width])
|
27 | 30 |
|
28 | 31 | print('')
|
@@ -77,8 +80,8 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
|
77 | 80 | lang_added, lang_skipped, lang_translated = False, False, False
|
78 | 81 | folder = lang_code.replace('-', '_') ; translated_msgs = {}
|
79 | 82 | if '-' in lang_code: # cap suffix
|
80 |
| - sep_index = folder.index('_') |
81 |
| - folder = folder[:sep_index] + '_' + folder[sep_index+1:].upper() |
| 83 | + sep_idx = folder.index('_') |
| 84 | + folder = folder[:sep_idx] + '_' + folder[sep_idx+1:].upper() |
82 | 85 |
|
83 | 86 | # Skip English locales
|
84 | 87 | if lang_code.startswith('en'):
|
@@ -121,11 +124,11 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
|
121 | 124 |
|
122 | 125 | # Format messages
|
123 | 126 | formatted_msgs = '{\n'
|
124 |
| - for index, (key, message_data) in enumerate(translated_msgs.items()): |
| 127 | + for idx, (key, message_data) in enumerate(translated_msgs.items()): |
125 | 128 | formatted_msg = json.dumps(message_data, ensure_ascii=False) \
|
126 | 129 | .replace('{', '{ ').replace('}', ' }') # add spacing
|
127 | 130 | formatted_msgs += ( f' "{key}": {formatted_msg}'
|
128 |
| - + ( ',\n' if index < len(translated_msgs) - 1 else '\n' )) # terminate line |
| 131 | + + ( ',\n' if idx < len(translated_msgs) - 1 else '\n' )) # terminate line |
129 | 132 | formatted_msgs += '}'
|
130 | 133 | with open(msgs_path, 'w', encoding='utf-8') as output_file : output_file.write(formatted_msgs + '\n')
|
131 | 134 |
|
|
0 commit comments