Skip to content

Commit

Permalink
New providers API (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
mufeedali authored Dec 11, 2023
2 parents c79a6c4 + 1594612 commit bde2562
Show file tree
Hide file tree
Showing 19 changed files with 2,026 additions and 1,543 deletions.
8 changes: 8 additions & 0 deletions dialect/define.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ VERSION = '@VERSION@'

TRANS_NUMBER = 10 # number of translations to save in history

LANG_ALIASES = {
'iw': 'he', # Hebrew
'jw': 'jv', # Javanese
'mni-Mtei': 'mni', # Meiteilon (Manipuri)
'zh-CN': 'zh-Hans',
'zh-TW': 'zh-Hant',
}

LANGUAGES = {
"aa": "Afar",
"ab": "Abkhazian",
Expand Down
29 changes: 0 additions & 29 deletions dialect/languages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,6 @@
from dialect.define import LANGUAGES


ALIASES = {
'iw': 'he', # Hebrew
'jw': 'jv', # Javanese
'mni-Mtei': 'mni', # Meiteilon (Manipuri)
'zh-CN': 'zh-Hans',
'zh-TW': 'zh-Hant',
}


def normalize_lang_code(code):
code = code.replace('_', '-') # Normalize separator
codes = code.split('-')

if len(codes) == 2: # Code contain a script or country code

if len(codes[1]) == 4: # ISO 15924 (script)
codes[1] = codes[1].capitalize()

elif len(codes[1]) == 2: # ISO 3166-1 (country)
codes[1] = codes[1].upper()

code = '-'.join(codes)

if code in ALIASES:
code = ALIASES[code]

return code


def get_lang_name(code):
name = LANGUAGES.get(code)
if name:
Expand Down
4 changes: 2 additions & 2 deletions dialect/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from dialect.define import RES_PATH
from dialect.settings import Settings
from dialect.providers import ProvidersListModel, MODULES, TTS
from dialect.providers import ProviderFeature, ProvidersListModel, MODULES, TTS
from dialect.widgets import ProviderPreferences


Expand Down Expand Up @@ -101,7 +101,7 @@ def _provider_has_settings(self, name):
if not name:
return False

if MODULES[name].change_instance or MODULES[name].api_key_supported:
if ProviderFeature.INSTANCES in MODULES[name].features or ProviderFeature.API_KEY in MODULES[name].features:
return True

return False
Expand Down
21 changes: 14 additions & 7 deletions dialect/providers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@
# SPDX-License-Identifier: GPL-3.0-or-later

import importlib
import logging
import pkgutil

from gi.repository import Gio, GObject

from dialect.providers.base import ProviderCapability, ProviderFeature, ProviderError, ProviderErrorCode # noqa
from dialect.providers import modules

MODULES = {}
TRANSLATORS = {}
TTS = {}
for _importer, modname, _ispkg in pkgutil.iter_modules(__path__):
if modname != 'base':
modclass = importlib.import_module('dialect.providers.' + modname).Provider
for _importer, modname, _ispkg in pkgutil.iter_modules(modules.__path__):
try:
modclass = importlib.import_module('dialect.providers.modules.' + modname).Provider
MODULES[modclass.name] = modclass
if modclass.translation:
TRANSLATORS[modclass.name] = modclass
if modclass.tts:
TTS[modclass.name] = modclass
if modclass.capabilities:
if ProviderCapability.TRANSLATION in modclass.capabilities:
TRANSLATORS[modclass.name] = modclass
if ProviderCapability.TTS in modclass.capabilities:
TTS[modclass.name] = modclass
except Exception as exc:
logging.warning(f'Could not load the {modname} provider: {exc}')


def check_translator_availability(provider_name):
Expand Down
Loading

0 comments on commit bde2562

Please sign in to comment.