Giter Site home page Giter Site logo

Comments (4)

Thutmose3 avatar Thutmose3 commented on August 21, 2024

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.

kbairak avatar kbairak commented on August 21, 2024

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:

image

image

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:

image

image

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!

image

image

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.

Thutmose3 avatar Thutmose3 commented on August 21, 2024

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.

kbairak avatar kbairak commented on August 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.