Comments (4)
As i'm paying quite a lot for my transifex subscription and the native tool is half broken and no response, i'm gonna fork this repo and fix it myself
from transifex-python.
Hey @Thutmose3 , we are terribly sorry for the delay. This slipped our radar.
Here is what I have tried so far:
# models.py
class Article(models.Model):
title = models.CharField(max_length=100, verbose_name=lazyt("Title"))
# views.py
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ["title"]
def index(request):
localized_title = Article._meta.get_field("title").verbose_name
return render(
request,
"index.html",
{"form": ArticleForm(), "localized_title": localized_title},
)
<!-- index.html -->
<div>{{ localized_title }}</div>
<div>{{ form }}</div>
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
...
<form>
And here is what I get as a result:
So, it would seem that the lazy string we used for the verbose_name
of the field generally works, hence the localized_title
variable appears correctly translated. It also seems that when Django creates a form, it saves a "copy" of the verbose name as a string somewhere. So, when the form is rendered, it uses that copy instead of the lazy string.
Interestingly, if I change the LANGUAGE
setting to Greek, then I get this behavior:
Which further reinforces my finding: During initialization, the verbose name is translated to the default language and saved as a non-lazy string. Then, that string is used regardless of the user's language choice.
My last attempt was this:
-class ArticleForm(forms.ModelForm):
- class Meta:
- model = Article
- fields = ["title"]
def index(request):
+ class ArticleForm(forms.ModelForm):
+ class Meta:
+ model = Article
+ fields = ["title"]
localized_title = Article._meta.get_field("title").verbose_name
return render(
request,
"index.html",
{"form": ArticleForm(), "localized_title": localized_title},
)
This time, it works as expected!
So, it turns out that Django copies the verbose name of the field when the form class is defined. This time, we defined the class inside the view and it took the user's language choice under consideration.
In which way are you using the verbose name in your application? Is it to display a form like we did here or something else?
In any case, I hope I've helped. Please return with more questions if there are any and let us know how it went. And, again, we are sorry for the delay.
from transifex-python.
Hello @kbairak yes, i use verbose name to display the form like that.
When i'm using from django.utils.translation import gettext_lazy as _
it work as expected.
from django.utils.translation import gettext_lazy as _
class PreapprovedPermission(models.Model):
user_permissions = models.ManyToManyField(
Permission,
verbose_name=_("user permissions"),
blank=True,
)
When i render the form in a normal way, this works. But when i use Transifex instead of gettext, it does not work. So this is a Transifex Native bug according to me?
from transifex-python.
I suspect that Django's lazy strings get "special treatment" from django itself when it gets to rendering. I suspect that something like this is going on:
from django... import LazyString
value = ...verbose_name
if not isinsntance(value, LazyString):
value = str(value) # This will apply the default language regardless of the user's choice
...
For Transifex Native, we had to implement our own version of lazy strings because Django's lazy strings are tightly coupled with gettext. So, our lazy strings do not get this special treatment.
Unfortunately, I cannot think of a way to get out of this. At least not yet.
As a workaround, I could suggest replacing this:
class ArticleForm(forms.ModelForm):
...
with this:
def ArticleForm(*args, **kwargs):
class cls(forms.ModelForm):
...
return cls(*args, **kwargs)
This way, you can still use the ArticleForm(request.POST, instance=...)
syntax in your views and have the user's language choice respected.
from transifex-python.
Related Issues (15)
- Python 3.12 support HOT 5
- CDS 404 Client Error HOT 1
- Error when translation None is django template
- Transifex native not working in combination with Django 3.x due to deprecated access HOT 1
- transifex_api.ResourceTranslationsAsyncDownload.download fails with missing attributes error HOT 3
- JsonApiException: (400, ["<JsonApiError: invalid - 'attributes' is a required property>"]) HOT 10
- Unable to pull from today HOT 2
- Support custom CDS handlers HOT 1
- [Django] Can't translate models.TextChoices enum values HOT 15
- Can't disable HTML escape when using lazy translation HOT 2
- Tests is installed as package HOT 2
- Is the OpenAPI spec available over HTTPS?
- APIv3: Bulk actions for /resource_translations_async_downloads GET and POST?
- Transifex doesn't work with Django 4.0 or later. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transifex-python.