-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathadd_language.py
More file actions
186 lines (150 loc) · 5.17 KB
/
add_language.py
File metadata and controls
186 lines (150 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
import argparse
import json
import logging
import os
import sys
from typing import Any
from typing import Dict
from typing import Optional
try:
import langcodes
import pycountry
except ImportError:
print("Required libraries not found. Installing them now...")
import subprocess
subprocess.check_call(
[sys.executable, "-m", "pip", "install", "langcodes[data]", "pycountry"]
)
import langcodes
import pycountry
def load_json_file(file_path: str) -> Dict[str, Any]:
"""Load JSON data from a file."""
try:
with open(file_path, "r", encoding="utf-8") as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"Error loading file: {e}")
return {}
def save_json_file(data: Dict[str, Any], file_path: str) -> None:
"""Save JSON data to a file with proper formatting."""
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, ensure_ascii=False, indent=2, sort_keys=True)
print(f"JSON data saved to {file_path}")
def _get_pycountry_language(code):
language = pycountry.languages.get(alpha_2=code) or pycountry.languages.get(
alpha_3=code
)
if not language:
return {}
data = {
"code": language.alpha_2 if hasattr(language, "alpha_2") else language.alpha_3,
"name": language.name,
}
if hasattr(language, "common_name"):
data["native_name"] = language.common_name
return data
RTL_SCRIPTS = {"Arab", "Hebr", "Thaa", "Syrc", "Mand", "Samr", "Nkoo"}
def get_language_info(query: str) -> Optional[Dict[str, str]]:
"""Get language information using langcodes and pycountry."""
# Try to parse as a language code first
data = {}
try:
lang = langcodes.get(query) or langcodes.find(query)
except LookupError:
lang = None
if not lang:
return _get_pycountry_language(query) or None
data["code"] = lang.to_tag()
# Get the language name in English
data["name"] = lang.display_name()
# Try to get the native name
data["native_name"] = lang.autonym() or data["name"]
if data["native_name"] == data["name"]:
data.update(_get_pycountry_language(data["code"]))
# Detect RTL from script
try:
script = lang.maximize().script
if script in RTL_SCRIPTS:
data["rtl"] = True
except Exception:
logging.warning("Could not detect script direction for %s", query)
return data
def add_language(data, query, confirm=False):
"""Look up a language and add it to the data dict.
Args:
data: language dictionary to update
query: language name or code to look up
confirm: if True, prompt the user before adding
Returns True if the language was added, False otherwise.
"""
language_info = get_language_info(query)
if not language_info:
print(f"Language not found: {query}")
return False
code = language_info["code"]
name = language_info["name"]
native_name = language_info["native_name"]
print(f"\nFound language: {code} - {name} ({native_name})")
if code in data:
print(
f"Warning: '{code}' already exists: "
f"{data[code].get('name')} ({data[code].get('native_name')})"
)
if confirm and input("Add this language? (Y/N): ").lower() != "y":
print("Language not added.")
return False
entry = {"name": name, "native_name": native_name}
if language_info.get("rtl"):
entry["rtl"] = True
data[code] = entry
print(f"Added language: {name}")
return True
def main():
parser = argparse.ArgumentParser(
description="Add languages to le_utils languagelookup.json"
)
parser.add_argument(
"languages",
nargs="*",
help="Language names or codes to add (non-interactive mode)",
)
args = parser.parse_args()
# Get file path
file_path = os.path.join(
os.path.dirname(__file__), "../le_utils/resources/languagelookup.json"
)
# Load existing data
data = load_json_file(file_path)
if not data:
print("Starting with an empty language dictionary.")
data = {}
else:
print(f"Loaded {len(data)} language entries.")
added = 0
if args.languages:
# Non-interactive: add all specified languages and save
for query in args.languages:
if add_language(data, query):
added += 1
else:
while True:
query = input(
"\nEnter language name or code to add (or press Enter to finish): "
)
if not query:
break
if add_language(data, query, confirm=True):
added += 1
if input("\nSave changes to the JSON file? (Y/N): ").lower() != "y":
return
if added:
save_json_file(data, file_path)
print(f"\nAdded {added} language(s).")
else:
print("\nNo languages were added.")
if __name__ == "__main__":
print("Language Manager: Add new languages to your JSON file")
print("---------------------------------------------------")
main()
print("Program finished.")