Skip to content

Commit

Permalink
- language codes and name as dict (#1614)
Browse files Browse the repository at this point in the history
* - language codes and name as dict

* - language codes and name as dict

---------

Co-authored-by: hghuge <[email protected]>
  • Loading branch information
hiteshghuge and hghuge authored Dec 4, 2024
1 parent c3def34 commit a5f2130
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
30 changes: 17 additions & 13 deletions kairon/shared/multilingual/utils/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
26 changes: 24 additions & 2 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion tests/unit_test/multilingual/multilingual_processor_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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):

Expand Down

0 comments on commit a5f2130

Please sign in to comment.