add auto translate
This commit is contained in:
39
.github/auto_translator.py
vendored
Normal file
39
.github/auto_translator.py
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
import i18n
|
||||
import json
|
||||
from deep_translator import GoogleTranslator
|
||||
import sys
|
||||
|
||||
|
||||
def get_path(lang):
|
||||
if lang == 'en':
|
||||
return f'assets/translations/strings.i18n.json'
|
||||
return f'assets/translations/strings_{lang}.i18n.json'
|
||||
|
||||
|
||||
def recursive_translate(src, dst, translator):
|
||||
for sk, sv in src.items():
|
||||
if type(sv) == str and sk not in dst:
|
||||
dst[sk] = translator.translate(sv)
|
||||
if not dst[sk]:
|
||||
del dst[sk]
|
||||
else:
|
||||
if sk not in dst:
|
||||
dst[sk] = {}
|
||||
recursive_translate(sv, dst[sk], translator)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
src = sys.argv[1]
|
||||
dst = sys.argv[2]
|
||||
|
||||
src_file = get_path(src)
|
||||
dst_file = get_path(dst)
|
||||
with open(src_file) as sf:
|
||||
src_pofile = json.load(sf)
|
||||
with open(dst_file) as df:
|
||||
dst_pofile = json.load(df)
|
||||
translator = GoogleTranslator(source=src, target=dst if dst != 'zh' else "zh-CN")
|
||||
recursive_translate(src_pofile, dst_pofile, translator)
|
||||
|
||||
with open(dst_file, 'w') as df:
|
||||
json.dump(dst_pofile, df)
|
||||
47
.github/sync_translate.sh
vendored
Normal file
47
.github/sync_translate.sh
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
key="5Tqp1dLHQSk98s-twNF6RpwZu7lZSLLM"
|
||||
wget -O assets/translations/strings.i18n.json "https://localise.biz/api/export/locale/en-US.json?index=id&key=$key"
|
||||
wget -O assets/translations/strings_fa.i18n.json "https://localise.biz/api/export/locale/fa.json?index=id&key=$key"
|
||||
wget -O assets/translations/strings_zh.i18n.json "https://localise.biz/api/export/locale/zh.json?index=id&key=$key"
|
||||
wget -O assets/translations/strings_pt.i18n.json "https://localise.biz/api/export/locale/pt.json?index=id&key=$key"
|
||||
wget -O assets/translations/strings_ru.i18n.json "https://localise.biz/api/export/locale/ru.json?index=id&key=$key"
|
||||
|
||||
|
||||
pip install polib deep-translator python-i18n
|
||||
|
||||
python3 auto_translate.py fa en
|
||||
python3 auto_translate.py en fa
|
||||
python3 auto_translate.py en zh
|
||||
python3 auto_translate.py en pt
|
||||
|
||||
|
||||
|
||||
function update_localise(){
|
||||
lang=$1
|
||||
pat="assets/translations/strings_${lang}.i18n.json"
|
||||
if [[ $lang == 'en' ]];then
|
||||
pat="assets/translations/strings.i18n.json"
|
||||
fi
|
||||
curl "https://localise.biz/api/import/json?index=id&delete-absent=false&ignore-existing=false&locale=$lang&flag-new=Provisional&key=$LOCALIZ_KEY" \
|
||||
-H 'Accept: application/json' \
|
||||
--data-binary $pat \
|
||||
--compressed
|
||||
}
|
||||
|
||||
function update_localise2(){
|
||||
lang=$1
|
||||
pat="assets/translations/strings_${lang}.i18n.json"
|
||||
if [[ $lang == 'en' ]];then
|
||||
pat="assets/translations/strings.i18n.json"
|
||||
fi
|
||||
curl "https://localise.biz/api/import/json?index=id&delete-absent=false&ignore-existing=false&locale=$lang&flag-new=Provisional&key=$LOCALIZ_KEY2" \
|
||||
-H 'Accept: application/json' \
|
||||
--data-binary $pat \
|
||||
--compressed
|
||||
}
|
||||
|
||||
update_localise fa
|
||||
update_localise en
|
||||
update_localise zh
|
||||
|
||||
update_localise2 en
|
||||
update_localise2 pt
|
||||
Reference in New Issue
Block a user