Skip to content

Commit 581c7c2

Browse files
committed
Changed --init to create .config.jsonc file, added more file types/names supported
1 parent 870a2b8 commit 581c7c2

6 files changed

Lines changed: 18 additions & 10 deletions

File tree

File renamed without changes.

translate-messages/docs/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Options can be set by using command-line arguments:
4646
| `--exclude-langs` | Comma-separated list of languages to exclude | `--exclude-langs=en,es`
4747
| `-k`, `--keys` | Comma-separated list of keys to translate | `--keys=appDesc,err_notFound`
4848
| `--exclude-keys` | Comma-separated list of keys to ignore | `--exclude-keys=appName,author`
49-
| `-i`, `--init` | Create .translate-msgs.config.json in project root to store defaults |
49+
| `-i`, `--init` | Create .translate-msgs.config.jsonc in project root to store defaults |
5050
| `-f`, `--force` | Force overwrite of existing config file when using `--init` |
5151
| `-W`, `--no-wizard` | Skip interactive prompts during start-up |
5252
| `-h`, `--help` | Show help screen |
@@ -69,11 +69,11 @@ translate-msgs -k appDesc,err_notFound -d _msgs -W
6969

7070
## Config file
7171

72-
Use `--init` to create `.translate-msgs.config.json` in your project root to set default options.
72+
Use `--init` to create `.translate-msgs.config.jsonc` in your project root to set default options.
7373

7474
Example defaults:
7575

76-
```json
76+
```jsonc
7777
{
7878
"locales_dir": "_locales",
7979
"target_langs": "",

translate-messages/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ translate-messages = "translate_messages.__main__:main"
7272

7373
[project.optional-dependencies]
7474
dev = [
75+
"json5>=0.9.0",
7576
"tomli>=2.0.0,<3.0.0",
7677
"tomli-w>=0.1.0,<2.0.0",
7778
]

translate-messages/src/translate_messages/lib/data/json.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import json, os
2+
import json5
23

3-
def read(json_path):
4-
if not os.path.exists(json_path) : return {}
5-
with open(json_path, 'r', encoding='utf-8') as file : return json.load(file)
4+
def read(file_path):
5+
with open(file_path, 'r') as file:
6+
return json5.load(file)
67

78
def write(src_data, target_path):
89
os.makedirs(os.path.dirname(target_path), exist_ok=True)

translate-messages/src/translate_messages/lib/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def config_file(cli):
1414
log.warn(f'Config already exists at {cli.config_filepath}. Skipping --init.')
1515
log.tip('Pass --force to overwrite.')
1616
return
17-
cli.config_filename = '.translate-msgs.config.json'
17+
cli.config_filename = '.translate-msgs.config.jsonc'
1818
cli.config_filepath = os.path.join(cli.project_root, cli.config_filename)
1919
try:
2020
jsd_url = f'{cli.urls.jsdelivr}/{cli.name}/{cli.config_filename}'

translate-messages/src/translate_messages/lib/settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
),
2727
init=sns(
2828
args=['-i', '--init'],
29-
action='store_true', help='Create .translate-msgs.config.json file to store defaults'
29+
action='store_true', help='Create .translate-msgs.config.jsonc file to store defaults'
3030
),
3131
force=sns(
3232
args=['-f', '--force', '--overwrite'],
@@ -48,15 +48,21 @@ def load(cli, caller_file):
4848
cli.config = sns()
4949
cli.project_root = path.join(path.dirname(caller_file),
5050
f"../../{ '' if 'src' in path.dirname(caller_file) else '../../' }")
51+
if not getattr(cli, 'short_name', None) : cli.short_name = cli.name.replace('messages', 'msgs')
5152
possile_config_filenames = [
52-
'.translate-msgs.config.json', 'translate-msgs.config.json',
53-
f'.{cli.name}.config.json', f'{cli.name}.config.json'
53+
f'.{cli.short_name}.config.json', f'{cli.short_name}.config.json',
54+
f'.{cli.short_name}.config.jsonc', f'{cli.short_name}.config.json',
55+
f'.{cli.short_name}.config.json5', f'{cli.short_name}.config.json',
56+
f'.{cli.name}.config.json', f'{cli.name}.config.json',
57+
f'.{cli.name}.config.jsonc', f'{cli.name}.config.jsonc',
58+
f'.{cli.name}.config.json5', f'{cli.name}.config.json5',
5459
]
5560
for filename in possile_config_filenames:
5661
cli.config_filepath = path.join(cli.project_root, filename)
5762
if path.exists(cli.config_filepath):
5863
cli.config = data.sns.from_dict(data.json.read(cli.config_filepath))
5964
cli.config_filename = filename
65+
print(cli.config) ; exit()
6066
break
6167

6268
# Parse CLI args

0 commit comments

Comments
 (0)