DEV Community

Cover image for Angular + @ngx-translate + Typings

Angular + @ngx-translate + Typings

Carlos Caballero on July 04, 2019

Introduction When you decide to use Angular in a project, it means that you are building a large SPA (otherwise, you should think of ano...
Collapse
 
vojtech profile image
Vojtech Mašek

I wrote a similar post on translation using lazyloading and dependency injection. Setup is easy and no library is required.
blog.angularindepth.com/angular-ty...

Collapse
 
carlillo profile image
Carlos Caballero Angular

Hi @vojtech !

I've been reading your article and it's great! A pity I have not discovered it before. Both proposals are similar, although different...

In your proposal, interpolation is direct and cleaner (you do not have to use the translate pipe). However, in your proposal you need a module per language and, in addition, that the language is on the route. In my opinion, if a translation receives a parameter it is more inefficient ( the simple fact of being a function). Another problem that I have detected is that when you change the language, you have to reconstruct the view component tree in which the user is, this is caused by

  1. The movement of the route.
  2. It is necessary for the new language to be injected satisfactorily.

Anything that I can help or share, count on me.
Greetings.

Collapse
 
vojtech profile image
Vojtech Mašek

Thanks for the feedback.

Yes, the change of the language is reconstructing the whole view, but it something we are willing to accept as a trade of as users are not changing the language that often. For some products, there is even a logic in cloud functions serving that page which language version will you be redirected to based on the request.

Why we decided to use the DI instead of the pipe was that we wanted to omit the impure translate pipe in our projects.
But some projects have the setup bit adjusted and have translations in ngrx store, displayed using async pipe. They are even combining and "patching" translations that are dynamically received from CMS (where editors write them) with local ones that we keep as the default and for the development. That means there should never be a missing translation in development nor production and everything new is just being merged with the default.

Overall I've seen multiple variations and they are perfectly fine, the best thing is the typings and errors in compile-time :D

Collapse
 
isaacplmann profile image
Isaac Mann • Edited

This looks incredibly useful. Thanks for writing this up.

Does this technique bundle all the language files with the app?

Collapse
 
carlillo profile image
Carlos Caballero Angular

Hi @isaacplmann ,

Only the default language is included in the bundle of the app (something that should work that way). The rest of the languages are downloaded using the WebpackLoader which are downloaded with the names 0.js 1.js ... etc (since this task has been delegated to Webpack).

Thanks!

Collapse
 
phillyvanilly profile image
philly-vanilly

Why would you use ngx-translate in Angular 8 though? The lib is buggy and it's not clear how long it will be supported, considering the official i18n-solution is in beta now and will be released in Angular 9.