|
1 | 1 | ''' |
2 | 2 | Name: translate-en-messages.py |
3 | | -Version: 2026.2.10.15 |
| 3 | +Version: 2026.2.10.16 |
4 | 4 | Author: Adam Lui |
5 | 5 | Description: Translate en/messages.json to other locales |
6 | 6 | Homepage: https://github.com/adamlui/python-utils |
|
15 | 15 | from translate import Translator |
16 | 16 | from urllib.request import urlopen |
17 | 17 |
|
18 | | -locales_folder = '_locales' ; provider = '' |
| 18 | +provider = '' |
19 | 19 | default_target_locales = [ |
20 | 20 | 'af', 'am', 'ar', 'az', 'be', 'bem', 'bg', 'bn', 'bo', 'bs', 'ca', 'ceb', 'cs', 'cy', 'da', 'de', 'dv', 'dz', 'el', |
21 | 21 | 'en', 'en-GB', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'gd', 'gl', 'gu', 'haw', 'he', 'hi', 'hr', 'ht', |
|
39 | 39 | parser.add_argument('--include-langs', type=str, help='Languages to include (e.g. "en,es,fr")') |
40 | 40 | parser.add_argument('--exclude-langs', type=str, help='Languages to exclude (e.g. "en,es")') |
41 | 41 | parser.add_argument('--ignore-keys', type=str, help='Keys to ignore (e.g. "appName,author")') |
| 42 | +parser.add_argument('--locales-dir', type=str, help='Name of folder containing locales') |
42 | 43 | parser.add_argument('--init', action='store_true', help='Create .config.json file to store defaults') |
43 | 44 | args = parser.parse_args() |
| 45 | +locales_dir = args.locales_dir or '_locales' |
44 | 46 |
|
45 | 47 | if args.init: # create config file |
46 | 48 | if os.path.exists(config_path): |
@@ -86,25 +88,24 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin |
86 | 88 | ignore_keys.append(key) |
87 | 89 |
|
88 | 90 | # Determine closest locales dir |
89 | | -print_trunc(f'\nSearching for { locales_folder }...') |
| 91 | +print_trunc(f'\nSearching for { locales_dir }...') |
90 | 92 | script_dir = os.path.abspath(os.path.dirname(__file__)) |
91 | | -locales_dir = None |
92 | 93 | for root, dirs, files in os.walk(script_dir): # search script dir recursively |
93 | | - if locales_folder in dirs: |
94 | | - locales_dir = os.path.join(root, locales_folder) ; break |
| 94 | + if locales_dir in dirs: |
| 95 | + locales_dir = os.path.join(root, locales_dir) ; break |
95 | 96 | else: # search script parent dirs recursively |
96 | 97 | parent_dir = os.path.dirname(script_dir) |
97 | 98 | while parent_dir and parent_dir != script_dir: |
98 | 99 | for root, dirs, files in os.walk(parent_dir): |
99 | | - if locales_folder in dirs: |
100 | | - locales_dir = os.path.join(root, locales_folder) ; break |
| 100 | + if locales_dir in dirs: |
| 101 | + locales_dir = os.path.join(root, locales_dir) ; break |
101 | 102 | if locales_dir : break |
102 | 103 | parent_dir = os.path.dirname(parent_dir) |
103 | 104 | else : locales_dir = None |
104 | 105 |
|
105 | 106 | # Print result |
106 | 107 | if locales_dir : print_trunc(f'_locales directory found!\n\n>> { locales_dir }\n') |
107 | | -else : print_trunc(f'Unable to locate a { locales_folder } directory.') ; exit() |
| 108 | +else : print_trunc(f'Unable to locate a { locales_dir } directory.') ; exit() |
108 | 109 |
|
109 | 110 | # Load en/messages.json |
110 | 111 | msgs_filename = 'messages.json' |
|
0 commit comments