Skip to content

Commit 242ed85

Browse files
committed
Disabled non-Latin scripts in terminals that don't support them
1 parent 9b03278 commit 242ed85

8 files changed

Lines changed: 115 additions & 15 deletions

File tree

remove-json-keys/pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ license-files = [
2020
dependencies = [
2121
"colorama>=0.4.6,<1.0.0 ; platform_system == 'Windows'",
2222
"json5>=0.9.0,<1.0.0",
23+
"ucs-detect>=2.0.2,<3.0.0",
2324
]
24-
requires-python = ">=3.6,<4.0"
25+
requires-python = ">=3.8,<4.0"
2526
keywords = [
2627
"cli",
2728
"console",
@@ -38,8 +39,6 @@ classifiers = [
3839
"Natural Language :: English",
3940
"Operating System :: OS Independent",
4041
"Programming Language :: Python :: 3",
41-
"Programming Language :: Python :: 3.6",
42-
"Programming Language :: Python :: 3.7",
4342
"Programming Language :: Python :: 3.8",
4443
"Programming Language :: Python :: 3.9",
4544
"Programming Language :: Python :: 3.10",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
"am",
3+
"ar",
4+
"bn",
5+
"bo",
6+
"ckb",
7+
"dv",
8+
"dz",
9+
"fa",
10+
"gu",
11+
"he",
12+
"hi",
13+
"ja",
14+
"ka",
15+
"km",
16+
"kn",
17+
"ko",
18+
"lo",
19+
"ml",
20+
"my",
21+
"ne",
22+
"pa",
23+
"ps",
24+
"si",
25+
"ta",
26+
"te",
27+
"th",
28+
"ti",
29+
"ur",
30+
"yi",
31+
"zh",
32+
"zh_CN",
33+
"zh_HK",
34+
"zh_SG",
35+
"zh_TW"
36+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
import sys
22

3+
def can_render_non_latin_scripts() -> bool: # e.g. ar, dv, zh
4+
import json, os, subprocess
5+
try:
6+
result = subprocess.run(
7+
['ucs-detect', '--quick', '--save-json', '-'], # to stdout vs. file
8+
capture_output=True, text=True, timeout=0.1
9+
)
10+
return json.loads(result.stdout).get('wide', False)
11+
except (subprocess.TimeoutExpired, json.JSONDecodeError, FileNotFoundError):
12+
return os.environ.get('WT_SESSION') is not None if sys.platform == 'win32' else True
13+
314
def is_debug_mode() -> bool:
415
return any(arg in ('--debug', '-V') for arg in sys.argv[1:])

remove-json-keys/src/remove_json_keys/lib/language.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,21 @@ def get_locales() -> List[str]:
5656
return random_lang
5757

5858
def get_msgs(cli: sn, lang_code: str = 'en') -> sn:
59-
from . import jsdelivr, url
59+
from . import env, jsdelivr, url
6060

6161
lang_code = format_code(lang_code)
6262
if getattr(get_msgs, 'cached', None) and lang_code == get_msgs.cached_lang:
63-
print(get_msgs.cached)
6463
return get_msgs.cached # don't re-fetch same msgs
6564

65+
mod_root_path = Path(__file__).parent.parent
66+
non_latin_locales = data.json.read(mod_root_path / 'assets/data/non_latin_locales.json')
6667
msgs = data.json.flatten(data.json.read( # local ones
67-
Path(__file__).parent.parent / '_locales/en/messages.json'))
68+
mod_root_path / '_locales/en/messages.json'))
6869

69-
if not lang_code.startswith('en'): # fetch non-English msgs from jsDelivr
70+
if (
71+
not lang_code.startswith('en') and
72+
not (lang_code in non_latin_locales and not env.can_render_non_latin_scripts())
73+
): # fetch non-English msgs from jsDelivr
7074
msgs_host_url = f'{jsdelivr.create_commit_url(cli, cli.commit_hashes.locales)}/src/remove_json_keys/_locales/'
7175
msg_href = f'{msgs_host_url}{lang_code}/messages.json'
7276
for attempt in range(3):

translate-messages/pyproject.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ dependencies = [
2121
"colorama>=0.4.6,<1.0.0 ; platform_system == 'Windows'",
2222
"json5>=0.9.0,<1.0.0",
2323
"translate>=3.8.0,<4.0.0",
24+
"ucs-detect>=2.0.2,<3.0.0",
2425
]
25-
requires-python = ">=3.6,<4.0"
26+
requires-python = ">=3.8,<4.0"
2627
keywords = [
2728
"chrome",
2829
"cli",
@@ -48,8 +49,6 @@ classifiers = [
4849
"Natural Language :: English",
4950
"Operating System :: OS Independent",
5051
"Programming Language :: Python :: 3",
51-
"Programming Language :: Python :: 3.6",
52-
"Programming Language :: Python :: 3.7",
5352
"Programming Language :: Python :: 3.8",
5453
"Programming Language :: Python :: 3.9",
5554
"Programming Language :: Python :: 3.10",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
[
2+
"am",
3+
"ar",
4+
"bn",
5+
"bo",
6+
"ckb",
7+
"dv",
8+
"dz",
9+
"fa",
10+
"gu",
11+
"he",
12+
"hi",
13+
"ja",
14+
"ka",
15+
"km",
16+
"kn",
17+
"ko",
18+
"lo",
19+
"ml",
20+
"my",
21+
"ne",
22+
"pa",
23+
"ps",
24+
"si",
25+
"ta",
26+
"te",
27+
"th",
28+
"ti",
29+
"ur",
30+
"yi",
31+
"zh",
32+
"zh_CN",
33+
"zh_HK",
34+
"zh_SG",
35+
"zh_TW"
36+
]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
11
import sys
22

3+
def can_render_non_latin_scripts() -> bool: # e.g. ar, dv, zh
4+
import json, os, subprocess
5+
try:
6+
result = subprocess.run(
7+
['ucs-detect', '--quick', '--save-json', '-'], # to stdout vs. file
8+
capture_output=True, text=True, timeout=0.1
9+
)
10+
return json.loads(result.stdout).get('wide', False)
11+
except (subprocess.TimeoutExpired, json.JSONDecodeError, FileNotFoundError):
12+
return os.environ.get('WT_SESSION') is not None if sys.platform == 'win32' else True
13+
314
def is_debug_mode() -> bool:
415
return any(arg in ('--debug', '-V') for arg in sys.argv[1:])

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,22 @@ def get_locales() -> List[str]:
8888
return random_lang
8989

9090
def get_msgs(cli: sn, lang_code: str = 'en') -> sn:
91-
from . import jsdelivr, url
91+
from . import env, jsdelivr, url
9292

9393
lang_code = format_code(lang_code)
9494
if getattr(get_msgs, 'cached', None) and lang_code == get_msgs.cached_lang:
95-
print(get_msgs.cached)
9695
return get_msgs.cached # don't re-fetch same msgs
9796

97+
mod_root_path = Path(__file__).parent.parent
98+
non_latin_locales = data.json.read(mod_root_path / 'assets/data/non_latin_locales.json')
9899
msgs = data.json.flatten(data.json.read( # local ones
99-
Path(__file__).parent.parent / '_locales/en/messages.json'))
100+
mod_root_path / '_locales/en/messages.json'))
100101

101-
if not lang_code.startswith('en'): # fetch non-English msgs from jsDelivr
102-
msgs_host_url = f'{jsdelivr.create_commit_url(cli, cli.commit_hashes.locales)}/src/translate_messages/_locales/'
102+
if (
103+
not lang_code.startswith('en') and
104+
not (lang_code in non_latin_locales and not env.can_render_non_latin_scripts())
105+
): # fetch non-English msgs from jsDelivr
106+
msgs_host_url = f'{jsdelivr.create_commit_url(cli, cli.commit_hashes.locales)}/src/remove_json_keys/_locales/'
103107
msg_href = f'{msgs_host_url}{lang_code}/messages.json'
104108
for attempt in range(3):
105109
try: # fetch remote msgs

0 commit comments

Comments
 (0)