Reputation: 698
I am using django and the standard internationalization package as showed in the : excellent marina mele tuto.
In a user's form on web and mobile i have to show a list of country names in the user's language.
To create the country list, i intend to use django-country it seems easy and well documented.
I could do one API, no template, to request the list of countries.
But How to translate this country list in the views.py ?
Any example would be welcome.
Thanks
Upvotes: 0
Views: 1442
Reputation: 828
In case someone is still looking for how to do it. It was pretty easy, in large part thanks to @slurms explanation and reading through the relevant docs (though it took me some time to find them). It's actually very straightforward.
First, you will need to add the necessary settings:
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware'
...
]
USE_I18N = True
Then get the language from your request or other parameter, and the translating can begin e.g.:
from django.utils import translation
from django_countries import countries
def foo_bar(language):
translation.activate(language)
return [(translation.gettext(country.name), country.code) for country in countries]
This is all you need!
Upvotes: 1
Reputation: 698
Finally, i am using a simple array
TranslatedCountries = {
'france': {
'en': u'france',
'fr': u'france',
},
'belgium': {
'en': u'belgium',
'fr': u'belgique',
},
'spain': {
'en': u'spain',
'fr': u'espagne',
},
'morocco': {
'en': u'morocco',
'fr': u'maroc',
},
}
and i access it:
try:
CCC = TranslatedCountries[test_country.lower()][user_language.lower()]
except :
print "The country %s is not defines for the language %s" % (test_country.lower(),user_language.lower() )
CCC = test_country
print CCC
I hope someone wil give us a simpler, faster and cleaner solution. Thanks you for you help. Al
Upvotes: 0
Reputation: 768
I assume you're talking about django-countries? You shouldn't need to do anything other than make sure that your user's language is activated translation.activate(language)
, which is also handled in Django's middleware if you're using i18n_urlpatterns
. From there, it will use the builtin gettext machinery to retrieve the country name in the user's language.
You can use the country objects in a serializer with django_countries.serializer_fields.CountryField
, or in a view with
from django_countries import countries
from django.http import JsonResponse
def countries_list(request):
for code, name in list(countries):
print(code, name)
return JsonResponse({
code: name for code, name in list(countries)
})
Or however you like. In a template:
{% load countries %}
{% get_country 'BR' as country %}
{{ country.name }}
should work, as long as your user's language is activated.
Upvotes: 1
Reputation: 56
You could take a look at django-modeltranslation, the library is used to do translation from your model data.
Otherwise if you want to translate a list of countries, you can construct a new list and use the builtin django translation tools on each item in the list.
Upvotes: 0