Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Created January 25, 2019 07:00
Show Gist options
  • Save iKlotho/ef633e4eba7b7d1aff13742eb18e6e73 to your computer and use it in GitHub Desktop.
Save iKlotho/ef633e4eba7b7d1aff13742eb18e6e73 to your computer and use it in GitHub Desktop.
i18n django
#django muti language
#add settings.py middleware
'django.middleware.locale.LocaleMiddleware',
#add settings.py
from django.utils.translation import ugettext_lazy as _
LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),)
LANGUAGES = (
('tr', _('Turkish')),
('en', _('English')),
)
#create the "locale" folder in the root directory
#execute
django-admin.py makemessages -l tr
django-admin.py makemessages -l en
django-admin.py compilemessages
#add views.py
def language(request, lang_code):
translation.activate(lang_code)
request.session[translation.LANGUAGE_SESSION_KEY] = lang_code
return HttpResponseRedirect('/')
#add template file
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
#example usage
{% trans "Dashboard"%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment