Skip to content

Commit

Permalink
fix/untangle_from_LF
Browse files Browse the repository at this point in the history
untangles configuration from lingua-franca, ovos-config should be the source of truth for everything config related

fix: LF was optional, but if installed would stop core from loading if configured language was unsupported
  • Loading branch information
JarbasAl authored Dec 22, 2023
1 parent ae24347 commit ab1db04
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions ovos_config/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

try:
import lingua_franca as LF
LF_SUPPORTED = True
except ImportError:
LF = None
LF_SUPPORTED = False

_lang = None
_default_tz = None
Expand Down Expand Up @@ -50,8 +52,6 @@ def get_full_lang_code(lang):

def get_primary_lang_code(config=None):
global _lang
if LF:
return LF.get_primary_lang_code()
if not _lang:
config = config or ovos_config.Configuration()
_lang = config.get("lang", "en-us")
Expand All @@ -65,19 +65,25 @@ def get_default_lang(config=None):
@return: lowercase BCP-47 language code
"""
global _lang
if LF and LF.get_default_loc():
return LF.get_default_loc()
if not _lang:
config = config or ovos_config.Configuration()
_lang = config.get("lang", "en-us")
return _lang


def set_default_lang(lang):
""" setup default language across OVOS packages
currently only configures lingua-franca language, in the future
other hooks may be added if we need to perform this operation globally"""
global _lang
_lang = lang
if LF:
LF.set_default_lang(lang)
try:
LF.set_default_lang(lang)
LF_SUPPORTED = True # re-enable LF
except:
LF_SUPPORTED = False


def get_config_tz():
Expand All @@ -91,11 +97,14 @@ def get_default_tz():


def set_default_tz(tz=None):
""" configure LF """
""" configure timezone across OVOS packages
currently only configures lingua-franca, in the future
other hooks may be added if we need to perform this operation globally """
global _default_tz
tz = tz or get_config_tz() or tzlocal()
_default_tz = tz
if LF:
if LF_SUPPORTED:
# tz added in recently, depends on version
try:
LF.time.set_default_tz(tz)
Expand All @@ -104,13 +113,27 @@ def set_default_tz(tz=None):


def load_languages(langs):
if LF:
LF.load_languages(langs)
""" load and configure lang specific resources across OVOS packages
currently only loads lingua-franca language data, in the future
other hooks may be added if we need to perform this operation globally"""
if LF_SUPPORTED:
try:
LF.load_languages(langs)
except:
LF_SUPPORTED = False


def load_language(lang):
if LF:
LF.load_language(lang)
""" load and configure lang specific resources across OVOS packages
currently only loads lingua-franca language data, in the future
other hooks may be added if we need to perform this operation globally"""
if LF_SUPPORTED:
try:
LF.load_language(lang)
except:
LF_SUPPORTED = False


def get_valid_languages():
Expand All @@ -121,6 +144,10 @@ def get_valid_languages():


def setup_locale(lang=None, tz=None):
""" setup default language, timezone and other locale data across OVOS packages
currently only configures lingua-franca, in the future
other hooks may be added if we need to perform this operation globally"""
lang_code = lang or ovos_config.Configuration().get("lang", "en-us")
valid_langs = get_valid_languages()
# load any lang specific resources
Expand Down

0 comments on commit ab1db04

Please sign in to comment.