Skip to content

Commit

Permalink
Add English fallback to all untranslated strings
Browse files Browse the repository at this point in the history
Signed-off-by: alfred richardsn <rchrdsn@protonmail.ch>
  • Loading branch information
r4rdsn committed Jun 17, 2020
1 parent 1fffd76 commit 421b169
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with TellerBot. If not, see <https://www.gnu.org/licenses/>.
import gettext
import typing
from pathlib import Path

Expand All @@ -35,6 +36,13 @@ def __init__(self, domain, path, default="en"):
self.path = path
self.default = default

def find_locales(self) -> typing.Dict[str, gettext.NullTranslations]:
"""Load all compiled locales from path and add default fallbacks."""
translations = super().find_locales()
for translation in translations.values():
translation.add_fallback(translations[self.default])
return translations

async def get_user_locale(
self, action: str, args: typing.Tuple[typing.Any]
) -> typing.Optional[str]:
Expand All @@ -58,9 +66,10 @@ async def get_user_locale(
return_document=ReturnDocument.AFTER,
)
if document:
return document.get("locale", user.language_code)
locale = document.get("locale", user.language_code)
else:
return user.language_code
locale = user.language_code
return locale if locale in self.available_locales else self.default


i18n = plural_i18n = I18nMiddlewareManual("bot", Path(__file__).parents[1] / "locale")

0 comments on commit 421b169

Please sign in to comment.