mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add translations for the viewer interface
This commit is contained in:
parent
0c7bcb0f8c
commit
4f486558de
@ -26,6 +26,7 @@ from calibre.gui2.webengine import (
|
|||||||
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
|
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
|
||||||
secure_webengine, to_js
|
secure_webengine, to_js
|
||||||
)
|
)
|
||||||
|
from calibre.srv.code import get_translations_data
|
||||||
from calibre.utils.config import JSONConfig
|
from calibre.utils.config import JSONConfig
|
||||||
from polyglot.builtins import iteritems
|
from polyglot.builtins import iteritems
|
||||||
|
|
||||||
@ -155,6 +156,9 @@ def create_profile():
|
|||||||
from calibre.utils.rapydscript import compile_viewer
|
from calibre.utils.rapydscript import compile_viewer
|
||||||
compile_viewer()
|
compile_viewer()
|
||||||
js = P('viewer.js', data=True, allow_user_override=False)
|
js = P('viewer.js', data=True, allow_user_override=False)
|
||||||
|
translations_json = get_translations_data()
|
||||||
|
if translations_json:
|
||||||
|
js = (b'window.calibre_translations_data = %s;\n\n' % translations_json) + js
|
||||||
insert_scripts(ans, create_script('viewer.js', js))
|
insert_scripts(ans, create_script('viewer.js', js))
|
||||||
url_handler = UrlSchemeHandler(ans)
|
url_handler = UrlSchemeHandler(ans)
|
||||||
ans.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler)
|
ans.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler)
|
||||||
|
@ -9,7 +9,7 @@ import random
|
|||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import zipfile
|
import zipfile
|
||||||
from json import load as load_json_file
|
from json import load as load_json_file, loads as json_loads
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
|
|
||||||
from calibre import as_unicode
|
from calibre import as_unicode
|
||||||
@ -95,25 +95,27 @@ def get_basic_query_data(ctx, rd):
|
|||||||
return library_id, db, sorts, orders, rd.query.get('vl') or ''
|
return library_id, db, sorts, orders, rd.query.get('vl') or ''
|
||||||
|
|
||||||
|
|
||||||
_cached_translations = None
|
def get_translations_data():
|
||||||
|
with zipfile.ZipFile(
|
||||||
|
P('content-server/locales.zip', allow_user_override=False), 'r'
|
||||||
|
) as zf:
|
||||||
|
names = set(zf.namelist())
|
||||||
|
lang = get_lang()
|
||||||
|
if lang not in names:
|
||||||
|
xlang = lang.split('_')[0].lower()
|
||||||
|
if xlang in names:
|
||||||
|
lang = xlang
|
||||||
|
if lang in names:
|
||||||
|
return zf.open(lang, 'r').read()
|
||||||
|
|
||||||
|
|
||||||
def get_translations():
|
def get_translations():
|
||||||
global _cached_translations
|
if not hasattr(get_translations, 'cached'):
|
||||||
if _cached_translations is None:
|
get_translations.cached = False
|
||||||
_cached_translations = False
|
data = get_translations_data()
|
||||||
with zipfile.ZipFile(
|
if data:
|
||||||
P('content-server/locales.zip', allow_user_override=False), 'r'
|
get_translations.cached = json_loads(data)
|
||||||
) as zf:
|
return get_translations.cached
|
||||||
names = set(zf.namelist())
|
|
||||||
lang = get_lang()
|
|
||||||
if lang not in names:
|
|
||||||
xlang = lang.split('_')[0].lower()
|
|
||||||
if xlang in names:
|
|
||||||
lang = xlang
|
|
||||||
if lang in names:
|
|
||||||
_cached_translations = load_json_file(zf.open(lang, 'r'))
|
|
||||||
return _cached_translations
|
|
||||||
|
|
||||||
|
|
||||||
def custom_list_template():
|
def custom_list_template():
|
||||||
|
@ -4,7 +4,7 @@ from __python__ import bound_methods, hash_literals
|
|||||||
|
|
||||||
import traceback
|
import traceback
|
||||||
from elementmaker import E
|
from elementmaker import E
|
||||||
from gettext import gettext as _
|
from gettext import gettext as _, install
|
||||||
|
|
||||||
import initialize # noqa: unused-import
|
import initialize # noqa: unused-import
|
||||||
from ajax import ajax
|
from ajax import ajax
|
||||||
@ -238,6 +238,8 @@ def update_font_size():
|
|||||||
|
|
||||||
if window is window.top:
|
if window is window.top:
|
||||||
# main
|
# main
|
||||||
|
if window.calibre_translations_data:
|
||||||
|
install(window.calibre_translations_data)
|
||||||
ui_operations.get_file = get_file
|
ui_operations.get_file = get_file
|
||||||
ui_operations.get_mathjax_files = get_mathjax_files
|
ui_operations.get_mathjax_files = get_mathjax_files
|
||||||
ui_operations.update_url_state = update_url_state
|
ui_operations.update_url_state = update_url_state
|
||||||
@ -262,3 +264,4 @@ if window is window.top:
|
|||||||
else:
|
else:
|
||||||
# iframe
|
# iframe
|
||||||
iframe_main()
|
iframe_main()
|
||||||
|
window.calibre_translations_data = v'undefined'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user