cic-stack/apps/cic-ussd/cic_ussd/translation.py

25 lines
919 B
Python
Raw Normal View History

2021-02-06 16:13:47 +01:00
"""
This module is responsible for translation of ussd menu text based on a user's set preferred language.
"""
import i18n
from typing import Optional
def translation_for(key: str, preferred_language: Optional[str] = None, **kwargs) -> str:
"""
Translates text mapped to a specific YAML key into the user's set preferred language.
2021-04-19 10:44:40 +02:00
:param preferred_language: Account's preferred language in which to view the ussd menu.
2021-02-06 16:13:47 +01:00
:type preferred_language str
:param key: Key to a specific YAML test entry
:type key: str
:param kwargs: Dynamic values to be interpolated into the YAML texts for specific keys
:type kwargs: any
:return: Appropriately translated text for corresponding provided key
:rtype: str
"""
if preferred_language:
i18n.set('locale', preferred_language)
2021-06-03 15:40:51 +02:00
else:
i18n.set('locale', i18n.config.get('fallback'))
2021-02-06 16:13:47 +01:00
return i18n.t(key, **kwargs)