Get translated text by key path Args: key_path: Dot-separated path to the text (e.g., 'buttons.ok') **kwargs: Variables for string formatting Returns: Translated text or key_path if not found
(self, key_path: str, **kwargs)
| 70 | return self.available_languages.copy() |
| 71 | |
| 72 | def get_text(self, key_path: str, **kwargs) -> str: |
| 73 | """ |
| 74 | Get translated text by key path |
| 75 | |
| 76 | Args: |
| 77 | key_path: Dot-separated path to the text (e.g., 'buttons.ok') |
| 78 | **kwargs: Variables for string formatting |
| 79 | |
| 80 | Returns: |
| 81 | Translated text or key_path if not found |
| 82 | """ |
| 83 | try: |
| 84 | # Get current language data |
| 85 | lang_data = self.languages.get(self.current_language, {}) |
| 86 | |
| 87 | # Navigate through the nested dict using key path |
| 88 | keys = key_path.split('.') |
| 89 | value = lang_data |
| 90 | |
| 91 | for key in keys: |
| 92 | if isinstance(value, dict) and key in value: |
| 93 | value = value[key] |
| 94 | else: |
| 95 | # Fallback to English if current language doesn't have the key |
| 96 | if self.current_language != "en_US": |
| 97 | return self._get_fallback_text(key_path, **kwargs) |
| 98 | else: |
| 99 | # Return key path if even English doesn't have it |
| 100 | return key_path |
| 101 | |
| 102 | # Format string with provided kwargs |
| 103 | if isinstance(value, str) and kwargs: |
| 104 | try: |
| 105 | return value.format(**kwargs) |
| 106 | except KeyError as e: |
| 107 | print(f"Missing format key {e} for text: {value}") |
| 108 | return value |
| 109 | |
| 110 | return str(value) |
| 111 | |
| 112 | except Exception as e: |
| 113 | print(f"Error getting text for key '{key_path}': {e}") |
| 114 | return key_path |
| 115 | |
| 116 | def _get_fallback_text(self, key_path: str, **kwargs) -> str: |
| 117 | """Get fallback text from English""" |
no test coverage detected