HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib/python3.10/site-packages/flask_wtf/i18n.py
from babel import support
from flask import current_app
from flask import request
from flask_babel import get_locale
from wtforms.i18n import messages_path

__all__ = ("Translations", "translations")


def _get_translations():
    """Returns the correct gettext translations.
    Copy from flask-babel with some modifications.
    """

    if not request:
        return None

    # babel should be in extensions for get_locale
    if "babel" not in current_app.extensions:
        return None

    translations = getattr(request, "wtforms_translations", None)

    if translations is None:
        translations = support.Translations.load(
            messages_path(), [get_locale()], domain="wtforms"
        )
        request.wtforms_translations = translations

    return translations


class Translations:
    def gettext(self, string):
        t = _get_translations()
        return string if t is None else t.ugettext(string)

    def ngettext(self, singular, plural, n):
        t = _get_translations()

        if t is None:
            return singular if n == 1 else plural

        return t.ungettext(singular, plural, n)


translations = Translations()