支持动态语言更新界面

This commit is contained in:
Harry Chen 2024-08-30 06:15:28 +00:00
parent f35f6e9b5e
commit a2f625afd9
3 changed files with 40 additions and 13 deletions

View File

@ -33,3 +33,4 @@ ko_pron
opencc; sys_platform != 'linux' opencc; sys_platform != 'linux'
opencc==1.1.1; sys_platform == 'linux' opencc==1.1.1; sys_platform == 'linux'
python_mecab_ko; sys_platform != 'win32' python_mecab_ko; sys_platform != 'win32'
gradio-i18n

View File

@ -2,35 +2,60 @@ import json
import locale import locale
import os import os
I18N_JSON_DIR : os.PathLike = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'locale') import gradio_i18n
I18N_JSON_DIR: os.PathLike = os.path.join(
os.path.dirname(os.path.relpath(__file__)), "locale"
)
def load_language_list(language): def load_language_list(language):
with open(os.path.join(I18N_JSON_DIR, f"{language}.json"), "r", encoding="utf-8") as f: with open(
os.path.join(I18N_JSON_DIR, f"{language}.json"), "r", encoding="utf-8"
) as f:
language_list = json.load(f) language_list = json.load(f)
return language_list return language_list
def scan_language_list(): def scan_language_list():
language_list = [] language_list = []
for name in os.listdir(I18N_JSON_DIR): for name in os.listdir(I18N_JSON_DIR):
if name.endswith(".json"):language_list.append(name.split('.')[0]) if name.endswith(".json"):
language_list.append(name.split(".")[0])
return language_list return language_list
class I18nAuto: class I18nAuto:
def __init__(self, language=None): def __init__(self, language=None):
if language in ["Auto", None]: if language in ["Auto", None] or not os.path.exists(
language = locale.getdefaultlocale()[0] os.path.join(I18N_JSON_DIR, f"{language}.json")
# getlocale can't identify the system's language ((None, None)) ):
if not os.path.exists(os.path.join(I18N_JSON_DIR, f"{language}.json")): self.language = "Auto"
language = "en_US" else:
self.language = language self.language = language
self.language_map = load_language_list(language) self.language_map = load_language_list(language)
def __call__(self, key): def __call__(self, key):
return self.language_map.get(key, key) if self.language == "Auto":
return gradio_i18n.gettext(key)
else:
return self.language_map.get(key, key)
def __repr__(self): def __repr__(self):
return "Use Language: " + self.language return "Use Language: " + self.language
def translate_blocks(self, block):
translation = {}
for lang in scan_language_list():
short_lang = lang.partition("_")[0]
if short_lang == "zh" and "CN" not in lang:
continue
translation[short_lang] = load_language_list(lang)
gradio_i18n.translate_blocks(block, translation)
if __name__ == "__main__": if __name__ == "__main__":
i18n = I18nAuto(language='en_US') i18n = I18nAuto(language="en_US")
print(i18n) print(i18n)

View File

@ -1045,6 +1045,7 @@ with gr.Blocks(title="GPT-SoVITS WebUI") as app:
close_tts.click(change_tts_inference, [bert_pretrained_dir,cnhubert_base_dir,gpu_number_1C,GPT_dropdown,SoVITS_dropdown, batched_infer_enabled], [tts_info,open_tts,close_tts]) close_tts.click(change_tts_inference, [bert_pretrained_dir,cnhubert_base_dir,gpu_number_1C,GPT_dropdown,SoVITS_dropdown, batched_infer_enabled], [tts_info,open_tts,close_tts])
version_checkbox.change(switch_version,[version_checkbox],[pretrained_s2G,pretrained_s2D,pretrained_s1,GPT_dropdown,SoVITS_dropdown]) version_checkbox.change(switch_version,[version_checkbox],[pretrained_s2G,pretrained_s2D,pretrained_s1,GPT_dropdown,SoVITS_dropdown])
with gr.TabItem(i18n("2-GPT-SoVITS-变声")):gr.Markdown(value=i18n("施工中,请静候佳音")) with gr.TabItem(i18n("2-GPT-SoVITS-变声")):gr.Markdown(value=i18n("施工中,请静候佳音"))
i18n.translate_blocks(app)
app.queue().launch(#concurrency_count=511, max_size=1022 app.queue().launch(#concurrency_count=511, max_size=1022
server_name="0.0.0.0", server_name="0.0.0.0",
inbrowser=True, inbrowser=True,