{"id":75938,"date":"2020-01-20T12:36:01","date_gmt":"2020-01-20T07:06:01","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75938"},"modified":"2024-04-12T14:17:42","modified_gmt":"2024-04-12T08:47:42","slug":"how-django-discovers-language-preference","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/","title":{"rendered":"How Django Discovers Language Preference"},"content":{"rendered":"<p>Once you\u2019ve prepared your translations \u2013 or, if you just want to use the translations that come with Django \u2013 you\u2019ll need to activate translation for your app.<\/p>\n<p>Behind the scenes, Django has a very flexible model of deciding which language should be used \u2013 installation-wide, for a particular user, or both.<\/p>\n<p>To set an installation-wide language preference, set LANGUAGE_CODE. Django uses this language as the default translation \u2013 the final attempt if no better matching translation is found through one of the methods employed by the locale middleware.<\/p>\n<p>If all you want is to run Django with your native language all you need to do is set LANGUAGE_CODE and make sure the corresponding message files and their compiled versions (.mo) exist.<\/p>\n<p>If you want to let each individual user specify which language they prefer, then you also need to use the LocaleMiddleware. LocaleMiddleware enables language selection based on data from the request. It customizes content for each user.<\/p>\n<p>To use LocaleMiddleware, add &#8216;django.middleware.locale.LocaleMiddleware&#8217; to your MIDDLEWARE_CLASSES setting. Because middleware order matters, you should follow these guidelines:<\/p>\n<ul>\n<li>&nbsp;Make sure it\u2019s one of the first middlewares installed.<\/li>\n<li>It should come after SessionMiddleware, because LocaleMiddleware makes use of session data. And it should come before CommonMiddleware because CommonMiddleware needs an activated language in order to resolve the requested URL.<br>\n\uf0fc If you use CacheMiddleware, put LocaleMiddleware after it. For example, your MIDDLEWARE_CLASSES might look like this:<\/li>\n<\/ul>\n<p>MIDDLEWARE_CLASSES = [<br>\n&#8216;django.contrib.sessions.middleware.SessionMiddleware&#8217;,<br>\n&#8216;django.middleware.locale.LocaleMiddleware&#8217;,<br>\n&#8216;django.middleware.common.CommonMiddleware&#8217;,<br>\n]\n<p>LocaleMiddleware tries to determine the user\u2019s language preference by following this algorithm:<\/p>\n<ul>\n<li>First, it looks for the language prefix in the requested URL. This is only performed when you are using the i18n_patterns function in your root URLconf.<\/li>\n<li>Failing that, it looks for the LANGUAGE_SESSION_KEY key in the current user\u2019s session.<\/li>\n<li>Failing that, it looks for a cookie. The name of the cookie used is set by the LANGUAGE_COOKIE_NAME setting. (The default name is django_language.)<\/li>\n<li>Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations.<\/li>\n<li>Failing that, it uses the global LANGUAGE_CODE setting.<br>\nNotes:<\/li>\n<li>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.<\/li>\n<li>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.<\/li>\n<li>Only languages listed in the LANGUAGES setting can be selected. If you want to restrict the language selection to a subset of provided languages (because your application doesn\u2019t provide all those languages), set LANGUAGES to a list of languages. For example:<\/li>\n<\/ul>\n<p>LANGUAGES = [<br>\n(&#8216;de&#8217;, _(&#8216;German&#8217;)),<br>\n(&#8216;en&#8217;, _(&#8216;English&#8217;)),<br>\n]\n<p>This example restricts languages that are available for automatic selection to German and English (and any sublanguage, like de-ch or en-us).<\/p>\n<ul>\n<li>&nbsp;If you define a custom LANGUAGES setting, as explained in the previous bullet, you can mark the language names as translation strings \u2013 but use ugettext_lazy() instead of ugettext() to avoid a circular import. Here\u2019s a sample settings file:<\/li>\n<\/ul>\n<p>from django.utils.translation import ugettext_lazy as _<\/p>\n<p>LANGUAGES = [<br>\n(&#8216;de&#8217;, _(&#8216;German&#8217;)),<br>\n(&#8216;en&#8217;, _(&#8216;English&#8217;)),<br>\n]\n<p>Once LocaleMiddleware determines the user\u2019s preference, it makes this preference available as request.LANGUAGE_CODE for each HttpRequest. Feel free to read this value in your view code. Here\u2019s a simple example:<\/p>\n<p>from django.http import HttpResponse<\/p>\n<p>def hello_world(request, count):<br>\nif request.LANGUAGE_CODE == &#8216;de-at&#8217;:<br>\nreturn HttpResponse(&#8220;You prefer to read Austrian German.&#8221;)<br>\nelse:<br>\nreturn HttpResponse(&#8220;You prefer to read another language.&#8221;)<\/p>\n<p>Note that, with static (middleware-less) translation, the language is in settings.LANGUAGE_CODE, while with dynamic (middleware) translation, it\u2019s in request.LANGUAGE_CODE.<\/p>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once you\u2019ve prepared your translations \u2013 or, if you just want to use the translations that come with Django \u2013 you\u2019ll need to activate translation for your app. Behind the scenes, Django has a very flexible model of deciding which language should be used \u2013 installation-wide, for a particular user, or both. To set an&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8867],"class_list":["post-75938","page","type-page","status-publish","hentry","category-django-web-development","tag-how-django-discovers-language-preference"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How Django Discovers Language Preference - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Django Discovers Language Preference - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Once you\u2019ve prepared your translations \u2013 or, if you just want to use the translations that come with Django \u2013 you\u2019ll need to activate translation for your app. Behind the scenes, Django has a very flexible model of deciding which language should be used \u2013 installation-wide, for a particular user, or both. To set an...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:42+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/\",\"name\":\"How Django Discovers Language Preference - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T07:06:01+00:00\",\"dateModified\":\"2024-04-12T08:47:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How Django Discovers Language Preference\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Django Discovers Language Preference - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/","og_locale":"en_US","og_type":"article","og_title":"How Django Discovers Language Preference - Tutorial","og_description":"Once you\u2019ve prepared your translations \u2013 or, if you just want to use the translations that come with Django \u2013 you\u2019ll need to activate translation for your app. Behind the scenes, Django has a very flexible model of deciding which language should be used \u2013 installation-wide, for a particular user, or both. To set an...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:42+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/","name":"How Django Discovers Language Preference - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T07:06:01+00:00","dateModified":"2024-04-12T08:47:42+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/how-django-discovers-language-preference\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"How Django Discovers Language Preference"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75938","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75938"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75938\/revisions"}],"predecessor-version":[{"id":83443,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75938\/revisions\/83443"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75938"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75938"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75938"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}