Add translations for the viewer interface

This commit is contained in:
Kovid Goyal 2019-08-07 12:37:51 +05:30
parent 0c7bcb0f8c
commit 4f486558de
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 27 additions and 18 deletions

View File

@ -26,6 +26,7 @@ from calibre.gui2.webengine import (
Bridge, RestartingWebEngineView, create_script, from_js, insert_scripts,
secure_webengine, to_js
)
from calibre.srv.code import get_translations_data
from calibre.utils.config import JSONConfig
from polyglot.builtins import iteritems
@ -155,6 +156,9 @@ def create_profile():
from calibre.utils.rapydscript import compile_viewer
compile_viewer()
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))
url_handler = UrlSchemeHandler(ans)
ans.installUrlSchemeHandler(QByteArray(FAKE_PROTOCOL.encode('ascii')), url_handler)

View File

@ -9,7 +9,7 @@ import random
import shutil
import sys
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 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 ''
_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():
global _cached_translations
if _cached_translations is None:
_cached_translations = False
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:
_cached_translations = load_json_file(zf.open(lang, 'r'))
return _cached_translations
if not hasattr(get_translations, 'cached'):
get_translations.cached = False
data = get_translations_data()
if data:
get_translations.cached = json_loads(data)
return get_translations.cached
def custom_list_template():

View File

@ -4,7 +4,7 @@ from __python__ import bound_methods, hash_literals
import traceback
from elementmaker import E
from gettext import gettext as _
from gettext import gettext as _, install
import initialize # noqa: unused-import
from ajax import ajax
@ -238,6 +238,8 @@ def update_font_size():
if window is window.top:
# main
if window.calibre_translations_data:
install(window.calibre_translations_data)
ui_operations.get_file = get_file
ui_operations.get_mathjax_files = get_mathjax_files
ui_operations.update_url_state = update_url_state
@ -262,3 +264,4 @@ if window is window.top:
else:
# iframe
iframe_main()
window.calibre_translations_data = v'undefined'