{"id":75937,"date":"2020-01-20T12:35:52","date_gmt":"2020-01-20T07:05:52","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75937"},"modified":"2024-04-12T14:17:42","modified_gmt":"2024-04-12T08:47:42","slug":"creating-language-files-2","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/","title":{"rendered":"Creating Language Files"},"content":{"rendered":"<p>Once the string literals of an application have been tagged for later translation, the translation themselves need to be written (or obtained). Here\u2019s how that works.<\/p>\n<h3>Message Files<\/h3>\n<p>The first step is to create a message file for a new language. A message file is a plain-text file, representing a single language, that contains all available translation strings and how they should be represented in the given language. Message files have a .po file extension.<\/p>\n<p>Django comes with a tool, django-admin makemessages, that automates the creation and upkeep of these files. The makemessages command (and compilemessages discussed later) use commands from the GNU gettext toolset: xgettext, msgfmt, msgmerge and msguniq. The minimum version of the gettext utilities supported is 0.15. To create or update a message file, run this command:<\/p>\n<p>django-admin makemessages -l de<\/p>\n<p>\u2026 where de is the locale name for the message file you want to create. For example, pt_BR for Brazilian Portuguese, de_AT for Austrian German or id for Indonesian.<\/p>\n<p>The script should be run from one of two places:<\/p>\n<ul>\n<li>&nbsp;The root directory of your Django project (the one that contains manage.py).<\/li>\n<li>The root directory of one of your Django apps.<\/li>\n<\/ul>\n<p>The script runs over your project source tree or your application source tree and pulls out all strings marked for translation. It creates (or updates) a message file in the directory locale\/LANG\/LC_MESSAGES. In the de example, the file will be locale\/de\/LC_MESSAGES\/django.po.<\/p>\n<p>When you run makemessages from the root directory of your project, the extracted strings will be automatically distributed to the proper message files. That is, a string extracted from a file of an app containing a locale directory will go in a message file under that directory. A string extracted from a file of an app without any locale directory will either go in a message file under the directory listed first in LOCALE_PATHS or will generate an error if LOCALE_PATHS is empty.<\/p>\n<p>By default, django-admin makemessages examines every file that has the .html or .txt file extension. In case you want to override that default, use the &#8211;extension or -e option to specify the file extensions to examine:<\/p>\n<p>django-admin makemessages -l de -e txt<\/p>\n<p>Separate multiple extensions with commas and\/or use -e or &#8211;extension multiple times:<\/p>\n<p>django-admin makemessages -l de -e html,txt -e xml<\/p>\n<p>When creating message files from JavaScript source code you need to use the special \u2018djangojs\u2019 domain, not -e js.<\/p>\n<p>If you don\u2019t have the gettext utilities installed, makemessages will create empty files. If that\u2019s the case, either install the gettext utilities or just copy the English message file (locale\/en\/LC_MESSAGES\/django.po) if available and use it as a starting point; it\u2019s just an empty translation file.<\/p>\n<p>If you\u2019re using Windows and need to install the GNU gettext utilities so makemessages works. The format of .po files is straightforward. Each .po file contains a small bit of metadata, such as the translation maintainer\u2019s contact information, but the bulk of the file is a list of messages \u2013 simple mappings between translation strings and the actual translated text for the particular language.<\/p>\n<p>For example, if your Django app contained a translation string for the text &#8220;Welcome to my site.&#8221;, like so:<\/p>\n<p>_(&#8220;Welcome to my site.&#8221;)<\/p>\n<p>\u2026 then django-admin makemessages will have created a .po file containing the following snippet \u2013 a message:<\/p>\n<p>#: path\/to\/python\/module.py:23<br>\nmsgid &#8220;Welcome to my site.&#8221;<br>\nmsgstr &#8220;&#8221;<\/p>\n<p>A quick explanation:<\/p>\n<ul>\n<li>&nbsp;msgid is the translation string, which appears in the source. Don\u2019t change it.<\/li>\n<li>msgstr is where you put the language-specific translation. It starts out empty, so it\u2019s your responsibility to change it. Make sure you keep the quotes around your translation.<\/li>\n<li>As a convenience, each message includes, in the form of a comment line prefixed with # and located above the msgid line, the filename and line number from which the translation string was gleaned.<\/li>\n<\/ul>\n<p>Long messages are a special case. There, the first string directly after the msgstr (or msgid) is an empty string. Then the content itself will be written over the next few lines as one string per line. Those strings are directly concatenated. Don\u2019t forget trailing spaces within the strings; otherwise, they\u2019ll be tacked together without white-space!<\/p>\n<p>Due to the way the gettext tools work internally and because we want to allow non-ASCII source strings in Django\u2019s core and your applications, you must use UTF-8 as the encoding for your PO files (the default when PO files are created). This means that everybody will be using the same encoding, which is important when Django processes the PO files.<\/p>\n<p>To re-examine all source code and templates for new translation strings and update all message files for all languages, run this:<\/p>\n<p>django-admin makemessages -a<\/p>\n<h3>Compiling Message Files<\/h3>\n<p>After you create your message file \u2013 and each time you make changes to it \u2013 you\u2019ll need to compile it into a more efficient form, for use by gettext. Do this with the django-admin compilemessages utility.<\/p>\n<p>This tool runs over all available .po files and creates .mo files, which are binary files optimized for use by gettext. In the same directory from which you ran django-admin makemessages, run:<\/p>\n<p>django-admin compilemessages<\/p>\n<p>That\u2019s it. Your translations are ready for use.<\/p>\n<p>If you\u2019re using Windows and need to install the GNU gettext utilities so django-admin compilemessages works.<\/p>\n<p>Django only supports .po files encoded in UTF-8 and without any BOM (Byte Order Mark) so if your text editor adds such marks to the beginning of files by default then you will need to reconfigure it.<\/p>\n<h3>Creating Message Files from Javascript Source Code<\/h3>\n<p>You create and update the message files the same way as the other Django message files \u2013 with the django-admin makemessages tool. The only difference is you need to explicitly specify what in gettext parlance is known as a domain in this case the djangojs domain, by providing a -d djangojs parameter, like this:<\/p>\n<p>django-admin makemessages -d djangojs -l de<\/p>\n<p>This would create or update the message file for JavaScript for German. After updating message files, just run django-admin compilemessages the same way as you do with normal Django message files.<\/p>\n<h3>gettext On Windows<\/h3>\n<p>This is only needed for people who either want to extract message IDs or compile message files (.po). Translation work itself just involves editing existing files of this type, but if you want to create your own message files, or want to test or compile a changed message file, you will need the gettext utilities:<\/p>\n<p>Download the following zip files from the GNOME servers:<\/p>\n<ul>\n<li>gettext-runtime-X.zip<\/li>\n<li>gettext-tools-X.zip<\/li>\n<\/ul>\n<p>(X is the version number; version 0.15 or higher is required.)<\/p>\n<p>Extract the contents of the bin\\ directories in both files to the same folder on your system (i.e. C:\\Program Files\\gettext-utils)<\/p>\n<p>Update the system PATH:<\/p>\n<ul>\n<li>Control Panel &gt; System &gt; Advanced &gt; Environment Variables.<\/li>\n<li>In the System variables list, click Path, click Edit.<\/li>\n<li>Add ;C:\\Program Files\\gettext-utils\\bin at the end of the Variable value field.<\/li>\n<\/ul>\n<p>You may also use gettext binaries you have obtained elsewhere, so long as the xgettext &#8211;version command works properly. Do not attempt to use Django translation utilities with a gettext package if the command xgettext &#8211;version<br>\nentered at a Windows command prompt causes a popup window saying xgettext.exe has generated errors and will be closed by Windows.<\/p>\n<h3>Customizing The Make messages Command<\/h3>\n<p>If you want to pass additional parameters to xgettext, you need to create a custom makemessages command and override its xgettext_options attribute:<\/p>\n<p>from django.core.management.commands import makemessages<\/p>\n<p>class Command(makemessages.Command):<br>\nxgettext_options = makemessages.Command.xgettext_options + [&#8216;&#8211;keyword=mytrans&#8217;]\n<p>If you need more flexibility, you could also add a new argument to your custom makemessages command:<\/p>\n<p>from django.core.management.commands import makemessages<\/p>\n<p>class Command(makemessages.Command):<\/p>\n<p>def add_arguments(self, parser):<br>\nsuper(Command, self).add_arguments(parser)<br>\nparser.add_argument(&#8216;&#8211;extra-keyword&#8217;,<br>\ndest=&#8217;xgettext_keywords&#8217;,<br>\naction=&#8217;append&#8217;)<\/p>\n<p>def handle(self, *args, **options):<br>\nxgettext_keywords = options.pop(&#8216;xgettext_keywords&#8217;)<br>\nif xgettext_keywords:<br>\nself.xgettext_options = (<br>\nmakemessages.Command.xgettext_options[:] +<br>\n[&#8216;&#8211;keyword=%s&#8217; % kwd for kwd in xgettext_keywords]\n)<br>\nsuper(Command, self).handle(*args, **options)<\/p>\n<h3>Explicitly Setting the Active Language<\/h3>\n<p>You may want to set the active language for the current session explicitly. Perhaps a user\u2019s language preference is retrieved from another system, for example. You\u2019ve already been introduced to django.utils.translation.activate(). That applies to the current thread only. To persist the language for the entire session, also modify LANGUAGE_SESSION_KEY in the session:<\/p>\n<p>from django.utils import translation<br>\nuser_language = &#8216;fr&#8217;<br>\ntranslation.activate(user_language)<br>\nrequest.session[translation.LANGUAGE_SESSION_KEY] = user_language<\/p>\n<p>You would typically want to use both: django.utils.translation.activate() will change the language for this thread, and modifying the session makes this preference persist in future requests.<\/p>\n<p>If you are not using sessions, the language will persist in a cookie, whose name is configured in LANGUAGE_COOKIE_NAME. For example:<\/p>\n<p>from django.utils import translation<br>\nfrom django import http<br>\nfrom django.conf import settings<br>\nuser_language = &#8216;fr&#8217;<br>\ntranslation.activate(user_language)<br>\nresponse = http.HttpResponse(&#8230;)<br>\nresponse.set_cookie(settings.LANGUAGE_COOKIE_NAME, user_language)<\/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 the string literals of an application have been tagged for later translation, the translation themselves need to be written (or obtained). Here\u2019s how that works. Message Files The first step is to create a message file for a new language. A message file is a plain-text file, representing a single language, that contains all&#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":[2666],"class_list":["post-75937","page","type-page","status-publish","hentry","category-django-web-development","tag-creating-language-files"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Creating Language Files - 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\/creating-language-files-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Creating Language Files - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Once the string literals of an application have been tagged for later translation, the translation themselves need to be written (or obtained). Here\u2019s how that works. Message Files The first step is to create a message file for a new language. A message file is a plain-text file, representing a single language, that contains all...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/\" \/>\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=\"8 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\/creating-language-files-2\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/\",\"name\":\"Creating Language Files - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T07:05:52+00:00\",\"dateModified\":\"2024-04-12T08:47:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Creating Language Files\"}]},{\"@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":"Creating Language Files - 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\/creating-language-files-2\/","og_locale":"en_US","og_type":"article","og_title":"Creating Language Files - Tutorial","og_description":"Once the string literals of an application have been tagged for later translation, the translation themselves need to be written (or obtained). Here\u2019s how that works. Message Files The first step is to create a message file for a new language. A message file is a plain-text file, representing a single language, that contains all...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/","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":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/","name":"Creating Language Files - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T07:05:52+00:00","dateModified":"2024-04-12T08:47:42+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/creating-language-files-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Creating Language Files"}]},{"@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\/75937","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=75937"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75937\/revisions"}],"predecessor-version":[{"id":83452,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75937\/revisions\/83452"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}