#174 i18n. Test translations, refactor, and launch into production! πŸš€

16/06/2024
i18n translation internationalization

The feature is ready, but the code is not production-grade yet.
Let's refactor the code and write some tests for translations.
This way we can go from good to great! πŸ’ͺ

Transcript
Hello friends, and this is the final episode in our translations miniseries. So in the previous episodes, we let our guests and users like their language in the nav bar and we also added translations into Spanish using the open AI and the gem item and tasks. And now as a final step, let's turn our code on translations from good to great and make it ready to be merged into production. So first of all, let's have a look at our application here. Something I see visually, hey, some translations are missing daytime distance in words X days. Now this is a rail specific translation of the method distance of time in inverse or time ago inver. And to fix this, I would need to use the gem rails 18 M Before I add this gem, I will, uh, think, look, I see this error with my eyes, but uh, if I run my tests rails test, I will not see this, uh, error. Why? Because, uh, I need to enable error detection in the development for the translations. So I'm going to say config it and, and raise on missing translations. Now I've restart the server because I have changed something in and initializer, let's run again. And you see I get this error missing translation data. I will also get it if I run my tests. Uh, so to fix this, I'm going to just add the gem rails ITNM for my rub rails version. I will add it, uh, globally and run bundle. Now run bin dev. Once again, refresh and application works. So all the real specific translations have been added and we don't have to manually add them in our translation files. Okay, this is very good. Now let's have a look at the, our application controller. You see this is, uh, translation logic and uh, we don't want to clutter our application controller over this. We can move it into a concern just as we did with our authentication. So let's create another concern. I will, uh, call it, uh, translation, uh, the same pattern as authentication. I would have named it I 18 N, but I will not because 18 n is reserved. So it would give us some crushes. So in two translation, I will, uh, cope with this concern. Name it, uh, translation here again, uh, the name of the module is translation. And let's, uh, clean it up. So this is an, uh, empty concern with the name translation and I'm just going to cope with the code from application controller. So included include the before action. And we are going to add this to private. So everything should work just as before. But now our application controller is much cleaner and we separate the authentication logic where set current user and the logic for translations into separate concerns. Okay, this is good. Let's, uh, just, uh, run our, yeah, have some kind of jobs running. I guess. Lemme stop this bin Dev. Okay, the application is running as before I will change the language and it seems to be working. Let's run our tests rails test and talking of tests we have not written and need test for our translations. So let's do this. Now. A good place would be our maybe controller test. So test controller, but we don't have an actual controller for translations. We keep it in application controller. So let's do it not as a controller test, but as integration test. I will name it translation test. And I can actually control, cope with something from a controller test because the logic is going to be same as in a controller test. So here I will have class, uh, translation test. Let's see. Our test should run successfully previously, and I will say test guest changes locale. So I will get root path locale. I prefer it to be a symbol. Uh, I don't want to assert success or something like this. I will actually assert that we find the, the specific text. So getting the root path in English, I want to see this text. And in Spanish, I want to see this text, but I don't want to hardcode, uh, these, uh, in my test I will actually try to find the, the line in the translation file. So looking at our landing page, it is static landing page title. So I will say, yeah, and this is, uh, I think head of one. Yeah, H one. So I will say, uh, a third select H one, uh, i 18 n dot translate static landing page title and locale will be EM. And I'm going to do the same for Spanish. Let's see if it works. Let's just run only this file. So it'll be foster Rails test, just this file. Okay, success. Let's make it fail. I will put em here. Then actually we have set Es. Okay, so failure works. Let's also test the user changes locale. I will say that we'll sign in user, yeah, this way it'll be faster than you GPT or uh, compiled t. But it's good to have, uh, both, uh, tests when a guest and when a user sign in because in our translation, we have both cases when we have a user and when we don't have a user. And talking of this, uh, currently we set the locale in our session, but uh, if we are doing a PWA or a native app, it would be better to store it in the cookies. So cookies locale, and cookies locale. Let's run our tests once again and everything is working great. So that's basically it. Now our pull request is ready to be pushed into production. Thanks for being with me and see you in the next one.
0
Join the conversation
Sign in to leave comments and save lessons to your watchlist.
We collect your email address, name and username to create your account. We do not share your email address with anyone else.