diff --git a/src/calibre/gui2/tweak_book/__init__.py b/src/calibre/gui2/tweak_book/__init__.py index 621c45eff2..220aeb2167 100644 --- a/src/calibre/gui2/tweak_book/__init__.py +++ b/src/calibre/gui2/tweak_book/__init__.py @@ -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 diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index ba168326b6..68ea275085 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -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: diff --git a/src/calibre/spell/dictionary.py b/src/calibre/spell/dictionary.py index fd5939c58e..acc5516952 100644 --- a/src/calibre/spell/dictionary.py +++ b/src/calibre/spell/dictionary.py @@ -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()