From 1eb7c2ce600af44122fdfe14ab76bda106f55790 Mon Sep 17 00:00:00 2001 From: JarbasAI <33701864+JarbasAl@users.noreply.github.com> Date: Fri, 29 Dec 2023 17:23:42 +0000 Subject: [PATCH] fix/get_message_lang (#71) --- ovos_bus_client/util/__init__.py | 4 +++- test/unittests/test_message.py | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ovos_bus_client/util/__init__.py b/ovos_bus_client/util/__init__.py index 077babc..9f67b26 100644 --- a/ovos_bus_client/util/__init__.py +++ b/ovos_bus_client/util/__init__.py @@ -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 diff --git a/test/unittests/test_message.py b/test/unittests/test_message.py index 33d51be..a3b285a 100644 --- a/test/unittests/test_message.py +++ b/test/unittests/test_message.py @@ -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 @@ -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