How to localize Content of a Django application

后端 未结 9 1676
暖寄归人
暖寄归人 2020-12-24 09:16

Hey, i am currently working on a django app for my studies, and came to the point of l18n. Localizing the site itself was very easy, but now i have to allow users, to transl

相关标签:
9条回答
  • 2020-12-24 09:56

    It depends on who will provide the translations. If you want to provide a web interface to translation, then you need to develop that yourself, and also represent the translations in the database.

    If the same translators who translated the site will also translate the data, you can provide them with the same model that they use for the site (presumably gettext), and you can then also use gettext for this content.

    0 讨论(0)
  • 2020-12-24 10:00

    Also looking for content localization plugin, or how to write it. Can add to the list django-i18n-model

    0 讨论(0)
  • 2020-12-24 10:06

    I think you should operate in two steps:

    1. Get translations
    2. Show translated strings

    For the first step, you should tell Django that the user-inserted strings are to be translated. I think there is no native way to do so. Maybe you can extract the strings from your db putting them in locale-specific files, run 'makemessages' on them, obtaint django.po files and translate.

    Second, use ugettext to show those strings on your web application.

    Hope this can help the ones with your same problem.

    0 讨论(0)
  • 2020-12-24 10:08

    I have 2 languages on my site: English and Arabic Users can switch between languages clicking on a flag. In models i use a proxy model:

    class Product(models.Model):
        name=models.CharField(max_length=100)
        name_ar=models.CharField(max_length=100, default='')
    
        def __unicode__(self):
            return self.name
    
    class Product_ar(Product):
        def __unicode__(self):
            return self.name_ar
        class Meta:
            proxy=True
    

    In forms I use 2 forms instead of one:

    class CollectionEditForm_en(forms.Form):
        name = forms.CharField(label=_('Name'), max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
        product = forms.ModelChoiceField(label=_('product'), queryset=Product.objects.filter(enabled=True), empty_label=None)
    
    class CollectionEditForm_ar(forms.Form):
        name = forms.CharField(label=_('Name'), max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
        product = forms.ModelChoiceField(label=_('product'), queryset=Product_ar.objects.filter(enabled=True), empty_label=None)
    

    In code check language this way:

    if request.LANGUAGE_CODE=='ar':
        CollectionEditForm=CollectionEditForm_ar
    else:
        CollectionEditForm=CollectionEditForm_en
    

    So in templates i check:

    {% if LANGUAGE_CODE == "ar" %}
      <a href="/product/{{product.alias}}/">{{product.name_ar}}</a>
    {% else %}
      <a href="/product/{{product.alias}}/">{{product.name}}</a>
    {% endif %}
    

    Hope this solution will help somebody

    0 讨论(0)
  • 2020-12-24 10:08

    "i must provide a way of translating theses names and descriptions to the users."

    "Is there a natural way in django to do this?"

    Are you asking if Django can translate from language to language? Are you asking about something like http://translate.google.com/ ?

    I don't think Django can translate user input into another language.

    If you are going to do the translation for your users, this must be part of your data model.

    Django's i18n filter allows you to have a table of translation strings. The documentation says this.

    1. Embed translation strings in your Python code and templates.
    2. Get translations for those strings, in whichever languages you want to support. This is something you do manually, by hiring translators or knowing a lot of languages yourself.
    3. Activate the locale middleware in your Django settings.
    0 讨论(0)
  • 2020-12-24 10:18

    I use django-multilingual for localize content and django-localeurl for choosing language based on url (for example mypage/en/).

    You can see how multilingua and localeurl work on JewishKrakow.net page.

    0 讨论(0)
提交回复
热议问题