41

Where can I find list of languages and language_code like this.

(Swedish,sv)
(English,en)
Chillar Anand
  • 27,936
  • 9
  • 119
  • 136
Hulk
  • 32,860
  • 62
  • 144
  • 215

5 Answers5

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:

Note that django.conf.settings isn’t a module

Alireza Ghaffari
  • 51
  • 1
  • 2
  • 11
Thomas
  • 11,757
  • 4
  • 41
  • 57
  • 1
    Added bonus to this method: since the gettext_noop() is a lazy lookup, the language names will be translated based upon your user's locale at template render time. Meaning an english user will see "French" but a french user will see "Francais" – Thomas Jul 08 '10 at 08:24
  • is there any reference Pages for the above languages .. – Hulk Jul 08 '10 at 08:53
  • 2
    http://code.djangoproject.com/browser/django/trunk/django/conf/global_settings.py will give you the full list of languages django supports out of the box, and http://docs.djangoproject.com/en/dev/ref/settings/#languages gives you an overview of usage. Hope this helps. – Thomas Jul 09 '10 at 02:24
  • 1
    `max_length` should be 5, there is a dash in country specific languages, e.g. `es-mx` – Kimvais Jun 18 '12 at 09:16
  • 7
    @Kimvais: 'sr-latn' has 7 characters –  Jun 18 '12 at 12:56
  • 1
    The list of languages in this setting is not the same as the list of values accepted by the LANGUAGE_CODE setting. An example is the 'en-us' default value, that doesn't exist in the LANGUAGES global setting. – Filipe Correia Apr 23 '14 at 13:58
26

Wiki:

http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

histrio
  • 1,217
  • 13
  • 24
25

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)
Pawel Furmaniak
  • 4,648
  • 3
  • 29
  • 33
vishes_shell
  • 22,409
  • 6
  • 71
  • 81
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

lennon310
  • 12,503
  • 11
  • 43
  • 61
user3283069
  • 355
  • 1
  • 6
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 in django.utils.translation. You should never import django.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'))
Wtower
  • 18,848
  • 11
  • 103
  • 80
Paul Bormans
  • 1,292
  • 16
  • 22