Musicreater/languages/lang.py

199 lines
6.3 KiB
Python
Raw Normal View History

2022-01-27 13:21:25 +00:00
# -*- coding:utf-8 -*-
'''对于音·创的语言支持兼语言文件编辑器'''
2022-01-27 13:21:25 +00:00
"""
Copyright 2022 Team-Ryoun
Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
2022-01-27 13:21:25 +00:00
DEFAULTLANGUAGE = 'zh-CN'
LANGUAGELIST = {
# 第一个是语言的中文名称和地区
# 第二个是语言的英文名称和地区
# 第三个是语言的本地名称和地区
2022-01-27 13:21:25 +00:00
'zh-CN': (
"简体中文 中国大陆",
"Simplified Chinese, Chinese Mainland",
"简体中文 中国大陆",
2022-01-27 13:21:25 +00:00
),
'zh-TW': (
"繁体中文 台湾省",
"Traditional Chinese, Taiwan Province",
"正體中文,台灣省",
),
# 'zh-HK': (
2022-03-17 08:21:17 +00:00
# "繁体中文 香港",
# "Traditional Chinese, the Hong Kong Special Administrative Region",
# "繁體中文,香港特別行政區",
# ),
# 'zh-MO': (
2022-03-17 08:21:17 +00:00
# "繁体中文 澳门",
# "Traditional Chinese, the Macao Special Administrative Region",
# "繁體中文,澳門特別行政區",
# ),
2022-01-27 13:21:25 +00:00
'en-GB': (
"英语 英国",
"British English, the United Kingdom",
"British English, the United Kingdom",
2022-01-27 13:21:25 +00:00
),
}
# 对于旧版本音·创的语言支持
# 重构之后将停止使用
2022-03-16 08:21:10 +00:00
try:
from languages.zhCN import READABLETEXT
except:
pass
2022-03-13 14:29:57 +00:00
2022-03-17 08:21:17 +00:00
2022-03-23 17:01:45 +00:00
from msctLib.log import log
2022-03-20 03:20:59 +00:00
def __loadLanguage(languageFilename: str):
with open(languageFilename, 'r', encoding='utf-8') as languageFile:
2022-03-17 08:21:17 +00:00
_text = {}
for line in languageFile:
2022-03-23 17:01:45 +00:00
if line.startswith('#'):
continue
2022-03-20 03:20:59 +00:00
line = line.split(' ', 1)
_text[line[0]] = line[1].replace('\n', '')
2022-03-17 08:21:17 +00:00
langkeys = _text.keys()
2022-03-20 03:20:59 +00:00
with open(languageFilename.replace(languageFilename[-10:-5], 'zh-CN'), 'r', encoding='utf-8') as defaultLangFile:
2022-03-17 08:21:17 +00:00
for line in defaultLangFile:
2022-03-23 17:01:45 +00:00
if line.startswith('#'):
continue
2022-03-20 03:20:59 +00:00
line = line.split(' ', 1)
2022-03-17 08:21:17 +00:00
if not line[0] in langkeys:
2022-03-20 03:20:59 +00:00
_text[line[0]] = line[1].replace('\n', '')
2022-03-17 08:21:17 +00:00
from msctLib.log import log
2022-03-20 03:20:59 +00:00
log(f'丢失对于 {line[0]} 的本地化文本', 'WARRING')
2022-03-17 08:21:17 +00:00
langkeys = _text.keys()
return _text
2022-03-13 14:29:57 +00:00
if not DEFAULTLANGUAGE == 'zh-CN':
if DEFAULTLANGUAGE in LANGUAGELIST.keys():
2022-03-20 03:20:59 +00:00
_TEXT = __loadLanguage('./languages/' + DEFAULTLANGUAGE + '.lang')
2022-03-13 14:29:57 +00:00
else:
raise KeyError(f'无法打开默认语言{DEFAULTLANGUAGE}')
2022-03-20 03:20:59 +00:00
def wordTranslate(singleWord: str, debug: bool = False):
2022-03-13 14:29:57 +00:00
import requests
try:
2022-03-20 03:20:59 +00:00
return \
requests.post('https://fanyi.baidu.com/sug', data={'kw': f'{singleWord}'}).json()['data'][0]['v'].split(
'; ')[0]
2022-03-13 14:29:57 +00:00
except:
2022-03-20 03:20:59 +00:00
log(f"无法翻译文本{singleWord}", level='WARRING', isPrinted=debug)
2022-03-13 14:29:57 +00:00
return None
2022-03-20 03:20:59 +00:00
def _(text: str, debug: bool = False):
2022-03-13 14:29:57 +00:00
try:
return _TEXT[text]
except:
2022-03-17 08:21:17 +00:00
if debug:
raise KeyError(f'无法找到翻译文本{text}')
else:
2022-03-23 17:01:45 +00:00
log(f'无法找到本地化文本{text}','ERROR')
return ''
2022-03-13 14:29:57 +00:00
if __name__ == '__main__':
# 启动语言编辑器
import tkinter as tk
2022-03-17 08:21:17 +00:00
from tkinter.filedialog import askopenfilename as askfilen
2022-03-20 03:20:59 +00:00
2022-03-17 08:21:17 +00:00
LANGNAME = _('LANGLOCALNAME')
2022-03-20 03:20:59 +00:00
2022-03-17 08:21:17 +00:00
def _changeDefaultLang():
global _TEXT
global DEFAULTLANGUAGE
fileName = askfilen(title='选择所翻译的语言文件', initialdir=r'./',
2022-03-20 03:20:59 +00:00
filetypes=[('音·创语言文件', '.lang'), ('所有文件', '*')],
defaultextension='.lang',
initialfile='.lang')
2022-03-17 08:21:17 +00:00
_TEXT = __loadLanguage(fileName)
DEFAULTLANGUAGE = _('LANGKEY')
LANGNAME = _('LANGLOCALNAME')
2022-03-17 08:21:17 +00:00
orignText = ''
transText = ''
2022-03-20 03:20:59 +00:00
for i, j in _TEXT.items():
orignText += i + '\n'
transText += j + '\n'
2022-03-17 08:21:17 +00:00
Origntextbar.insert('end', orignText)
Translatetextbar.insert('end', transText)
global setlangbutton
setlangbutton['text'] = f'对标语言{LANGNAME}'
2022-03-20 03:20:59 +00:00
def _autoSave(event=None):
with open('autosave.tmp.txt', 'w', encoding='utf-8') as f:
f.write(Translatetextbar.get(1.0, 'end'))
print(str(event))
2022-03-20 03:20:59 +00:00
root = tk.Tk()
2022-03-17 08:21:17 +00:00
root.geometry('600x500')
2022-03-20 03:20:59 +00:00
root.bind("<Motion>", _autoSave)
2022-03-16 08:21:10 +00:00
2022-03-20 03:20:59 +00:00
nowText = ''
2022-03-16 08:21:10 +00:00
2022-03-20 03:20:59 +00:00
Orignrame = tk.Frame(root, bd=2)
Translaterame = tk.Frame(root, bd=2)
2022-03-16 08:21:10 +00:00
2022-03-17 08:21:17 +00:00
Orignscrollbar = tk.Scrollbar(Orignrame)
2022-03-20 03:20:59 +00:00
Origntextbar = tk.Text(Orignrame, width=35, height=40)
2022-03-16 08:21:10 +00:00
2022-03-20 03:20:59 +00:00
Translatetextbar = tk.Text(Translaterame, width=40, height=37, undo=True)
2022-03-16 08:21:10 +00:00
Translatescrollbar = tk.Scrollbar(Translaterame)
2022-03-17 08:21:17 +00:00
2022-03-20 03:20:59 +00:00
def ctrlZ():
Translatetextbar.edit_undo()
Translatetextbar.bind("<Control-z>", ctrlZ)
def ctrlY():
Translatetextbar.edit_redo()
Translatetextbar.bind("<Control-y>", ctrlY)
tk.Button(Translaterame, text='保存', command=_autoSave).pack(side='bottom', fill='x')
2022-03-16 08:21:10 +00:00
2022-03-20 03:20:59 +00:00
tk.Label(Orignrame, text='中文原文').pack(side='top')
2022-03-16 08:21:10 +00:00
Origntextbar.pack(side='left', fill='y')
Orignscrollbar.pack(side='left', fill='y')
2022-03-20 03:20:59 +00:00
setlangbutton = tk.Button(Translaterame, text=f'对标语言{LANGNAME}', command=_changeDefaultLang)
setlangbutton.pack(side='top')
2022-03-16 08:21:10 +00:00
Translatescrollbar.pack(side='right', fill='y')
Translatetextbar.pack(side='right', fill='y')
Orignscrollbar.config(command=Origntextbar.yview)
Origntextbar.config(yscrollcommand=Orignscrollbar.set)
Translatescrollbar.config(command=Translatetextbar.yview)
Translatetextbar.config(yscrollcommand=Translatescrollbar.set)
2022-03-17 08:21:17 +00:00
Orignrame.pack(side='left')
Translaterame.pack(side='right')
2022-03-16 08:21:10 +00:00
2022-03-17 08:21:17 +00:00
tk.mainloop()