Actually if you did all the configurations correctly in settings.py, views and templates and you installed gettext and all good, then you may want to check where your virtual environment is. For instance if it's inside your root folder in your project folder your structure I suppose is myapp_project/venv/
Also I'm assuming you've you created an empty folder called locale in your root myapp_project folder.
Try to run it like this if you're translating french for example:
and note: replace venv with whatever you named your virtual environment.
This is the short answer
django-admin.py makemessages -l fr -i venv
this above will get you the local/fr/LC_MESSAGES/django.po
but you now have to run the second command to compile the .po
file created by makemessages to a .mo
file in the LC_MESSAGES
folder
So, then run:
django-admin.py compilemessages
now this should get you the django.po file.
This is the long answer
If you configured your urls.py
correctly with i18n_patterns, and you filled the translations of msgstr ""
in your .po file and then run django-admin.py compilemessages
again you can now run your server and go to your desired page 127.0.0.1:8000/fr/
and you should see your translations.
In case you are wondering what your settings.py file should look like (for french). It should look like this at least part of it.
from django.utils.translation import gettext_lazy as _
LANGUAGES = [
('fr', _('French')),
('en', _('English')),
]
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
LOCALE_PATHS = [
os.path.join(BASE_DIR, 'locale'),
]
Also in settings.py
in the MIDDLEWARE list make sure you add the component. 'django.middleware.locale.LocaleMiddleware'
and it's important where on the list you put that component as it executes from top to bottom. So put it under the sessions component.
For your main app (you may have a few or one) urls.py
file make sure you import from django.conf.urls.i18n import i18n_patterns
.
Next on the same urls.py
file ensure that you actually add/or edit the url patterns alright. Here is an example of what it could look like:
urlpatterns += i18n_patterns (
path('', include('pages.urls')),
path('meals/', include('meals.urls')),
prefix_default_language=False
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can play with this and obviously on the paths above it don't include the admin. So just play around but just giving you an idea of what it should look like in order for /fr/
url to work.
You will also notice above the line prefix_default_language=False
this is to prevent the /en/ default pattern. In this example we are translating from English to French. So no need to have /en/ in the pattern.
Don't forget in your templates... yes all of them, it don't matter if it's parent template or not. You have to include {% load i18n %}
for this to work.
Important to Note:
When you go back to your templates files and you start updating your text content and adding your transblocks etc. After you do this. Go back to your terminal and stop your server, and again if you have your venv folder in your root you must run these commands:
django-admin.py makemessages -l fr -i venv
django-admin.py compilemessages
Now you can open your django.po file and add your translations to the edited text content in your templates. If your venv folder ins't in your root folder. You can just run django-admin.py makemessages -l fr
instead.
If you don't do this, you will end up doing things manually in your .po file. You don't wanna do that.
I hope this helped.
Unknown encoding "latin-1"
😂 – Sympathize