mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Implement mapping of locales to dictionary names
This commit is contained in:
parent
fd4db9e8ed
commit
b16b5535bf
@ -133,6 +133,8 @@ def find_tests(which_tests=None):
|
||||
a(find_tests())
|
||||
from calibre.gui2.viewer.convert_book import find_tests
|
||||
a(find_tests())
|
||||
from calibre.utils.hyphenation.test_hyphenation import find_tests
|
||||
a(find_tests())
|
||||
if iswindows:
|
||||
from calibre.utils.windows.wintest import find_tests
|
||||
a(find_tests())
|
||||
|
@ -3,3 +3,42 @@
|
||||
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import json
|
||||
|
||||
from calibre.utils.localization import lang_as_iso639_1
|
||||
from polyglot.builtins import iteritems
|
||||
from polyglot.functools import lru_cache
|
||||
|
||||
|
||||
def locale_map():
|
||||
ans = getattr(locale_map, 'ans', None)
|
||||
if ans is None:
|
||||
ans = locale_map.ans = {k.lower(): v for k, v in iteritems(json.loads(P('hyphenation/locales.json', data=True)))}
|
||||
return ans
|
||||
|
||||
|
||||
@lru_cache()
|
||||
def dictionary_name_for_locale(loc):
|
||||
loc = loc.lower().replace('-', '_')
|
||||
lmap = locale_map()
|
||||
if loc in lmap:
|
||||
return lmap[loc]
|
||||
parts = loc.split('_')
|
||||
if len(parts) > 2:
|
||||
loc = '_'.join(parts[:2])
|
||||
if loc in lmap:
|
||||
return lmap[loc]
|
||||
loc = lang_as_iso639_1(parts[0])
|
||||
if not loc:
|
||||
return
|
||||
if loc in lmap:
|
||||
return lmap[loc]
|
||||
if loc == 'en':
|
||||
return lmap['en_us']
|
||||
if loc == 'de':
|
||||
return lmap['de_de']
|
||||
q = loc + '_'
|
||||
for k, v in iteritems(lmap):
|
||||
if k.startswith(q):
|
||||
return lmap[k]
|
||||
|
26
src/calibre/utils/hyphenation/test_hyphenation.py
Normal file
26
src/calibre/utils/hyphenation/test_hyphenation.py
Normal file
@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env python2
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPL v3 Copyright: 2019, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||
|
||||
import unittest
|
||||
from calibre.utils.hyphenation.dictionaries import dictionary_name_for_locale
|
||||
|
||||
|
||||
class TestHyphenation(unittest.TestCase):
|
||||
|
||||
ae = unittest.TestCase.assertEqual
|
||||
|
||||
def test_locale_to_hyphen_dictionary(self):
|
||||
def t(x, expected=None):
|
||||
self.ae(dictionary_name_for_locale(x), 'hyph_{}.dic'.format(expected) if expected else None)
|
||||
t('en', 'en_US')
|
||||
t('en_IN', 'en_GB')
|
||||
t('de', 'de_DE')
|
||||
t('fr', 'fr')
|
||||
t('XXX')
|
||||
|
||||
|
||||
def find_tests():
|
||||
return unittest.defaultTestLoader.loadTestsFromTestCase(TestHyphenation)
|
Loading…
x
Reference in New Issue
Block a user