Implement translations for the new content server

This commit is contained in:
Kovid Goyal 2016-03-03 22:33:23 +05:30
parent fb3290d917
commit 543482ae65
4 changed files with 43 additions and 2 deletions

1
.gitignore vendored
View File

@ -21,6 +21,7 @@ resources/template-functions.json
resources/editor-functions.json
resources/user-manual-translation-stats.json
resources/content-server/main.js
resources/content-server/locales.zip
resources/mozilla-ca-certs.pem
icons/icns/*.iconset
setup/installer/windows/calibre/build.log

View File

@ -215,6 +215,7 @@ class Translations(POT): # {{{
return locale, os.path.join(self.DEST, locale, 'messages.mo')
def run(self, opts):
self.compile_content_server_translations()
l = {}
exec(compile(open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lc_data.py'))
.read(), os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lc_data.py'), 'exec'), l, l)
@ -258,6 +259,25 @@ class Translations(POT): # {{{
self.freeze_locales()
self.compile_user_manual_translations()
def compile_content_server_translations(self):
self.info('\nCompiling content-server translations')
from calibre.utils.rapydscript import msgfmt
from calibre.utils.zipfile import ZipFile, ZIP_DEFLATED, ZipInfo
with ZipFile(self.j(self.RESOURCES, 'content-server', 'locales.zip'), 'w', ZIP_DEFLATED) as zf:
for src in glob.glob(os.path.join(self.TRANSLATIONS, 'content-server', '*.po')):
with open(src, 'rb') as f:
po_data = f.read().decode('utf-8')
data = json.loads(msgfmt(po_data))
translated_entries = {k:v for k, v in data['entries'].iteritems() if v and sum(map(len, v))}
data['entries'] = translated_entries
if translated_entries:
raw = json.dumps(data, ensure_ascii=False, sort_keys=True)
if isinstance(raw, type(u'')):
raw = raw.encode('utf-8')
zi = ZipInfo(os.path.basename(src).rpartition('.')[0])
zi.compress_type = ZIP_DEFLATED
zf.writestr(zi, raw)
def check_iso639(self, path):
from calibre.utils.localization import langnames_to_langcodes
with open(path, 'rb') as f:

View File

@ -4,7 +4,7 @@
from __future__ import (unicode_literals, division, absolute_import,
print_function)
import re, hashlib, random
import re, hashlib, random, zipfile
from functools import partial
from threading import Lock
from json import load as load_json_file, dumps as json_dumps
@ -19,6 +19,7 @@ from calibre.srv.routes import endpoint, json
from calibre.srv.utils import get_library_data, get_use_roman
from calibre.utils.config import prefs, tweaks
from calibre.utils.icu import sort_key
from calibre.utils.localization import get_lang
from calibre.utils.search_query_parser import ParseException
html_cache = {}
@ -81,6 +82,22 @@ def get_basic_query_data(ctx, rd):
sorts, orders = ['timestamp'], ['desc']
return library_id, db, sorts, orders
_cached_translations = None
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
DEFAULT_NUMBER_OF_BOOKS = 50
@ -100,6 +117,7 @@ def interface_data(ctx, rd):
'gui_timestamp_display_format':tweaks['gui_timestamp_display_format'],
'gui_last_modified_display_format':tweaks['gui_last_modified_display_format'],
'use_roman_numerals_for_series_number': get_use_roman(),
'translations': get_translations(),
}
ans['library_map'], ans['default_library'] = ctx.library_info(rd)
ud = {}

View File

@ -3,7 +3,7 @@
from ajax import ajax
from elementmaker import E
from gettext import gettext as _
from gettext import gettext as _, install
from session import UserSessionData
from utils import parse_url_params
@ -15,6 +15,8 @@ def on_library_loaded(end_type, xhr, ev):
p.parentNode.removeChild(p)
if end_type == 'load':
interface_data = JSON.parse(xhr.responseText)
if interface_data.translations:
install(interface_data.translations)
sd = UserSessionData(interface_data['username'], interface_data['user_session_data'])
set_session_data(sd)
Boss(interface_data)