|
1 | 1 | ''' |
2 | 2 | Name: translate-en-messages.py |
3 | | -Version: 2026.2.10.14 |
| 3 | +Version: 2026.2.10.15 |
4 | 4 | Author: Adam Lui |
5 | 5 | Description: Translate en/messages.json to other locales |
6 | 6 | Homepage: https://github.com/adamlui/python-utils |
|
26 | 26 | ] |
27 | 27 |
|
28 | 28 | # Init/load config file |
29 | | -DEFAULT_CONFIG = { 'include_langs': '', 'exclude_langs': '', 'ignore_keys': '' } |
30 | 29 | script_name = os.path.splitext(os.path.basename(__file__))[0] |
31 | 30 | config_filename = f'{script_name}.config.json' |
32 | 31 | config_path = os.path.join(os.path.dirname(__file__), config_filename) |
33 | | -config_data = DEFAULT_CONFIG.copy() |
| 32 | +config_data = {} |
34 | 33 | if os.path.exists(config_path): |
35 | 34 | with open(config_path, 'r', encoding='utf-8') as file_config: |
36 | 35 | config_data.update(json.load(file_config)) |
37 | 36 |
|
38 | 37 | # Parse CLI args |
39 | 38 | parser = argparse.ArgumentParser(description='Translate en/messages.json to other locales') |
40 | | -parser.add_argument('--include-langs', type=str, help='Comma-separated list of languages to include (e.g. "en,es,fr")') |
41 | | -parser.add_argument('--exclude-langs', type=str, help='Comma-separated list of languages to exclude (e.g. "en,es")') |
42 | | -parser.add_argument('--ignore-keys', type=str, help='Comma-separated list of keys to ignore (e.g. "appName,author")') |
43 | | -parser.add_argument('--init', action='store_true', help='Create a .config.json adjacent to this script') |
| 39 | +parser.add_argument('--include-langs', type=str, help='Languages to include (e.g. "en,es,fr")') |
| 40 | +parser.add_argument('--exclude-langs', type=str, help='Languages to exclude (e.g. "en,es")') |
| 41 | +parser.add_argument('--ignore-keys', type=str, help='Keys to ignore (e.g. "appName,author")') |
| 42 | +parser.add_argument('--init', action='store_true', help='Create .config.json file to store defaults') |
44 | 43 | args = parser.parse_args() |
45 | 44 |
|
46 | 45 | if args.init: # create config file |
47 | 46 | if os.path.exists(config_path): |
48 | 47 | print(f'Config already exists at {config_path}') |
49 | 48 | else: |
50 | | - try: # to use jsDelivr copy |
| 49 | + try: # try to fetch template from jsDelivr |
51 | 50 | jsd_url = f'https://cdn.jsdelivr.net/gh/adamlui/python-utils/translate-messages/{config_filename}' |
52 | 51 | with urlopen(jsd_url) as resp: |
53 | 52 | if resp.status == 200 : config_data = json.loads(resp.read().decode('utf-8')) |
54 | | - except Exception: |
55 | | - config_data = { 'include_langs': '', 'exclude_langs': '' } |
56 | | - with open(config_path, 'w', encoding='utf-8') as f: json.dump(config_data, f, indent=2) |
| 53 | + except Exception : pass |
| 54 | + with open(config_path, 'w', encoding='utf-8') as configFile: |
| 55 | + json.dump(config_data, configFile, indent=2) |
57 | 56 | print(f'Default config created at {config_path}') |
58 | 57 | exit() |
59 | 58 |
|
|
0 commit comments