Rails Internationalization (I18n): Step-By-Step

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and thanks for joining me my name is Ilya and today we are going to see how to implement rails internationalization step by step and in this video we are going to cover the following topics setting up i18n working with the translation files translating modal's before men pluralization and localizing date and time I'm going to start by generating a new rail application for this demo I be using rail six point zero point two but most of the concepts can be applied to older versions as well next we are going to generate and you can fooler with an index section and let's tweak the generated view by adding some sample content and then I would like to generate a feedback page so that the users can share their opinion of our a website we are going to be interested in two actions in you and create so last week then you action to fetch all reviews from the database and order them by creation date also I would like to redirect the user to the feedback page when the form is processed and the record is persistent next let's render the feedbacks collection on the new page and create a partial for an individual feedback which is going to display the time while the feedback was created who created it and of the contents next let's take care of the routes and after that I would like to add a global menu to our layout after that you can run your migrations and boot up your server next just navigate to localhost port 3000 and make sure that everything is working fine the next step is deciding which languages we are going to support in our application and you can choose any but I'm going to stick with Russian and English and English is going to be set as the default one so open config application RB file and add the following lines next I would like to hook up a rails by 18 MGM this GM provides local data for vast array of different languages so it has translated names for month it has pluralization rules and other stuff now let's see how to store translations for your website we are going to utilize special Yano files which are stored under the config locales directory so let's replace this sample data with a welcome message and also let's great and our Yudhoyono file for Russian translations and also provide a welcome message here so as you can see we are using a value format and so the key is going to be used inside our code and this key will be replaced with its value based on the currently set locale all right so let's use the key in our index of you now I am going to say T welcome so this T is a special method to actually translate something based on the provided key and so this key here is going to be replaced with its translation once the page is render next let's take care of the second message in this view I am going to create a key called services HTML and this HTML postfix is used to properly display am tell because if you do not provide the HTML postfix then all your HTML markup is going to be displayed as a plain text so that's quite important I know we can use our services HTML key inside our view now let's stop for a second and talk a bit more about this keys suppose we have not two but maybe 500 and keys if all our keys are stored a right under this e n ru key without any grouping then we are going to end up with at least two problems so first of all we would need to make sure that all keys have unique names and also it is going to be hard to locate all the related translations and so it is a really good idea to actually group your translations under some keys so for instance we may say static pages then inside index and inside this index we are going to have our translation keys now we can return to the view and rename the keys here as well but as you can see the names of the skis are become quite long well it's not really convenient but luckily we have a lazy look up so if you are performing translations inside of you or anxiety control and your translation keys are name spaced properly following the folder structure then you can unmeet of the namespaces so you can simply say dot welcome next we can translate our global menu and use some namespace here as well for our convenience and well in this case we can not take advantage of the lazy look up so let's provide a the full name for our key now let's proceed to the feedback page and take here this forum so we need to translate these labels and well rails allows us to provide translations for the Moodle attributes and they will be utilized automatically or you need to namespace these translations properly as well so we should say active record of the and attributes and then of the name of you Moodle which is feedback so the name should be in singular form and then under the name for your model you should provide the actual attributes that you would like to translate now the labels will be translated automatically for you and well as for the submit button you can also provide translation for the Moodle name itself to do so you should say active record models feedback but honestly I do not like this great feedback text on this button so let's stick with a generic submit word and well I'm going to use the global namespace and provide forms submit here next let's utilize this translation and the form is taken care of the next step is to take care of our validations because I do not want my visitors to post empty feedbacks and therefore let's provide some simple validation rules but the question is how do we translate the corresponding error messages but well it appears of that we don't need to do anything because of the rails I ate in GM that we have already installed takes care of that so it knows how to translate human errors one problem however is that our forum contains the subtitle which says well some errors prohibited this feedback from being saved and this message is not translated so let's take here of there and as long a there can be one or more errors than this error wort should be paralyzed accordingly and as you know in English words are usually pluralized by 18 and as specifics but for Russian language things are somewhat more complex so let's create a namespace code messages under forms there there is going to be errors and I'm going to provide two cases for the English language that is one error message and well many error messages so this count part here is interpolation and it is going to be replaced with an actual number and next we can take care of the Russian loci you which has four potential cases and once you're done you can utilize your new translations inside your for and note that we are providing the count value here and this count is going to be used to properly paralyze your error message the next stroke is the feedback partial because here we need to localize this posted by stream and the date time so as for the posted by we are going to utilize interpolation I'm going to just say feedback posted by the author here is going to be an interpolated value but what about the date time the created add attribute so to take care of that attribute we can take advantage of the localize method which is alized as just l so this method is going to produce a translated version of the date and it is going to use the data provided by a 18m rales jail so here I would like to utilize a predefined format a very Scotland but well there are some other available formats or you create you own format so now our application is fully translated but there is quite an important thing missing we cannot change of the low key so let's fix it now and stick with the following approach so our URLs are going to have an optional low key so first of all let's tweak the routes RB file here I would like to include a scope with an optional rpm an X we should say at a before action inside the application controller where we are going to check if the locale is set so if it's not set we are going to just use the default login and if there is some local set then we should check if this local is supported or not and also the next thing is to provide the default URL options method here it's a special method for rails and it is going to include the local pair into every link which we generate with rails helpers and the last thing to do is to present links to actually switch between your locals we are doing that inside our layout also we may our translate of this new feedback right so to do that we can just use this new name space and provide title right here for English and for Russian and after that utilizes this title key by taking advantage of lazy Lordan and so this is it we have just translated our application and we have added an to switch between languages so now our users have two languages to choose from which is really great this tutorial is also available at localized so you can read it of the year and that's all for today folks I thank you for stayin with me and until the next time
Info
Channel: Lokalise
Views: 3,546
Rating: undefined out of 5
Keywords:
Id: hqC2zZqSrTI
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Thu Apr 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.