11'''
22Name: translate-en-messages.py
3- Version: 2026.2.10.21
3+ Version: 2026.2.10.22
44Author: Adam Lui
55Description: Translate en/messages.json to other locales
66Homepage: https://github.com/adamlui/python-utils
3131]
3232
3333# Init/load config file
34- script_name = os .path .splitext (os .path .basename (__file__ ))[0 ]
35- config_filename = f'{ script_name } .config.json'
36- config_path = os .path .join (os .path .dirname (__file__ ), config_filename )
37- config_data = {}
38- if os .path .exists (config_path ):
39- with open (config_path , 'r' , encoding = 'utf-8' ) as file_config :
40- config_data .update (json .load (file_config ))
34+ cli . script_name = os .path .splitext (os .path .basename (__file__ ))[0 ]
35+ cli . config_filename = f'{ cli . script_name } .config.json'
36+ cli . config_path = os .path .join (os .path .dirname (__file__ ), cli . config_filename )
37+ cli . config_data = {}
38+ if os .path .exists (cli . config_path ):
39+ with open (cli . config_path , 'r' , encoding = 'utf-8' ) as file_config :
40+ cli . config_data .update (json .load (file_config ))
4141
4242# Parse CLI args
4343parser = argparse .ArgumentParser (description = 'Translate en/messages.json to other locales' )
4747parser .add_argument ('--locales-dir' , type = str , help = 'Name of folder containing locales' )
4848parser .add_argument ('--init' , action = 'store_true' , help = 'Create .config.json file to store defaults' )
4949args = parser .parse_args ()
50- locales_dir = args .locales_dir or config_data .get ('locales_dir' , '' ) or '_locales'
50+ locales_dir = args .locales_dir or cli . config_data .get ('locales_dir' , '' ) or '_locales'
5151
5252if args .init : # create config file
53- if os .path .exists (config_path ):
54- print (f'Config already exists at { config_path } ' )
53+ if os .path .exists (cli . config_path ):
54+ print (f'Config already exists at { cli . config_path } ' )
5555 else :
5656 try : # try to fetch template from jsDelivr
57- jsd_url = f'{ cli .urls .jsdelivr } /{ cli .name } /{ config_filename } '
57+ jsd_url = f'{ cli .urls .jsdelivr } /{ cli .name } /{ cli . config_filename } '
5858 with urlopen (jsd_url ) as resp :
59- if resp .status == 200 : config_data = json .loads (resp .read ().decode ('utf-8' ))
59+ if resp .status == 200 : cli . config_data = json .loads (resp .read ().decode ('utf-8' ))
6060 except Exception : pass
61- with open (config_path , 'w' , encoding = 'utf-8' ) as configFile :
62- json .dump (config_data , configFile , indent = 2 )
63- print (f'Default config created at { config_path } ' )
61+ with open (cli . config_path , 'w' , encoding = 'utf-8' ) as configFile :
62+ json .dump (cli . config_data , configFile , indent = 2 )
63+ print (f'Default config created at { cli . config_path } ' )
6464 exit ()
6565
6666# Init target_locales
6767def parse_csv_langs (str ) : return [lang .strip () for lang in str .split (',' ) if lang .strip ()]
68- include_arg = args .include_langs or config_data .get ('include_langs' , '' )
69- exclude_arg = args .exclude_langs or config_data .get ('exclude_langs' , '' )
68+ include_arg = args .include_langs or cli . config_data .get ('include_langs' , '' )
69+ exclude_arg = args .exclude_langs or cli . config_data .get ('exclude_langs' , '' )
7070target_locales = parse_csv_langs (include_arg ) or default_target_locales
7171exclude_langs = set (parse_csv_langs (exclude_arg ))
7272target_locales = [lang for lang in target_locales if lang not in exclude_langs ]
@@ -85,7 +85,7 @@ def overwrite_print(msg) : stdout.write('\r' + msg.ljust(terminal_width)[:termin
8585print ('' )
8686
8787# Prompt user for keys to ignore
88- ignore_keys = parse_csv_langs (args .ignore_keys or config_data .get ('ignore_keys' , '' ))
88+ ignore_keys = parse_csv_langs (args .ignore_keys or cli . config_data .get ('ignore_keys' , '' ))
8989while True :
9090 if ignore_keys : print ('Ignored key(s):' , ignore_keys )
9191 key = input ('Enter key to ignore (or ENTER if done): ' )
0 commit comments