68👍
If you want something you can use from within django, try:
from django.conf import settings
this will be in the format above, making it perfect for assignment in one of your models choices= fields. (i.e. user_language = models.CharField(max_length=7, choices=settings.LANGUAGES)
)
LANGUAGES = (
('ar', gettext_noop('Arabic')),
('bg', gettext_noop('Bulgarian')),
('bn', gettext_noop('Bengali')),
etc....
)
Note about using settings:
- [Django]-Aggregation of an annotation in GROUP BY in Django
- [Django]-Making django server accessible in LAN
- [Django]-How would you create a 'manual' django migration?
27👍
Previous answers mention only getting LANGUAGE
from settings.py, hovewer there is a big chance that this variable will be overwritten. So, you can get the full list from django.conf.global_settings.LANGUAGES
from django.db import models
from django.conf.global_settings import LANGUAGES
class ModelWithLanguage(models.Model):
language = models.CharField(max_length=7, choices=LANGUAGES)
- [Django]-Get user profile in django
- [Django]-Empty Request.FILES with Django Upload forms
- [Django]-Django syncdb and an updated model
3👍
from django.conf import settings
#note settings is an object , hence you cannot import its contents
settings.configure()
#note LANGUAGES is a tuple of tuples
lang_dict = dict(settings.LANGUAGES)
#use lang_dict for your query.
print lang_dict['en']
Regards
sachin
- [Django]-"{% extends %}" vs "{% include %}" in Django Templates
- [Django]-How do you catch this exception?
- [Django]-How do i pass GET parameters using django urlresolvers reverse
0👍
I understood from Django Project that you can only use a dummy gettext function:
If you define a custom LANGUAGES setting, as explained in the previous bullet, it’s OK to mark the languages as translation strings — but use a “dummy”
ugettext()
function, not the one indjango.utils.translation
. You should never importdjango.utils.translation
from within your settings file, because that module in itself depends on the settings, and that would cause a circular import.”.
It took me some time to find the solution, but I finally got it; the choices of the model field needs to have a tuple with real gettext functions, with a lambda function the dummy’s can be wrapped in the real gettext functions as follows:
from django.utils.translation import ugettext_lazy as _
language = models.CharField(max_length=5, choices=map(lambda (k,v): (k, _(v)), settings.LANGUAGES), verbose_name=_('language'))
- [Django]-Django rest framework: set field-level error from serializer validate() method
- [Django]-Cannot import name _uuid_generate_random in heroku django
- [Django]-How do I use CSS in Django?