Anbarryprojects / fastapi-babel

FastAPI babel support pybable tools like translation of text, formatting of dates, times and numbers, and time zones.
MIT License
49 stars 15 forks source link

Allow gettext's placeholders #44

Open Garito opened 1 month ago

Garito commented 1 month ago

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

Thanks!

Gu-f commented 1 month ago

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.

Garito commented 1 month ago

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

Gu-f commented 1 month ago

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?

Garito commented 1 month ago

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

Legopapurida commented 1 month ago

@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 commented 1 month ago

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 commented 1 month ago

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)
Garito commented 1 month ago

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