{"id":774,"date":"2023-04-13T05:00:00","date_gmt":"2023-04-13T12:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=774"},"modified":"2023-04-11T15:01:50","modified_gmt":"2023-04-11T22:01:50","slug":"how-to-internationalize-your-angular-application","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-internationalize-your-angular-application\/","title":{"rendered":"How to internationalize your Angular application?"},"content":{"rendered":"\n<p>Yesterday, we touched on how to change the locale of our Angular applications. The next step is to fully internationalize our apps by translating all text into different languages.<\/p>\n\n\n\n<p>While Angular has its <a href=\"https:\/\/angular.io\/guide\/i18n-overview\" target=\"_blank\" rel=\"noopener\" title=\"\">localization implementation<\/a> to internationalize applications, the most popular library used for such a task is <a href=\"http:\/\/www.ngx-translate.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">ngx-translate<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"381\" height=\"85\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-19.png\" alt=\"\" class=\"wp-image-786\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-19.png 381w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-19-300x67.png 300w\" sizes=\"auto, (max-width: 381px) 100vw, 381px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p><strong>Why not use Angular&#8217;s internal implementation?<\/strong> The main reason is that Angular builds one version of your application per locale. As a result, if you have to support 20 different locales, your Angular build would output 20 different applications. This means that changing the site would result in a complete reload of the application. This also means that changing a translation requires a complete rebuild and redeploy of the application.<\/p>\n\n\n\n<p><strong>ngx-translate<\/strong> is much more flexible because it relies on pipes, directives, and services to translate our text at runtime instead of build-time. Here are some examples:<\/p>\n\n\n\n<p>With <code>TranslateService<\/code>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"863\" height=\"162\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-20.png\" alt=\"\" class=\"wp-image-787\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-20.png 863w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-20-300x56.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-20-768x144.png 768w\" sizes=\"auto, (max-width: 863px) 100vw, 863px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>With <code>TranslatePipe<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"111\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-21.png\" alt=\"\" class=\"wp-image-788\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-21.png 862w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-21-300x39.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-21-768x99.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>With <code>TranslateDirective<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"113\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-22.png\" alt=\"\" class=\"wp-image-789\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-22.png 865w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-22-300x39.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-22-768x100.png 768w\" sizes=\"auto, (max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Translations come from specific files where translation keys (<code>\"HELLO\"<\/code> in my example) are mapped to actual values in each language. <code>ngx-translate<\/code> supports different formats out of the box (such as <a href=\"https:\/\/github.com\/ngx-translate\/http-loader\" target=\"_blank\" rel=\"noopener\" title=\"\">JSON<\/a> and <a href=\"https:\/\/github.com\/biesbjerg\/ngx-translate-po-http-loader\" target=\"_blank\" rel=\"noopener\" title=\"\">PO<\/a>).<\/p>\n\n\n\n<p>Here is an example of English translations stored in <strong>en.json<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"187\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-23.png\" alt=\"\" class=\"wp-image-790\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-23.png 875w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-23-300x64.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-23-768x164.png 768w\" sizes=\"auto, (max-width: 875px) 100vw, 875px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>And the same translations in French in <strong>fr.json<\/strong>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"870\" height=\"184\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-24.png\" alt=\"\" class=\"wp-image-791\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-24.png 870w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-24-300x63.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-24-768x162.png 768w\" sizes=\"auto, (max-width: 870px) 100vw, 870px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>All we have to do is <a href=\"https:\/\/blog.angulartraining.com\/how-to-internationalize-i18n-your-angular-application-tutorial-dee2c6984bc1#:~:text=For%20instance%2C%20you,i18n\/%27)%3B%0A%7D\" target=\"_blank\" rel=\"noopener\" title=\"\">indicate where the translation files are located<\/a> and <code>ngx-translate<\/code> will download them as needed based on the locale.<\/p>\n\n\n\n<p>Defining which languages are supported as well as which one to use by default can all be done with the <code>TranslateService<\/code> from the library:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"865\" height=\"287\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-26.png\" alt=\"\" class=\"wp-image-793\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-26.png 865w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-26-300x100.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/04\/image-26-768x255.png 768w\" sizes=\"auto, (max-width: 865px) 100vw, 865px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>You can see a live example of <code>ngx-translate<\/code> in <a href=\"https:\/\/stackblitz.com\/edit\/ngx-translate-at?file=src%2Fapp%2Fapp.component.ts\" target=\"_blank\" rel=\"noopener\" title=\"\">Stackblitz here<\/a>. If you want to try it, you can use my <a href=\"https:\/\/blog.angulartraining.com\/how-to-internationalize-i18n-your-angular-application-tutorial-dee2c6984bc1\" target=\"_blank\" rel=\"noopener\" title=\"\">ngx-translate step-by-step tutorial<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday, we touched on how to change the locale of our Angular applications. The next step is to fully internationalize our apps by translating all text into different languages. While Angular has its localization implementation to internationalize applications, the most popular library used for such a task is ngx-translate. Why not use Angular&#8217;s internal implementation? [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,27],"tags":[],"class_list":["post-774","post","type-post","status-publish","format-standard","hentry","category-angular","category-i18n"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/774","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=774"}],"version-history":[{"count":4,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/774\/revisions"}],"predecessor-version":[{"id":796,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/774\/revisions\/796"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}