Django - How To Translate A Website

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys here is how to do translations so first you must load the I 18 dag in every template that uses the translation tax so let's do that at this also to the child template even if the parent template already has it so now we can do this the translate texts the translation system is enabled by default but you can turn it off in the settings file in here and we also need a place to store the translations I'm going to use the side route so let's at this local paths list so we are going to store these translations in this folder in the site route let's create the folder so Jango looks for the translations in directories that are specified in this list and now let's run make message cease this will generate a message file for the Finnish language now let's add the translation and compile the message file so now if we go back to the settings file and enable the Finnish language we should see the text translated and there it is let's set some tags around Achatz so it's easier to see and there we go you can also use the local middleware to determine the user's language preference so let's go to settings file and add that middleware between the session and common middleware so the first thing the local middleware looks for is URL language prefix if you are using I 18 patterns function in the main URL file so let's try that import I 18 patterns like this and use it for the paths you want to prefix with the language code so I'm going to separate the admin paths so we don't add the language prefix to the admin paths but we added to everything else so now all the paths defined in this space app URLs that PI file we will prepend with the language prefix at this moment there is only one path and that is the front page path so now we can use the language prefix let's try it let's put a fee here and we can see the Finnish translation and let's put en here and now it's the original language you can hide the default language by adding this prefix default language argument here and let's change the default language back to the original and if we add defeat prefix then it's translated next place the local middleware looks for the language preference is the language session key and the language cooky so let's print out the current language in the template so we can see it you can use this tag to get the current language and then let's print it out so here is an example on how to set the language session key manually we can import the translation in here and set the language so this will set the current language that this but this is not persistent that's why we are using session like this and also let's not use I 18 patterns to prepend the current language code so let's disable this for now let's visit the home page so now we can see that the language code is fee even though we had the default as e'en us so the language is determined by the session key value session data is something that is stored in the server cookies are stored in the client computer let's remove the session code and refresh the page the Finnish language is still activated because we made it persistent with the session key now if we delete that session key and visit the home page we should see the original language let's do that so if translation language session key is in here let's delete it so now it's deleted but we are still seeing the Finnish language and this is because the last place the local middleware looks for the language preference is the exit language header that is sent by the user browser so in chrome you can change that language here and you can see it's finished for me so if I move this English to the top and refresh we should see now English language so let's recap the language determination if you are using I 18 patterns in the URLs like this then Django first looks the language prefix in the URL then it's X the session key and possible cookie and after that it's X the HTTP header sent by the browser and if none of those are available then this language will be used let's create a language switzer there is a helper view that you can use to set the language let's add this the main URL spy file to activate it [Music] so now we have a path like this and we can send the language there with the form so let's do that there's an example in the tango documentation on how to create the language Switzer so let's use that code so this is the path we are sending it so if I hit command P in PyCharm we can see where this set language comes from and it's here it's this path so we can reverse this path by using this URL tag let's change the method post and at CS arif token that you need for all forms and then we can specify an input with the name next and this is where the form will redirect you after the language has been changed so you can specify anything you want here in here we will use this redirect to context and let's create the select list for the languages so we already have the current language here now let's get all languages let's use this to get a list that is easier to work with and then loop through it and here we add the options and the value will be the language code and if the current language is equal to the language in the list then we selected and let's print out the language name and here we add the language code at the end let's add the input button to and let's try it and here we need four and there it is we can change the language and these names are translated to the actual language of the selection that's why we used this local here you don't have to show all the languages there so let's restrict the options go to the settings file and specify the languages list so this is a list of tuples so now if we refresh this page we can only see two languages in Python code you can use the get text and its variations to translate messages so let's try that so let's add a title to the page and let's import this so it's imported in here it's useful to use this alias so we don't have to write the whole text instead we can do this and let's set it here so now we can use this in the template now let's make messages and add the translation and compile so now this is translated we can also translate URLs and for that it's recommended to use language prefixes so let's enable those again but this time let's add the admin pages to the list too so we will use the language prefix for admin pages too you must leave this out of the prefix prepend so that's why I separated and now we can translate this like this an import get text translation and we will actually import this lazy version the lazy translations are only translated when they are used in a string context like when you render things in templates again let's make messages and we will find the path here let's add translation compile so now if I go to admin I will see the original language but if I add fee here and use the translated path name we will see a finished translation so there are some default translations already in China for the admin area you can get the translated path in the template files by using the URL tag so let's do that so let's add a link and again use the URL tag and here we can select admin index now let's show it also and let's see the homepage so now if we change the language you can see that the path that the admin page has changed to like that so this is very useful because you don't have to know manually create any logic to determine the paths for different languages so for example I could do some custom path here and then use this name to reverse it in here using this URL tag and it will automatically show the translated path and last let's look at localized formatting so that's owned by default here and this means that when you for example at a date field and print it in the template it will be formatted using localized conventions let's try that so called the models and at the date field make migrations and migrate and now let's use this in the template let's save this and see what happens first we have to add of course a date and there we have it so if I change this language now you can see that the date is formatted in different way thanks for watching if you like this video please like share comment and subscribe see you in the next tutorial bye
Info
Channel: Samuli Natri
Views: 42,211
Rating: undefined out of 5
Keywords: WDTutorials.com, wdtutorials, web, design, development, tutorials, tutorial, Web Development (Industry), video, instructional, django, python, translation, multilingual
Id: xI97sLMd1rM
Channel Id: undefined
Length: 23min 4sec (1384 seconds)
Published: Sat Mar 17 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.