Reputation: 568
I changed the default LANGUAGE_CODE from en-us to fa-ir in settings.py file but some sentences and words are still english and not translated (i have some examples in images attached)
here is my admin.py file for my app :
from django.contrib import admin, messages
from django.utils.translation import ngettext
from .models import Product, Category, ProductImage
# Register your models here.
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('Name', 'Slug', 'Parent',)
prepopulated_fields = {"Slug": ("Name",)}
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
fields = (('Title', 'Slug'), ('Description', 'IsExists'), 'Category', 'Created',)
list_display = ('Title', 'Slug', 'Description', 'IsExists', 'Category', 'Created',)
prepopulated_fields = {"Slug": ("Title",)}
def save_form(self, request, form, change):
super().save_form(request, form, change)
@admin.register(ProductImage)
class ProductAdmin(admin.ModelAdmin):
list_display = ('Product', 'Image',)
Upvotes: 0
Views: 1438
Reputation: 1592
i18n is a little more involved than just changing the language code, unfortunately. You need to mark strings for translation, generate a .po
file, add the translations, then compile these to .mo
files.
Django has some strings already marked for translation, which is why some of your text is translated in the admin. Other text, like field names on the model, help text or template text, will not be. To mark models/fields for translation, I typically do something like:
...
from django.utils.translation import ugettext_lazy as _
class MyModel(models.Model):
my_field = models.CharField(verbose_name=_('My Field'), max_length=123)
class Meta:
verbose_name = _('My Model')
verbose_name_plural = _('My Models')
Then you can use the Django management command ./manage.py makemessages
, this will generate the messages file (.po
) in your locale
directory (defined in settings.py
).
You can then go in and add the translated versions of the strings it's generated. Once your translations are added to the .po
file, you can run manage.py compilemessages
- this will generate a .mo
file which is the binary file used by Django to show the translations when it encounters a string that has a translation.
I'd recommend having a good read through the docs as there are a few steps to this stuff and if you have a lot of strings to translate, this translate toolkit is quite helpful
Upvotes: 1