How to override the django admin translation? - python

I'm trying to override the default translations of Django's admin site.
I'm using Django 1.6. My settings.py contains:
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# ...
LANGUAGE_CODE = 'nl'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)
I have copied the file django/contrib/admin/locale/nl/LC_MESSAGES/django.po to my_project/locale/nl/LC_MESSAGES/django.po and I've made some changes to it.
Next, I have run python manage.py compilemessages and python manage.py runserver.
When I visit localhost:8000/admin, however, I'm still seeing Django's default admin translations. What am I doing wrong?
Edit - I found the problem:
The above description is the correct way to override app translations. I followed my own instructions and they work. The reason for my problem was that I accidentally omitted the nl subdirectory the first time. I am a dumb person.

I'm providing an answer, even though #hedgie mostly answered their own question. I'll add a bit of context and description of what's happening. This answer is still applicable as of Django 3.0.
Just as you can override a Django-provided admin template by duplicating the template's name and directory structure within our own project, you can override Django-provided admin translations by duplicating a .po file's name and directory structure within our project.
Django's admin translations live in django/contrib/admin/locale/ and are organized by language in directories named [language code]/LC_MESSAGES/. These individual language directories contain two .po files, django.po and djangojs.po, and their respective compiled .mo files. You will be overriding the .po files, and compiling our own .mo files.
The first thing you have to do is enable translations in settings, and tell Django where you store our translation files.
settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# ...
LANGUAGE_CODE = 'nl-NL'
USE_I18N = True
USE_L10N = True
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),) # our custom translations will go here
Note that although the directory Django uses is nl, the full language identifier for Dutch in the Netherlands is nl-NL. You can find a full list of identifiers organized by country here.
Next, you'll mimic Django's directory structure and create two files in a new directory:
my_project/locale/nl/LC_MESSAGES/django.po
my_project/locale/nl/LC_MESSAGES/djangojs.po
Note that this path must also match what you provided in settings.py.
Copy and paste the contents of Django's translation files. You can now edit the translations for whichever strings you like. For example:
django.po
msgid "Are you sure?"
--- msgstr "Weet u het zeker?"
+++ msgstr "Weet u het zeker?!"
Now you need to compile the messages with:
python manage.py compilemessages
This command compiles your .po files into .mo files, which Django will use to translate any matching gettext calls. You should now see your custom translations in the admin interface.

Related

Changing the name of the sub folder inside Django Template path

I am new to Django and I have a path to my HTML file as
./templates/registration/login.html and I want to change it to
./templates/index/login.html
After renaming it to /templates/index/login.html, it is still picking up the old directory /templates/registration, could not find login.html screen and it throws an error.
Could someone tell me what changes we have to make in settings.py when we rename a folder name inside templates structure?
In your settings.py, you can define the templates location, which applies the template function per app.
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates')
)
I assume your app is named registration, hence why it is looking there. You can either rename your app or place all your templates in a global folder at the same level as the apps (and not within the apps themselves). The latter is not recommended for large applications.
Edit: Why is this labeled Python2? What version of Django is this?

Django: change language from timesince

I want to change the language of timesince. Do I need to change directly in my django source or is there a way to modify TIMESINCE_CHUNKS variable in the settings file?
You have to ./manage.py makemessages.
Then edit the .po files and then ./manage.py compilemessages.
For this to work, however, you have to go through the internationalization chapter, and of course its implementation notes.

Django- Multiple languages on the same site

My issue has to do with a Django site running using one language, and using specific languages for specific apps
My example is:
I want to run a 'pt-pt' (Portuguese- Portugal) Django site. In it, I'm using a 3rd party App that has pt-BR (Portuguese- Brazil) as an available language, but no pt-pt translation. Lacking, pt-pt, pt-BR would be better than English.
I can set the language as:
#settings.py...
LANGUAGE_CODE = 'pt-pt'
But then the site Admin uses pt-pt and the App uses English (undesirable).
If I set it to pt-BR I'll get pt-BR on the site (undesirable) and pt-BR on the App.
So I read Django's Docs again and tried the Languages setting
#settings.py...
LANGUAGE_CODE = 'pt'
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('pt', _('Portuguese')),
('pt-br', _('Brazilian Portuguese')),
)
to no avail (nothing changed). Is there a way to do this?
I believe your best option is to copy the locale files of the pt-BR app to the project root locale directory. Then rename the folder from pt-BR to pt-pt and when you browse the site as pt-pt it will include that particular locale file.
Another option would be to use activate('pt-BR') in the pt-BR app but that would not be very conveniant.

Django: how do I redistribute a project/app that contains extendable HTML templates?

I am reading through the Django tutorial How to write reusable apps. I am trying to figure out how to package HTML base templates so that people who install my app (through pip) can extend them (e.g. with {% extends %}.) When I import a python module I don't have to know its location on the filesystem, but is that the case for Django templates?
(Side note: My project consists of plumbing that make it easier to write a specific type of app. So I have various abstract base classes [models, views, forms], template tags, URL configuration, and HTML templates that users can inherit from. It also contains customizations to Django Admin. Right now it is a project but I am trying to package it as an app because according to what I am reading, that seems to be the right way to package Django code, but maybe I should be doing it differently.)
Use a template dir structure like this:
awesome_app_name/
templates/
awesome_app_name/
base.html
cool_template.html
This allows someone to extend your templates with:
{% extends 'awesome_app_name/cool_template.html' %}
OR they could just swap it out with their own template like this:
my_app_name/
templates/
my_app_name/
my_template.html
awesome_app_name/
cool_template.html <-- this overloads your template with their own
This makes for very flexible templates in shared packages.
EDIT:
This works if you configure django with both a template directory for your project and the app_directories.Loader template loader. I believe this to be the configuration used by most:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_DIR, 'templates'),
)
Template loading is then done in the following order:
Resolve template from project directory
Resolve template from app directories
Here's an example project that follows this structure: https://github.com/brutasse/django-password-reset

Multiple locale file lookup in Django

In my Django app i have the usual django.po locale file under each of the languages. However, I'd like to create a custom locale file called custom.po. Is there any way I could Django to first check for a translation string in the custom.po file and if it doesn't exist, check the django.po file
Thanks.
Check the LOCALE_PATHS setting. This tuple is order-specific, so if you put the path to your custom.po first, you will achieve the effect you're after.
Edit: Additional link for detailed information about How Django discovers translations.

Categories

Resources