Skip to content

Commit

Permalink
fix/get_message_lang (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl authored Dec 29, 2023
1 parent eb84afb commit 1eb7c2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit 1eb7c2c

Please sign in to comment.