Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/lang_utils #47

Merged
merged 1 commit into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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