Dynamic Translations in Angular

Share on facebook
Share on google
Share on twitter
Share on linkedin

Yet another post of me fixing something I find flawed about the way Angular wants you to do stuff. Just like the way Angular’s built-in way of handling environment configuration wants you to create a build for every environment, Angular’s internalization library wants you to have a separate build for every language. This is BOGUS and I have previously written about why you should opt for a build once, deploy many setup for the most ease and maintainability.

Another point is that an Angular app can benefit from getting the translations from an external API, as this will allow for actually change the translations in the app without needing to redeploy the Angular app. Also, centralizes the translation, as they might be used by multiple clients such as web apps, mobile apps, and back-end services.

This post is going to cover how to fix this by introducing dynamic translation capabilities using ngx-translate for getting translations from a NodeJS REST API.

Creating an Angular app with dynamic translations from a REST API

In this part, we are going to look at how to add translations to a todo app on my Github using Angular, ngx-translate and a NodeJS API for serving the translations. You can find the complete source code in my Github repo with Angular best practices demonstrated in a todo app.

Starting out, the todo app had static English texts hardcoded in the code. I wanted to make it able to support Danish translations as well (my mother language):

The first step is to install ngx-translate/core and ngx-translate/http-loader.

To do this simply open the terminal and type:

npm install ngx-translate/core

npm install ngx-translate/http-loader

Setting up translations on the Node server

From my todo app is a server folder, which contains a simple Express app.

We want to enable our server to serve the translations for our app. We are first going to add two language files to the Node server in the folder assets/i18n called da-lang.json and en-lang.json:

In these files you put your translations for your app, like these English translations for the todo app:

The translations are served on the Node server with:

This will expose everything in the assets folder as static content which can be fetched from eg. our Angular app.

Now, we have created the translations and have served them on the Node server, we can are ready to make our app use the translations.

Use translations in the app

In app.module we set up the app to fetch the translations from the Node server:

The TranslateModule provide us with the TranslateService which we are going to use to initialize the translations upon startup:

This will add the languages, set English as the default language and try to set the browser language if that is available.

To use the translate pipe in the app we import TranslateModule in the SharedModule, which is getting imported by every feature module.

Now the translate pipe can be used like this to translate text in the templates:

Creating a change language dropdown

Because we want the user to be able to change the language dynamically at runtime, we are going to provide a dropdown to change the language in the NavBarComponent.

We are going to create a dropdown of the available languages like this:

This will show a dropdown option for every available language and will set that language on change using the TranslateService‘s use method.

Now you have created an app where you can easily change translations at runtime because they are not hardcoded in the app anymore but fetched from the Node server.


In this post, we saw how to overcome shortcomings of the built-in method of handling translations in an Angular app by using ngx-translate to enable dynamic translation in an Angular app, enabling you to change the translations at runtime in a todo app.

Thanks for reading. Remember to comment, follow me on Twitter and subscribe for getting new posts about how to become a better Angular developer every Sunday.

Do you want to become an Angular architect? Check out Angular Architect Accelerator.

Related Posts and Comments

Build a Course Platform with SPARTAN Stack

Build a Course Platform with the SPARTAN Stack​ Step by Step Guide giving you a full production ready setup with industry best practices. <p style=”text-align: center;”><b>Press for sound.</b><b></b></p> <h2>Here’s what’s inside…</h2> <p >Section 1</p> <h2>Designing the course portal (Figma)</h2> <p style=”text-align: left;”>We will cover the fundaments of designing a brand new site in Figma including

Read More »


Develop Real Projects With Industry Best Practices Join our community of expert developers and get the industry level best practices, step-by-step, from real projects. https://www.youtube.com/watch?v=XHOmBV4js_E Hi, I’m Christian Lüdemann I’m a web developer from Denmark and the creator of this blog. You are here to learn how to code like the best companies and learn

Read More »

Debugging Angular apps (with Sentry) and workflow

In this video, we cover the topic of debugging Angular apps and provide an overview of various tools and approaches that can be used. I highlight the use of Sentry as a tool for error logging and performance monitoring. I also cover my scientific approach to fixing hard-to-debug bugs. Do you want to become an

Read More »