fastapi-babel icon indicating copy to clipboard operation
fastapi-babel copied to clipboard

Allow gettext's placeholders

Open Garito opened this issue 1 year ago • 8 comments

Hi! In pybabel you could use _("$(name)s's house", name = "Garito") Whould be nice to have it here

Thanks!

Garito avatar Jul 15 '24 17:07 Garito

The other way of writing it looks good if supported...

from fastapi_babel.core import lazy_gettext as _l
_l("Not support type: {mytype}").format(mytype=cls._type)

But I got an error:

AttributeError: '__LazyText' object has no attribute 'format'

Add one thing: gettext supports format but lazy_gettext does not, I think it should have methods supported by string objects, otherwise it would be weird.

Gu-f avatar Jul 17 '24 12:07 Gu-f

In my mind, fastapi-babel should mimick pybabel as much as possible, otherwise should not be called fastapi-babel

Garito avatar Jul 17 '24 13:07 Garito

In my mind, fastapi-babel should mimick pybabel as much as possible, otherwise should not be called fastapi-babel

Is it better to mimic babel/python's original gettext? And not pybabel?

Gu-f avatar Jul 17 '24 15:07 Gu-f

python gettext doesn't allow placeholders and, with fastapi-babel, one expects the pybabel api integrated as well as possible with fastapi, no?

Garito avatar Jul 17 '24 16:07 Garito

@Garito

Python gettext doesn't allow placeholders and, with fastapi-babel, one expects the pybabel API integrated as well as possible with fastapi, no?

I agree with you but we have to consider backend ecosystems like databases, forms, views, templates, and so on. so we have to make some APIs that work well with pybabel and Fastapi. I am working on some issues that handle all conflicts.

Legopapurida avatar Jul 17 '24 17:07 Legopapurida

The other way of writing it looks good if supported...

from fastapi_babel.core import lazy_gettext as _l
_l("Not support type: {mytype}").format(mytype=cls._type)

But I got an error:

AttributeError: '__LazyText' object has no attribute 'format'

Add one thing: gettext supports format but lazy_gettext does not, I think it should have methods supported by string objects, otherwise, it would be weird.

LazyGettext is not the main gettext, It's only a wrapper around the gettext object that when we need to apply translation texts into params without calling them explicitly like database orm fields, Wtform model fields, and ...

so you cannot merge these two approaches and invoke them directly from the initialized object.

for example:

from fastapi_babel.core import lazy_gettext as _

class Model(BaseModel):
     text: str = Field(default=_("Write what you want")

model = Model() model.text.format()

Legopapurida avatar Jul 17 '24 18:07 Legopapurida

Hi! In pybabel you could use _("$(name)s's house", name = "Garito") Whould be nice to have it here

Thanks!

the best approach is:


name = "Parsa"
welcome_text = _("Welcome %s" % name)

Legopapurida avatar Jul 17 '24 18:07 Legopapurida

If this works well with po editors and allows the normal string formatting, would be good for my part

Garito avatar Jul 18 '24 11:07 Garito

@Garito I have been many issues and please upgrade your babel

Legopapurida avatar Dec 05 '24 21:12 Legopapurida

The new version raises this error:

ImportError: cannot import name '_context_var' from 'fastapi_babel.core' (/usr/local/lib/python3.12/site-packages/fastapi_babel/core.py)

Should I open a new issue?

Garito avatar Dec 19 '24 12:12 Garito

@Garito

I have checked everything

please refactor your code:

from fastapi_babel import Babel, _, BabelConfigs, BabelMiddleware
from fastapi import FastAPI


app = FastAPI()
babel_config = BabelConfigs(
    __file__,
    BABEL_DEFAULT_LOCALE="en",
    BABEL_TRANSLATION_DIRECTORY="lang",
)
babel = Babel(babel_config)

app.add_middleware(BabelMiddleware, babel_configs=babel_config)

Legopapurida avatar Dec 20 '24 07:12 Legopapurida

uninstall previous version and install it again

Legopapurida avatar Dec 20 '24 07:12 Legopapurida

or use this

from fastapi_babel.local_context import context_var

Legopapurida avatar Dec 20 '24 08:12 Legopapurida

That works, thanks

Garito avatar Jan 13 '25 10:01 Garito