Skip to content

Commit

Permalink
feat/lang_utils
Browse files Browse the repository at this point in the history
helper methods to retrieve active language from bus messages and lingua_franca
  • Loading branch information
JarbasAl committed May 31, 2022
1 parent c5300d0 commit a1fb417
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
19 changes: 19 additions & 0 deletions ovos_utils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
xdg_cache_home
)

try:
from lingua_franca.lang import get_default_lang as _lf_get_default_lang
except ImportError:
_lf_get_default_lang = None


def get_xdg_config_dirs(folder=None):
folder = folder or get_xdg_base()
Expand Down Expand Up @@ -39,6 +44,20 @@ def get_xdg_cache_save_path(folder=None):
return join(xdg_cache_home(), folder)


def get_default_lang(config=None):
"""Get the default language from lingua_franca or from mycroft.conf
Args:
config (dict): mycroft.conf data, if not set read_mycroft_config() is used
Returns:
The language code for the default language.
"""
if _lf_get_default_lang:
return _lf_get_default_lang()
config = config or read_mycroft_config()
default_lang = config.get("lang") or "en-us"
return default_lang


def get_ovos_config():
# populate default values
config = {"xdg": True,
Expand Down
20 changes: 18 additions & 2 deletions ovos_utils/messagebus.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from mycroft_bus_client import MessageBusClient, Message
from mycroft_bus_client import MessageBusClient
from mycroft_bus_client.message import dig_for_message, Message
from ovos_utils.log import LOG
from ovos_utils.configuration import read_mycroft_config
from ovos_utils.configuration import read_mycroft_config, get_default_lang
from ovos_utils import create_loop
from ovos_utils.json_helper import merge_dict
import time
Expand Down Expand Up @@ -109,6 +110,21 @@ def close(self):
self.on_close()


def get_message_lang(message=None):
"""Get the language from the message or the default language.
Args:
message: message to check for language code.
Returns:
The language code from the message or the default language.
"""
message = message or dig_for_message()
default_lang = get_default_lang()
if not message:
return default_lang
lang = message.data.get("lang") or message.context.get("lang") or default_lang
return lang.lower()


def get_websocket(host, port, route='/', ssl=False, threaded=True):
"""
Returns a connection to a websocket
Expand Down

0 comments on commit a1fb417

Please sign in to comment.