Merge pull request #202 from katafrakt/fix-default-locale

Get default enchant Dict language in more reliable way
This commit is contained in:
Olivier 2017-11-09 11:35:22 +01:00 committed by GitHub
commit d6a874db61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,7 +2,7 @@
# --!-- coding: utf8 --!--
import re
from PyQt5.QtCore import QTimer, QModelIndex, Qt, QEvent, pyqtSignal, QRegExp
from PyQt5.QtCore import QTimer, QModelIndex, Qt, QEvent, pyqtSignal, QRegExp, QLocale
from PyQt5.QtGui import QTextBlockFormat, QTextCharFormat, QFont, QColor, QIcon, QMouseEvent, QTextCursor
from PyQt5.QtWidgets import QWidget, QTextEdit, qApp, QAction, QMenu
@ -76,7 +76,7 @@ class textEditView(QTextEdit):
# Spellchecking
if enchant and self.spellcheck:
try:
self._dict = enchant.Dict(self.currentDict if self.currentDict else enchant.get_default_language())
self._dict = enchant.Dict(self.currentDict if self.currentDict else self.getDefaultLocale())
except enchant.errors.DictNotFoundError:
self.spellcheck = False
@ -87,6 +87,15 @@ class textEditView(QTextEdit):
self.highlighter = basicHighlighter(self)
self.highlighter.setDefaultBlockFormat(self._defaultBlockFormat)
def getDefaultLocale(self):
default_locale = enchant.get_default_language()
if default_locale is None:
default_locale = QLocale.system().name()
if default_locale is None:
default_locale = enchant.list_dicts()[0][0]
return default_locale
def setModel(self, model):
self._model = model
try:
@ -384,7 +393,7 @@ class textEditView(QTextEdit):
if enchant and self.spellcheck and not self._dict:
if self.currentDict:
self._dict = enchant.Dict(self.currentDict)
elif enchant.dict_exists(enchant.get_default_language()):
elif enchant.get_default_language() and enchant.dict_exists(enchant.get_default_language()):
self._dict = enchant.Dict(enchant.get_default_language())
else:
self.spellcheck = False