From b16b5535bfbac7ed7af3e676e367eab0f4c6611b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 1 Dec 2019 10:50:32 +0530 Subject: [PATCH] Implement mapping of locales to dictionary names --- setup/test.py | 2 + src/calibre/utils/hyphenation/dictionaries.py | 39 +++++++++++++++++++ .../utils/hyphenation/test_hyphenation.py | 26 +++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/calibre/utils/hyphenation/test_hyphenation.py diff --git a/setup/test.py b/setup/test.py index 4825407a48..41d4d2c32a 100644 --- a/setup/test.py +++ b/setup/test.py @@ -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()) diff --git a/src/calibre/utils/hyphenation/dictionaries.py b/src/calibre/utils/hyphenation/dictionaries.py index 0407e63cd6..0e683740e0 100644 --- a/src/calibre/utils/hyphenation/dictionaries.py +++ b/src/calibre/utils/hyphenation/dictionaries.py @@ -3,3 +3,42 @@ # License: GPL v3 Copyright: 2019, Kovid Goyal 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] diff --git a/src/calibre/utils/hyphenation/test_hyphenation.py b/src/calibre/utils/hyphenation/test_hyphenation.py new file mode 100644 index 0000000000..b65c78b89a --- /dev/null +++ b/src/calibre/utils/hyphenation/test_hyphenation.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python2 +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2019, Kovid Goyal + +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)