Coverage for seedboxsync/front/babel.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.16.1, created at 2026-09-26 17:14 +0000

1# 

2# Copyright (C) 2015-2026 Guillaume Kulakowski <guillaume@kulakowski.fr> 

3# 

4# For the full copyright and license information, please view the LICENSE 

5# file that was distributed with this source code. 

6# 

7"""SeedboxSync front i18n module.""" 

8 

9from typing import cast 

10from flask import request 

11from flask_babel import Babel, lazy_gettext 

12from seedboxsync.core import Config, current_app 

13 

14babel = Babel() 

15ALLOWED_LANGUAGES = ["fr_FR", "en_US"] 

16 

17 

18def gettext(message: str) -> str: 

19 """ 

20 Return a lazily translated string with a type compatible with WTForms. 

21 

22 The cast is required because Flask-Babel returns a LazyString while 

23 WTForms type annotations expect a str. 

24 """ 

25 return cast(str, lazy_gettext(message)) 

26 

27 

28def get_locale() -> str | None: 

29 """ 

30 Get locale from browser. 

31 

32 Returns: 

33 str: The local. 

34 """ 

35 locale = current_app.config.get(Config.CONFIG_NAMESPACE + "WEBUI_LANGUAGE", "auto") 

36 if locale != "auto": 

37 return str(locale) 

38 return request.accept_languages.best_match(ALLOWED_LANGUAGES)