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.
Related
I work on a project where we want to have multilingual site. We start with two languages defined in settings.py
LANGUAGES = (
("en-us", _("United States")),
("cs", _("Czech Republic")),
)
I am not the programmer doing the work but if I understood correctly all we need is to be able to add - for example - French language for the whole website but not via setting.py but Django admin web interface.
LANGUAGES = (
("en-us", _("United States")),
("cs", _("Czech Republic")),
("fr", _("French")),
)
We are using rosetta for translating in Django admin. So I want to use Django admin to add new laguage so it appears in rosetta interface.
Could someone tell me how we can control ( add or remove or disable ) languages from Django admin?
I checked these but did not find the answer
Adding new site language in Django admin
How to manage system languages from django admin site?
https://djangowaves.com/tutorial/multiple-languages-in-Django/
Add translation for model field using django rosetta
Adding languages dynamically through Django Admin
The short answer is that you can't do that.
The settings.py of a Django project is not designed, and not recommended to be modified by the web application.(It can introduce a security breach.)
So I recommend to change LANGUAGES manually, or to enable all languages supported by Django by removing LANGUAGES key. Of course, don't forget to generate message files with the makemessages command.
If you really want such a dynamic feature, your best bet will be to implement it on your own by modifying the Django Rosetta source code.(Define a preference item for supported languages on a DB model, and filter languages by its value.)
I'm trying to do Internationalization using Django. I've followed through docs and able to set it up right.
My questions here are
i) What are all the language supported by django?
ii) I've created '.po', '.mo' message files for the languages 'de' , 'de-at' but when trying to access those resources from API I'm only able to get resources from 'de' for both the cases. Does Django overrides 'de-at' by 'de' message file resources. Why is it so?
I've created message files using below commands by executing in project directory.
django-admin.py makemessages -l de
After running above command. I've added resources for both the languages corresponding in their '.po' file
django-admin.py compilemessages
Any help would be appreciated.
de-at is not supported by Django. So by default Django takes de message files.
Django documentation says :
In each of these places, the language preference is expected to be in the standard language format, as a string. For example, Brazilian Portuguese is pt-br.
If a base language is available but the sublanguage specified is not, Django uses the base language. For example, if a user specifies de-at (Austrian German) but Django only has de available, Django uses de.
Hence It returns messages inside a file 'de'
If the question is actual this might help you.
I had task to translate website both to de and de-at.
Django supports de-at language.
List of supported languages:
https://msdn.microsoft.com/en-us/library/ms533052(v=vs.85).aspx
Or you can find on git language_info, I saw list of supported languages there too.
If you created in languages de-at - dash is separator and folder in locale named de_AT ( makemessages de_AT) it is enough. Django defines them as different languages. Problem may be in form you are using on front-end side. In my case form that I got from django documentation crashes on sublanguages so try to change your front-end form, you can find them in another stackoverflow questions.
I use third-party application (django-flatblocks) in my Django project. How can i change the name of the application in admin interface (grappelli)? I need russian name. I already tried to create localization files (.po.mo) and it is work fine in some parts of admin interface, but in breadcrumbs do not. I think grappelli does not use translation in some templates and the best way to resolve the problem is to replace django-flatblocks verbose_name in apps.py "on the fly". Can i do it somehow?
Found this in the official documentation did. The solution is so simple: For application users
To quote the Manual:
If you’re using “Rock ’n’ roll” in a project called anthology, but you
want it to show up as “Jazz Manouche” instead, you can provide your
own configuration:
# anthology/apps.py
from rock_n_roll.apps import RockNRollConfig
class JazzManoucheConfig(RockNRollConfig):
verbose_name = "Jazz Manouche"
# anthology/settings.py
INSTALLED_APPS = [
'anthology.apps.JazzManoucheConfig',
# ...
]
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.
I am writing a web site http://www.perlesloyfer.no that will use
country = self.request.headers.get('X-AppEngine-Country') to determine if its a norwegian
visitor or one from another language.
The template is shown using
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))
If visitor is from another country, it should show the HTML templates internationalized to english, else they should be on norwegian like it is now.
Should i make separate html templates for each language, or is it a way to make one template and just get strings from different files with Django?
Django has a decent i18n feature. Please just use it. As a side note, using X-AppEngine-Country to determine the content language is a bad idea (don't forget people abroad).