diff --git a/kairon/shared/multilingual/utils/translator.py b/kairon/shared/multilingual/utils/translator.py index 387583a8c..1f720097a 100644 --- a/kairon/shared/multilingual/utils/translator.py +++ b/kairon/shared/multilingual/utils/translator.py @@ -69,24 +69,28 @@ def translate_text_bulk(text: List[Text], s_lang: Text, d_lang: Text, mime_type: @staticmethod def get_supported_languages(): + multilingual_env = Utility.environment.get('multilingual', {}) + service_account_creds = multilingual_env.get('service_account_creds', {}) + service_account_info_json = { - "type": Utility.environment['multilingual']['service_account_creds'].get('type', "service_account"), - "project_id": Utility.environment['multilingual']['project_id'], - "private_key_id": Utility.environment['multilingual']['service_account_creds']['private_key_id'], - "private_key": Utility.environment['multilingual']['service_account_creds']['private_key'], - "client_email": Utility.environment['multilingual']['service_account_creds']['client_email'], - "client_id": Utility.environment['multilingual']['service_account_creds']['client_id'], - "auth_uri": Utility.environment['multilingual']['service_account_creds']['auth_uri'], - "token_uri": Utility.environment['multilingual']['service_account_creds']['token_uri'], - "auth_provider_x509_cert_url": Utility.environment['multilingual']['service_account_creds']['auth_provider_x509_cert_url'], - "client_x509_cert_url": Utility.environment['multilingual']['service_account_creds']['client_x509_cert_url'] + "type": service_account_creds.get('type', "service_account"), + "project_id": multilingual_env.get('project_id'), + "private_key_id": service_account_creds.get('private_key_id'), + "private_key": service_account_creds.get('private_key'), + "client_email": service_account_creds.get('client_email'), + "client_id": service_account_creds.get('client_id'), + "auth_uri": service_account_creds.get('auth_uri'), + "token_uri": service_account_creds.get('token_uri'), + "auth_provider_x509_cert_url": service_account_creds.get('auth_provider_x509_cert_url'), + "client_x509_cert_url": service_account_creds.get('client_x509_cert_url') } logger.debug(service_account_info_json) credentials = service_account.Credentials.from_service_account_info(service_account_info_json) client = translate.TranslationServiceClient(credentials=credentials) location = "global" - parent = f"projects/{Utility.environment['multilingual']['project_id']}/locations/{location}" - response = client.get_supported_languages(parent=parent) + parent = f"projects/{multilingual_env.get('project_id')}/locations/{location}" + response = client.get_supported_languages(parent=parent, display_language_code="en") + result = {language.language_code: language.display_name for language in response.languages} + return result - return [language.language_code for language in response.languages] diff --git a/tests/integration_test/services_test.py b/tests/integration_test/services_test.py index 86d94605e..3df07c8ef 100644 --- a/tests/integration_test/services_test.py +++ b/tests/integration_test/services_test.py @@ -28113,7 +28113,18 @@ def test_multilingual_translate_logs(): def test_multilingual_language_support(monkeypatch): def _mock_supported_languages(*args, **kwargs): - return ["es", "en", "hi"] + return {"ab": "Abkhaz", + "ace": "Acehnese", + "ach": "Acholi", + "af": "Afrikaans", + "sq": "Albanian", + "alz": "Alur", + "am": "Amharic", + "ar": "Arabic", + "hy": "Armenian", + "as": "Assamese", + "awa": "Awadhi", + "ay": "Aymara"} monkeypatch.setattr( Translator, "get_supported_languages", _mock_supported_languages @@ -28124,7 +28135,18 @@ def _mock_supported_languages(*args, **kwargs): headers={"Authorization": pytest.token_type + " " + pytest.access_token}, ).json() - assert response["data"] == ["es", "en", "hi"] + assert response["data"] == {"ab": "Abkhaz", + "ace": "Acehnese", + "ach": "Acholi", + "af": "Afrikaans", + "sq": "Albanian", + "alz": "Alur", + "am": "Amharic", + "ar": "Arabic", + "hy": "Armenian", + "as": "Assamese", + "awa": "Awadhi", + "ay": "Aymara"} assert response["success"] assert response["error_code"] == 0 diff --git a/tests/unit_test/multilingual/multilingual_processor_test.py b/tests/unit_test/multilingual/multilingual_processor_test.py index 72f6d9100..ee81ad4dc 100644 --- a/tests/unit_test/multilingual/multilingual_processor_test.py +++ b/tests/unit_test/multilingual/multilingual_processor_test.py @@ -585,9 +585,11 @@ class LanguageResponse: class Language: language_code = "en" + display_name = "English" def __init__(self): self.language_code = "en" + self.display_name = "English" languages = [] @@ -603,7 +605,7 @@ def __init__(self): mocked_new.side_effect = _mock_service_client supported_languages = Translator.get_supported_languages() - assert supported_languages == ["en"] + assert supported_languages == {"en": "English"} def test_translation_call_fail(self, monkeypatch):