# #172 i18n. Set language preferences in session or in User model

- URL: https://superails.com/posts/ruby-on-rails-172-i18n-set-language-preferences-in-session-or-in-user-model
- Published: 2024-06-11
- Duration: 08:30
- Access: free
- Topics: i18n, translation, internationalization
- Playlists: i18n (internationalization)

🚫 The official Rails Guides do not recommend to store locale in session/cookie.

Let's break the rules! 😈

In this episode:
- not logged in users store language preferences in the Session.
- logged in users store language preferences in User model.

This way our routes will stay clean 🙌

## Transcript

Hello friends. So in the previous episode, we added translations to Ruben Res application. Here you see the application, uh, has this, uh, title and subtitle in Spanish because I have the, uh, application slash local Es. And, uh, if I don't it, uh, to English. You see here I have the application in English. I can go to any UL and I will have slash ian or slash uh, uh, es if I select Spanish as a language. And this way, whenever you share a page in your application, everybody will see, uh, the application in the same language that you shared it in. So this is setting the, uh, local from the URL pers. And what if you don't want to have, uh, the, the language in the UL pars? What if you want to have it stored in session or in cookies if the user is not logged in, and if the user is logged in, in the user model? Well, let's do it. Another approach. Let's do it to close this setting the locale from session or cookies. So the official guides say that it is not a good approach because, uh, what if you view the application in Spanish, you send a URL to another user and he sees the application in the English because this is the default language. So, uh, in some cases it's fine. So let's try to do it. So we are going to make it so that users that are not logged in see the application, uh, in English, and they can save their preferences in cookies. If they change the language or in session and logged in, users can update it in their user model. So let's do this. Um, I'm going to do it based on the previous poll, uh, on the previous com comet. Uh, here you can see the changes. So, um, first of all, I'm going to undo the scope. I'm going to use the scopes, we'll need them. Uh, so going back, this should not work, but uh, removing slash es it should work. Okay, so now I can set language, uh, like this. Let's, uh, try to save it works. So it still works, uh, because I have the locale in the URL. Uh, now I'm going to also remove the code going to application controller. I'm going to remove this default URL options. And we are going to set the locale in a different manner. So now, uh, I refresh, we still see this, uh, translate to Spanish because we set the locale from perms locale, but I'm going to remove this. And this way they're going to set the locale to be the default locale. So the default locale is English, therefore it is always in English. Okay? So let's, uh, make it so that the users can actually save it changes in the, let's say session. So I select Spanish and I want the application to be in the Spanish. So, uh, I will say, uh, I think local Equals, or let's actually do it another way. Let's say if PERS locale present, then we'll say, uh, uh, session locale equals pers local two sim, or I don't think we need for the session. Uh, okay. And uh, we will say it locale equals the session locale, or they're going to use the default locale. So this way we are going to store the language in the session. Let's, uh, set it to Spanish. Now, uh, I navigate around the application and I have Spanish set. I click on English and I say the application in the English. So it seems to be broken. Okay, this was, uh, easy. So this way the language is stored in the session. Let's try again. I will, uh, click on English. I will, uh, visit around. Now you see Spanish is highlighted. Once again, why? So let's go to the N bar. Uh, it's highlighted because, uh, yeah, let's display the it end local. Maybe we have an error. Let's say. Yeah, it is Spanish. Let's go here. I will set it to English. I will go back to local host. Okay, it is set to English. So again, switch to Spanish. It is set to Spanish. Yeah, it's spoken. Yeah, just there was some thin cash. That's why it, uh, blinked for a moment. Okay, so it's English, and now it is Spanish vo. Uh, so, uh, this favor said the locale in the session was our application controller. And what if there is a user who is logged in and uh, uh, we want to allow the user to save, uh, the language in his, uh, model. Let's do this. I will say rails generate, uh, uh, migration. Add the locale to user locale, uh, stream. Let's go to add locale to user. Let's, uh, say default, uh, will be n but let, we don't even need to do it because let's make it more dynamic. Let's just use the default language. So, um, yeah, but we can also set it to no false, no, let's just leave it like this. Okay, real to be migrate. Okay, so now the user has, uh, local and uh, let's say if uh, there is a user who is logged in, they're going to use, uh, his, uh, locale. How can we do this? Um, so this is used for sufficient locale. Let's also update it in the user model. If, uh, there is current user, let's say current user dot update, local, prime, local, if user send in, uh, and they're going to use, uh, the current user dot locale if there is a current user or they're going to use the session locale or the default locale. So, uh, let's see if this works. I will start the server once again. Uh, refreshing. Uh, I'm not logged in. Uh, I have it in Spanish. Let me, uh, yeah, let me log in. I have the application in English. Now let me set it to Spanish and, uh, let me visit some page. It is still in Spanish. And let's, uh, run the rails console user dot first. And uh, the local is Spanish. So we managed to update the local of the user looks. Uh, fine. Uh, let's update it to English. Let's go back to the console. And the local is English, so looks good. Let's log out now. And the local is still English. Again, let's try changing to Spanish. Log in, uh, log out. And it is, uh, set again to English because this is the default locale. Again, I will set to Spanish, not been logged in. It'll assist as been Spanish, so looks fine. And, uh, this is basically how you can set the locale, uh, for users that are not logged in in the session, and for users that are logged in based, uh, on the locale that they stored in their user model. So this is, uh, about it. Thanks for win with me and see you in the next one.
