Introducing Transloco: Angular Internationalization Done Right
For a while I’ve been thinking about creating an Angular i18n library, which incorporates some concepts I had in mind.
A couple of weeks ago I was bored, so I decided that’s it time to take the bull by the horns and go ahead and create a robust library, chock-full of all the features I’d expect from such a library.
I recruited Shahar Kazaz and Itay Oded to the task; Together, we worked hard over the weeknights, and that’s how Transloco was born.
I’ve also created the
ng-neat organization, to hold all my open source libraries. Currently it only holds Transloco, but I also plan to transfer Spectator, ngx-until-destroy, ngx-content-loader, and any future open-source Angular libraries I create.
Let’s take a look at what Transloco has to offer.
Install the library using Angular CLI:
ng add @ngneat/transloco
As part of the installation process you’ll be presented with questions; Once you answer them, everything you need will automatically be created for you. Let’s take a closer look at the generated files.
First, Transloco creates boilerplate files for the requested translations:
Next, it injects the
TranslocoModule into the
AppModule, and sets some default options for you:
Let’s explain each one of the
listenToLangChange: Subscribes to the language change event, and allows you to change the active language. This is not needed in applications that don’t allow the user to change the language in runtime (i.e., from a dropdown), so by setting it to false in these cases, you can save on memory by rendering the view once, and unsubscribing from the language changes event (defaults to
defaultLang: Sets the default language
fallbackLang: Sets the default language/s to use as a fallback
failedRetries: How many time should Transloco retry to load translation files, in case of a load failure (defaults to 2)
It also injects the httpLoader into the AppModule providers:
HttpLoader is a class that implements the
TranslocoLoader interface. It’s responsible for instructing transloco how to load the translation files. It uses Angular HTTP client to fetch the files, based on the given path.
Translation in the Template
Transloco provides three ways to translate your templates:
Using the Structural Directive
This is the recommended approach. It’s DRY and efficient, as it creates one subscription per template:
Using the Attribute Directive
Using the Pipe
Sometimes you may need to translate a key in a component or a service. To do so, you can inject the
TranslocoService and use its
But Wait — There’s More!
This was just a taste of Transloco, it also supports:
You can find everything you need, along with examples, in our official README file.
Migration from ngx-translate
Transloco provides a schematics command that will help you with the migration process.
Happy translating, and don’t forget to show your appreciation by starring!
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...
With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...
You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...