using django model translation with django rest
- Install the django-modeltranslation package using pip:
bash
pip install django-modeltranslation
- Add 'modeltranslation' to the list of installed apps in your Django settings file:
python
INSTALLED_APPS = [
...
'modeltranslation',
...
]
- Define the languages you want to use in the LANGUAGES setting in your Django settings file:
```python from django.utils.translation import gettext_lazy as _
LANGUAGES = [ ('en', ('English')), ('es', ('Spanish')), # Add more languages as needed ] ```
- Add the necessary settings for modeltranslation in your Django settings file:
```python from modeltranslation import gettext as _
# Example settings MODELTRANSLATION_DEFAULT_LANGUAGE = 'en' MODELTRANSLATION_LANGUAGES = ('en', 'es') ```
- Register the models you want to translate in the model's admin file using the
register
decorator:
```python from modeltranslation.admin import TranslationAdmin
@admin.register(YourModel) class YourModelTranslationAdmin(TranslationAdmin): pass ```
- Create translation fields in your model by adding the
TranslatedFields
to your model definition:
```python from modeltranslation.models import TranslatedFields
class YourModel(models.Model): translations = TranslatedFields( title=models.CharField(max_length=200), description=models.TextField(), # Add more fields as needed ) ```