mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Keep track of the current book's locale as a separate global variable for performance
This commit is contained in:
parent
56ef487a5d
commit
01a034b848
@ -10,6 +10,8 @@ import string
|
||||
from future_builtins import map
|
||||
|
||||
from calibre.utils.config import JSONConfig
|
||||
from calibre.spell.dictionary import Dictionaries, parse_lang_code
|
||||
|
||||
tprefs = JSONConfig('tweak_book_gui')
|
||||
d = tprefs.defaults
|
||||
|
||||
@ -69,3 +71,12 @@ class NonReplaceDict(dict):
|
||||
actions = NonReplaceDict()
|
||||
editors = NonReplaceDict()
|
||||
TOP = object()
|
||||
dictionaries = Dictionaries()
|
||||
|
||||
def set_book_locale(lang):
|
||||
try:
|
||||
dictionaries.default_locale = parse_lang_code(lang)
|
||||
if dictionaries.default_locale.langcode == 'und':
|
||||
raise ValueError('')
|
||||
except ValueError:
|
||||
dictionaries.default_locale = dictionaries.ui_locale
|
||||
|
@ -27,7 +27,8 @@ from calibre.ebooks.oeb.polish.toc import remove_names_from_toc, find_existing_t
|
||||
from calibre.ebooks.oeb.polish.utils import link_stylesheets, setup_cssutils_serialization as scs
|
||||
from calibre.gui2 import error_dialog, choose_files, question_dialog, info_dialog, choose_save_file
|
||||
from calibre.gui2.dialogs.confirm_delete import confirm
|
||||
from calibre.gui2.tweak_book import set_current_container, current_container, tprefs, actions, editors
|
||||
from calibre.gui2.tweak_book import (
|
||||
set_current_container, current_container, tprefs, actions, editors, set_book_locale)
|
||||
from calibre.gui2.tweak_book.undo import GlobalUndoHistory
|
||||
from calibre.gui2.tweak_book.file_list import NewFileDialog
|
||||
from calibre.gui2.tweak_book.save import SaveManager, save_container, find_first_existing_ancestor
|
||||
@ -237,6 +238,7 @@ class Boss(QObject):
|
||||
set_current_container(container)
|
||||
with BusyCursor():
|
||||
self.current_metadata = self.gui.current_metadata = container.mi
|
||||
set_book_locale(self.current_metadata.language)
|
||||
self.global_undo.open_book(container)
|
||||
self.gui.update_window_title()
|
||||
self.gui.file_list.current_edited_name = None
|
||||
@ -732,6 +734,7 @@ class Boss(QObject):
|
||||
f.write(ed.data)
|
||||
if name == container.opf_name:
|
||||
container.refresh_mime_map()
|
||||
set_book_locale(container.mi.language)
|
||||
if container is current_container():
|
||||
ed.is_synced_to_container = True
|
||||
if name == container.opf_name:
|
||||
|
@ -34,6 +34,7 @@ def get_codes():
|
||||
return ccodes, ccodemap
|
||||
|
||||
def parse_lang_code(raw):
|
||||
raw = raw or ''
|
||||
parts = raw.replace('_', '-').split('-')
|
||||
lc = canonicalize_lang(parts[0])
|
||||
if lc is None:
|
||||
@ -161,6 +162,7 @@ class Dictionaries(object):
|
||||
self.default_locale = parse_lang_code(get_lang())
|
||||
except ValueError:
|
||||
self.default_locale = parse_lang_code('en-US')
|
||||
self.ui_locale = self.default_locale
|
||||
|
||||
def clear_caches(self):
|
||||
self.dictionaries.clear(), self.word_cache.clear()
|
||||
|
Loading…
x
Reference in New Issue
Block a user