Skip to content

Commit

Permalink
Update language support
Browse files Browse the repository at this point in the history
  • Loading branch information
Masterain98 committed Oct 4, 2022
1 parent 877567c commit 407ab98
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ cython_debug/
.idea/
/config/*
/dict/*
tester.py
tester.py
35 changes: 32 additions & 3 deletions config/api_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,33 @@
ACCEPTED_LANGUAGES = ["chs", "cht", "de", "en", "es", "fr", "id", "jp", "kr",
"pt", "ru", "th", "vi"]
ACCEPTED_LANGUAGES = ["chs", "cht", "de", "en", "es", "fr", "id", "jp", "kr", "pt", "ru", "th", "vi",
"zh-cn", "zh-tw", "de-de", "en-us", "es-es", "fr-fr", "id-id", "ja-jp", "ko-kr", "pt-pt", "ru-ru",
"th-th", "vi-vn"]
LANGUAGE_PAIRS = {
"chs": "zh-cn",
"cht": "zh-tw",
"de": "de-de",
"en": "en-us",
"es": "es-es",
"fr": "fr-fr",
"id": "id-id",
"jp": "ja-jp",
"kr": "ko-kr",
"pt": "pt-pt",
"ru": "ru-ru",
"th": "th-th",
"vi": "vi-vn",
"zh-cn": "chs",
"zh-tw": "cht",
"de-de": "de",
"en-us": "en",
"es-es": "es",
"fr-fr": "fr",
"id-id": "id",
"ja-jp": "jp",
"ko-kr": "kr",
"pt-pt": "pt",
"ru-ru": "ru",
"th-th": "th",
"vi-vn": "vi"
}
TOKEN = "5XKknUbCQCKq4KDC5HPLheMZ" # change this
DOCS_URL = "/api/v1/docs" # change this
DOCS_URL = "/api/v1/docs" # change this
26 changes: 17 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import os
import requests
from config.api_config import ACCEPTED_LANGUAGES, TOKEN, DOCS_URL
from config.api_config import ACCEPTED_LANGUAGES, TOKEN, DOCS_URL, LANGUAGE_PAIRS

app = FastAPI(docs_url=DOCS_URL, redoc_url=None)
db = MysqlConn()
Expand All @@ -20,6 +20,8 @@ async def translate(lang: str, word: str):
lang = lang.lower()
if lang not in ACCEPTED_LANGUAGES:
raise HTTPException(status_code=403, detail="Language not supported")
if len(lang) == 5:
lang = LANGUAGE_PAIRS[lang]

if word.startswith("[") and word.endswith("]"):
# Make list with original order
Expand Down Expand Up @@ -56,13 +58,15 @@ async def translate_generic(word: str):
raise HTTPException(status_code=404, detail="Hash ID not found")
else:
return {"item_id": result[0][0],
"lang": [result[i][1] for i in range(len(result))]}
"lang": [LANGUAGE_PAIRS[result[i][1]] for i in range(len(result))]}


def make_language_dict_json(lang: str):
lang = lang.lower()
if lang not in ACCEPTED_LANGUAGES:
return False
if len(lang) == 5:
lang = LANGUAGE_PAIRS[lang]
sql = r"SELECT item_id, %s FROM i18n_dict" % lang.lower()
result = db.fetch_all(sql)
if result is None:
Expand All @@ -71,7 +75,7 @@ def make_language_dict_json(lang: str):
try:
os.mkdir("dict")
except FileExistsError:
print("dict folder already exists")
pass
lang_dict = {result[i][1]: result[i][0] for i in range(len(result)) if result[i][1] != ""}
with open("dict/" + lang + ".json", 'w+', encoding='utf-8') as f:
json.dump(lang_dict, f, indent=4, separators=(',', ': '), ensure_ascii=False)
Expand All @@ -82,13 +86,15 @@ async def download_language_dict_json(lang: str):
lang = lang.lower()
if lang not in ACCEPTED_LANGUAGES and lang != "all":
raise HTTPException(status_code=403, detail="Language not supported")
if len(lang) == 5:
lang = LANGUAGE_PAIRS[lang]

if os.path.exists("dict/" + lang + ".json"):
return FileResponse(path="dict/" + lang + ".json", filename=lang + ".json", media_type="application/json")
return FileResponse(path="dict/" + lang + ".json", filename=LANGUAGE_PAIRS[lang] + ".json", media_type="application/json")
else:
make_dict_result = make_language_dict_json(lang)
if make_dict_result:
return FileResponse(path="dict/" + lang + ".json", filename=lang + ".json", media_type="application/json")
return FileResponse(path="dict/" + lang + ".json", filename=LANGUAGE_PAIRS[lang] + ".json", media_type="application/json")
else:
raise HTTPException(status_code=400, detail="Failed to create dictionary, please try again later")

Expand Down Expand Up @@ -227,15 +233,17 @@ def force_refresh_local_data():

# Make dict files for each language
for language in ACCEPTED_LANGUAGES:
make_language_dict_json(language)
if len(language) != 5:
make_language_dict_json(language)
print("Successfully made dict file for %s" % language)

# Make a dict for all languages
all_language_dict = {}
for language in ACCEPTED_LANGUAGES:
this_lang_dict = json.loads(open("dict/" + language + ".json", "r", encoding="utf-8").read())
all_language_dict[language] = this_lang_dict
print("Loaded " + language + " dict")
if len(language) != 5:
this_lang_dict = json.loads(open("dict/" + language + ".json", "r", encoding="utf-8").read())
all_language_dict[language] = this_lang_dict
print("Loaded " + language + " dict")
open("dict/all.json", "w", encoding="utf-8").write(json.dumps(all_language_dict, ensure_ascii=False, indent=4))
print("Successfully generated dict/all.json")

Expand Down

0 comments on commit 407ab98

Please sign in to comment.