Python Django - Multiple Languages - Internationalization - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to this quick start guide on using django internationalization within your application so in this tutorial we're going to prepare a django application just a small application to offer multiple languages so to give you a little bit of preview of what we're building here um this is a really small scale i may have gone overboard here with the graphics so this is the home page it's in english so we've set up this system for two languages you can add more obviously so we can go to french and now it's uh the whole page will have now turned into french now we can then also traverse and go into other pages now utilizing this new structure this url structure so the fr there is really determining or helping django determine how to translate this page it should be in french for example so there you go it's in french that's all we're really translating is the word hello at this point because if i go back into english and then go back to that page you can see now it's in english so this is a setup where we're going to go for i'll go through the different details we're going to just have a look at this tutorial like i said having a look at the template tags and how to kind of set up a basic template for internationalization now looking at the broader scope of this internationalization and localization here in django we do have a number of key areas here so we can be looking at translating text obviously on the page but internationalization is a little bit more than that because potentially time dates and numbers are all different depending on what languages you're working in and of course we have time zones as well which might also affect your application so in this tutorial we're just focusing on a little bit on translating text so again just to give you a broader view of what's happening here although i'm very focused here on just translating text of course text might be in a model you might find text in the database in the template in the form so here we're just going to focus on a basic setup looking at the templates of translating text in a template so in this tutorial we're going to go through this process which is useful to know before we start this so our process is very simple we're going to mark a string for translation within our template we're then going to run the make messages command and that's essentially going to scan our application for where we've entered or where we've defined strings that need translating and then what we can do we will produce a document and on there we'll be able to then go into our different languages and specify what the translation is so we have to manually type in the translation so once we've done that we then compile those messages into a new file and then we can run our application so before we can actually perform translations here on windows we're going to need to install the gnu get text utilities um so that we can use the make messages commands so what we're going to need to do here then is just head over here to the 3.0 topic slash i 1 8. so this is internationalization just a short word abbreviation for internationalization i1an translation and then here we've got the tech get text on windows i'll leave a link in the description for you um so follow that link and then you'll be presented this link here and this will take you to the pre-compiled binary installer for windows so you'll be presented with this page here you just need to download the 64-bit or the 32-bit so go ahead and download that and then just go run through that install it there isn't really much there to talk about and then you'll just need to make sure that if you want to then run it typically you need to restart the terminal in order for to get that working okay so let's quickly whiz for a set up here with django application so i've created a new project folder in visual studio code here so first thing is then install the vent so use the commands here i'm just going to breeze through these very quickly so set up a new virtual environment and of course we need to activate it and then i go ahead and install django so go ahead and start a new project don't forget the space and the dot at the end is important so it's creating a new project here in our folder and then we can now run the manage pi file so we can then start an app we're going to call this lang so let's just go ahead and just register this application we're not going to be using the database in this case but just because we can so lang that needs to be in there then we're going to set up the urls so let's go ahead and do that so first of all bring in include because we're going to include this set in okay so i've gone ahead and add a new path for this so i'm just referencing the urls in the lang which i'm now going to build so let's just create a new file here called urls dot pi and then inside of here what i want to now do is just specify a single url which is going to be the home page so from the top we've imported the path so i can create url patterns i've then imported the views because we're going to be utilizing a view in a second app name lang to match up the name space in the url course here and then we add a new path here to the home so now that's all set up i can go into my views and i can quickly just create quickly just create a a new view here and it's just going to take in the request okay so what we're going to do here is we're going to start off then our translations right here so the first thing we need to do is identify a string we want to translate right so what we're going to need to do is we're going to need to use the get text tool and this is a tool which is worth reading up a little bit about so if you read through the documentation here you can see that standard translation which we're doing here we can specify translation strings by using the function get text so that's why we're using here so python standard library get text so it's a standard python library now if we want to expand on the slide we're going to need to install in a minute a package for in this case i'm using windows and it's going to be slightly different on your operating system if you're using a different operating system to windows but this is the tool basically that's going to flag the strings that we want to translate then we're going to use a different utility which is going to then translate that into a file which we can then edit the names against the different languages and then we need to compile that file this is what i was explaining earlier in the process so this is pivotal this is a pivotal this is important sorry um tool that we're using here from as it says here the python standard library okay so we bring that in now anything we want to translate here we're using an underscore to define the fact that the next thing is going to be the thing we want to translate the string that we want to translate so let's go ahead now and let's just add a new variable here called uh say trans equals and now i want to translate a string so i use the underscore kind of match this up here and then i then define my string so let's just go for let's just go for hello right okay so now all i'm going to do is just going to render that out onto the page so there we go we're just returning this render i'm presuming that you understand this to understand kind of or how to to actually implement this i'm assuming that you've got some basic knowledge here of django so we're just going to render that out so we're going to need a new folder in lang here so i'm going to create a new folder call that of course uh templates and inside of here i'm just going to put the the new file home so this is the default area django's going to look isn't it for templates um what we're doing html.html home.htm there we go and then we're just going to need some sort of template so again let's just go over to um let's go to bootstrap and just grab a simple template so in the docs in the introduction scroll down go to starter templates copy that apologize if i'm going too quickly here uh there we go so we've got our template there you can pause and quickly grab that if you want to follow this so that's all we need and now of course what we want to do is just output that string which is um referred to as translated or transferry um so we're referencing that string there through trans the data into our template and now we should be able to go ahead hopefully we've done this right and run the server there's no problems so let's go back into our page let's just open up edge again there we go so we have hello so now we want to go through the process of actually translating this into another language so we're going to have to enable our project to work with translation so we're going to need this middleware here so it does specify on this page which is the translation overview page here so this middleware needs to go in a very specific place so let's go into our settings here and in the core so we want to put this after sessions because our language settings can be placed in a session for example so we want the session to run first and then before the common so the common deals with the urls and later on we're going to set up urls so that we can go to different languages based upon the url and that's all going to happen automatically so we want to be able to have that setting before we sort out the urls so this is why we have it before the comment and that's kind of a general overview why it's going to be there so that's important okay so there's some more settings now we want to configure of course we want to specify what language we want to work with so to do that we go down to the bottom or wherever you want to put it here and we just need to now specify some languages so we've got this uh languages here and you can see that i've just specified two languages here and that's going to be english and french now notice also i can actually translate the languages so of course french in french won't look like that will it so we can also translate that too so you can see i've already added the underscore there and i can then add in the import put it at the top of course if you prefer i'll just put it there for now um so you can see what's happening so we can also translate this to which is handy so those are the settings that's going to define what languages that i want to work with now i can also kind of set a default language here so language code for example so i can set a default language if i wanted to um and that's pretty much it we're going to need there apart from one more thing so what's going to happen is we're going to scan like i said for the translations that are needed that we specified and they need to go into a folder so let's create a a new folder here just to follow the same kind of um naming conventions so inside of here i'm going to create a new folder one for english en and then i'm going to create a new folder for french there we go so those are my two languages i've selected okay so now i've done that and now i've actually got something needs to be translated i can now run the tool to kind of scan find those and create a new file which i can then edit in the french for example and then make the translation okay so let's do that so py manage pi and now let's run the make messages and we just run the the all so we're just going to blanket this so that should take a couple of seconds and notice straight away now inside of our folders we now now have the the profile so let's go over to the french profile and let's have a look in here because we can see if we go down um we have the word hello and that was in my view so it's identified that this needs translated so hopefully by at this point you can see what's happening here anything you flag so we've got french and english which was flagged um is now going to be in this file among other things here um that we've utilizing the all kind of selected so bonjour uh hello in french okay so that's the french equivalent right so now we've done that we've got this in place we now once we're happy with that let's just go back into here and now we want to run a new command uh compile this or these settings so we can actually use them so let's go ahead and now run that so this is going to be compile messages so let's now go back into our view and make a few changes here so what we need to do is bring in some tools because we want to make a translation and send it to the the template that's what we're going to do here so let's bring in some tools so from translation there's a number of different tools and i've brought them in this way just to show them get language activate and of course get text we're using earlier so get language is going to be a tool that allows us to identify language we're currently using activate is going to activate a language and then get text that's obviously where we define what the string that we want to translate and of course before we're just referencing it as the underscore here to save us write get text everywhere so it's like a a nice and easy way of working so let's first of all we're going to make a new function here and called translate and we're going to pass in a language of fr fr which is one of our languages that we translate our text into our strings into in this system that's what we defined in our setting force at the bottom so one thing also to notice that before i didn't add the comma to the middleware so just make sure we've got that comma otherwise you'll receive an error okay so back in our view then so we're going to create a new function here called translate i'm going to pass in the ver language here to it fr and now what i want to do is get the current language so i put the current language in this um variable here and what i can now do is print it out if i wanted to to see it so that's going to produce the current language so i can go ahead and try and activate the language that i've passed in which is french so i then i'm going to select something i want to translate again i can just use the underscore there if i wanted to but i'd pour that in just to purposely show you what's going on it's the same thing so next up then i try and activate if i can't activate then i'm basically going to just activate my current language whatever that might be as like a fallback and then i return the text so i return the translated text over to my function here and then i'm going to output that so uh let's uh have a look to see if that works so if you remember before what we had was just i think it was just hello so now if i refresh it now says it now says hello that was a surprise so let's work out why this is so if you go into the settings if you remember before we made a folder here but what we actually haven't done is we actually haven't told django where this folder is so we've gone through the process of actually utilizing it looks like it's been the default folder to actually create all these files within this named folder here um so it looks like we've been following the pattern that's already preset but we haven't actually told django uh where it is so let's go ahead and do that so we can do that with the path here so base directory and the name of the folder whatever it is you want it to be so let's go up here and just let's uh import os i'm going to need so we can access the folder structure of our operating system so based directory and the folder name so now we've got that in place let's just run our server again and now let's refresh and there we go we now have the translations so hopefully this gives you a general idea now what's happening here with these commands how it's working we need to activate the new language how we've used get text to actually identify what to change and then also the getting the current language okay so now we can go a little bit more crazy i guess and let's go into our into our template and now let's add something here that we want to so now for us to work in a template here what we're going to need to do is to load in the internationalization tool so i 1a n at the top here so that needs to be up the top and then you can see here i've used a template tag here of translate and then i'm just asking django to translate here hello so you can see i can use the transl translate here in the title to change the title so let's also put this here um just before the trans so this was the the translation we performed earlier on the word hello so we've got that in so now of course at the moment we're not actually going to be translating this because our default language is english so let's just go back to here and just refresh so you can now see it says hello bonjour and up top here um this is obviously the title name and it's still hello so we're now going to build a very quick kind of switch so that we can change between languages so i just add some extra html and just build this up slightly so it looks a little bit more effective as if we're going to implement it in our page so let's go to the docs here and i'm just going to go to the introductory and i just need to bring in the javascript bundle here so i'm just going to copy that javascript bundle and just place it just below here so that's the javascript bundle uh for bootstrap so i can then run a drop down and then one just go down into the components here and i'm just going to select a navbar and just grab the top nav bar that has a drop down so just copy this move across you can just check out the code say you haven't kind of follow all this so i've done that and then i've got rid of the form to form the form at the end of this and then we want to keep this we want to get rid of disabled list item here and then we're going to keep uh this drop down menu here and this li here so we're going to call this where it says drop down i'm going to say language and then i want to get rid of this link here and then we'll just keep the link to the home page for now um i'm going to call that i'm just going to make a new page in a minute so let's just call that item page for example and then we'll remove the active so it's not a different color and yeah i think um there's no href no okay so that's pretty much it so if we now look at our page we should have a nice little navigation bar at the top here if i just uh zoom out a bit we have the language drop down working in a minute um notice it's not working um maybe i've got rid of uh let's have a look at the top here okay so for some reason it's not working so the reason why it's not working is because i've added the script here just to show you mine's already included at the bottom here um so i at the top or at the bottom it doesn't matter so let's just go back and refresh so you can now see that we've got the drop down so obviously we want to now place all the languages that we have available on our system in this drop down so we can select it and change the language of the page so with the following approach what we're doing is we're going to build urls and in the url django is going to automatically place the current language that's set and by doing that so there's nothing we need to do we just continue utilizing our normal paths and this will just automatically happen when we select different languages so we have this prefix which is the language and that then also allows django to determine what language to display this page in so this is what's going to happen let's just set this up and we can see this happening so let's first we'll go into our we're going to need to go into our core and we're basically going to need to define um what paths we want to include in our internationalization so let's just extract it from here i'm just going to separate this i should do the way shouldn't i i don't know yeah let's do it away so let's just uh copy this down okay so we're just gonna have one path so essentially what we're gonna do here is we're going to flag or we're going to detail what paths we want to use for internationalization so let's just bring in the tools first so the i18n patterns and then what we're going to need to do is just add now so instead of um equals we're just going to add this to the urls we're just doing it separately here so you can see what's going on and there we go so basically any of the paths are inside of here remember this is the includes in the core so we can't utilize the iating patterns on an included set of urls we have to do it here on the core that's important so we can't do this um in the lang project so this is done then in the core urls so we've enabled the patterns and then we've gone off and we've kind of identified then the internationalization paths that we want to activate and of course then anything in there can then be utilized for that so what we can also do um let's not forget is we can use our um i'll get texts just about anywhere so here for example if we wanted to have separate names for um the admin well we could we could do that so let's just enable that for example there we go so potentially we could um we'll look at this in further tutorials but we could change the url potentially or translate the url anyway so let's move down to here so it plus equals so we're going to add this to the main urls essentially and then all the other urls that we have obviously within i say obviously in our urls here so we're just adding them as per normal right so we've only got one url here and that's home so that is now enabled so now let's go back into our views oh sorry our template here and let's just set this up so i'm just going to remove these two items here and the translations and we're just going to add now a little uh set up the drop down so we'll just remove the items that are inside of this drop down existing drop down so i'll get rid of those okay so now we need to iterate over the languages that are available in order for us to make this list we want to build so let's have a look at some of the tools that we can bring in here so we can get the current language if we want as language code and then we can go ahead and use the get available languages so we can get the current language and get the available languages now what we want to do is we want to kind of iterate over them and to list them out of course so if we go into the django documentation here we can we can have a look at some of these commands so the uh get language info list um to retrieve information for the list of languages so we can use it to iterate over the languages in the template so for available languages lengths and then we can loop it out so let's do that so we're gonna essentially now um just uh list these out so let's drop this into our code here so we're gonna use the get language info list for languages as languages now essentially what we're doing here is we're just getting a list of all the languages so with our list then in languages let's go ahead and iterate so for um for each item in languages we're just going to create a new list item um and then we are then going to create a link in here for that so um this is going to be a drop down item this is a bootstrap class here and the href is going to be slash then the language code so that's what we want to put there slash language code slash and to build that up and then we can then add the language local name um so that's going to be obviously the name of the tag there right so now we've got that in that literally just loops around and then sets up the link for it so what we're doing here is we're actually setting up the link here and this can be expanded and changed because we could also because you'll see that when we click on this it's going to take us into essentially the home page all the time but it is going to change the language and by adding that there the prefix of our url like i said django is going to then be able to determine what language we want to display or render that page in so with that in place let's have a look to see what this looks like so we now have um oh we've now got a problem let's go back into our server then um so it looks like we've got an error here so core urls line 11 okay so line 11 we've got our url patterns okay so what we're going to need here is the parentheses so not the brackets there so okay i should fix that and let's refresh okay so we've got our languages so at the moment um it doesn't look like they're appearing the actual text isn't appearing but and you can see that it's going to be taking us to the right place um so let's have a look at why that isn't working so i've just renamed this incorrectly so you can see here that each time we look through we're referring to the item as lang so let's just uh put that back to lang so now it should output the actual name so there we go english and french so you can see as i click on english and friends it's changing the prefix there um to the the rest of the url so what we're going to do now is just um well let's have a look shall we so let's just add something in here let's just get rid of this action we're not going to need that um so let's just go ahead and add a new div here just build a quick container so inside of here if you remember we passed in from our view we were passing in the translated hello but this time what we're going to do here is not pass that in we're just going to ask to be trans ask an item or string to be translated here on the template so if you remember previously we use the translate template tag and then here i'm just going to put the string hello so now that should be translated so let's have a look and we're going to refresh we've got bonjour because we're in french and if we go to english that changes back to hello so remember that we've been through that process um of creating those and defining those translations if yours doesn't work and remember you then need to once you've created your translation strings that you want to translate remember then you need to run the command so that command remember was make messages so that's then going to create the files remember on the left hand side here so we can go into our profiles and we've changed hello if we go into the french version now you can see that we've changed that manually and then i then gone to they've gone and compiled this utilizing the compile messages no space so that compiled it remember and that's why this is working so there we go that's a simple example for us to start off with okay so you don't have to take my word for this let's just make a new page um i think we started to so yeah let's make a quick url just to showcase this so we call this item slash views and then item so name is going to be item so just make a new page just to show that it will affect all the other pages once we've started this and you can then see how that prefix works and then gets added um to the base of the url so we're going to go to the path item so let's go into our views we're just going to change this home i've copied this down into item and then we're not going to need that so we're just going to return the template item we're not going to pass anything in there's no context we need to send across so that's what we've got for the item so let's just go into our home we copy and paste that we call that item so that's our template and then of course um there's nothing we need to do there because it's gonna just translate that's all good so let's have a look at that um oh i'm gonna need a link can't i'm gonna need to link to it but that's cool so when i now go to item slash you can now see it's also being translated so just to confirm that's the item page let's just uh write something here in a p tag for example it's going to be the item page okay so now we can go back here just refresh it says the item page bonjour and let's just go into english and you can see it's in english but it takes us back to the home page remember because we've not set that up um the go to item and you can now see it's back in english so if you take a look at the djangopackages.org and have a look for internationalization there's a few package popular packages here potentially um that you might want to take a look at to add some functionality and so take a look at some of these um for example uh we could look at for example the django rosetta that could be useful um the parlor software here and django solid um i18n urls um so there's a few softwares here that you might want to take a look at maybe i can feature them in further tutorials okay so that's a a brief introduction then to django internationalization hopefully that's got you started now thinking about uh now incorporating and just go yeah just really getting you started utilizing django internationalization now have a look through the documentation um it probably makes it a lot easier now to work with the documentation in that you've now got a an example to work with i feel so the django documentation how it is wonderful it's absolutely wonderful but it was a little bit confusing to get things at least started to this point to move on so hopefully this has been useful and as for normal hopefully i'll see you in the next tutorial
Info
Channel: Very Academy
Views: 7,482
Rating: 4.9807692 out of 5
Keywords: python, django, python django, django il8n, django internationalization, django multiple languages, django 2021, django tutorial, djangotutorial, django tut, django 3, django examples, learn django, django beginners, beginners django, django example
Id: z_p8WxFGV5A
Channel Id: undefined
Length: 32min 37sec (1957 seconds)
Published: Tue Feb 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.