-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Not working with django model field verbose_name=lazyt("Title") #110
Comments
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 |
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 Interestingly, if I change the 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. |
Hello @kbairak yes, i use verbose name to display the form like that.
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? |
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 |
When i'm displaying a form and have set verbose_name=lazyt("Title") like this:
title = models.CharField(max_length=250, null=True, blank=True, unique=False, verbose_name=lazyt("Title"))
And i'm rendering a form:
The translations are not working/taken into account, it stays in English
The text was updated successfully, but these errors were encountered: