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

fix/get_message_lang #71

Merged
merged 1 commit into from
Dec 29, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ovos_bus_client/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ def get_message_lang(message=None):
return None
# old style lang param
lang = message.data.get("lang") or message.context.get("lang")
if lang:
return lang

# new style session lang
if not lang and "session_id" in message.context or "session" in message.context:
if "session_id" in message.context or "session" in message.context:
sess = SessionManager.get(message)
return sess.lang

Expand Down
19 changes: 19 additions & 0 deletions test/unittests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from unittest import TestCase
import json
from ovos_bus_client import Message
from ovos_bus_client.util import get_message_lang
from ovos_config.locale import setup_locale
from ovos_bus_client.message import dig_for_message
from ovos_bus_client.session import Session, SessionManager

Expand Down Expand Up @@ -155,6 +157,23 @@ def test_class_patching(self):
self.assertIsInstance(m2, Message)


class TestLanguageExtraction(TestCase):
def test_no_lang_in_message(self):
"""No lang in message should result in lang from active locale."""
setup_locale("it-it")
msg = Message('test msg', data={})
self.assertEqual(get_message_lang(msg), 'it-it')
setup_locale("en-us")
self.assertEqual(get_message_lang(msg), 'en-us')

def test_lang_exists(self):
"""Message has a lang code in data, it should be used."""
msg = Message('test msg', data={'lang': 'de-de'})
self.assertEqual(get_message_lang(msg), 'de-de')
msg = Message('test msg', data={'lang': 'sv-se'})
self.assertEqual(get_message_lang(msg), 'sv-se')


class TestCollectionMessage(unittest.TestCase):
# TODO
pass
Expand Down
Loading