{"id":23064,"date":"2015-07-17T15:14:49","date_gmt":"2015-07-17T09:44:49","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23064"},"modified":"2017-08-03T15:50:16","modified_gmt":"2017-08-03T10:20:16","slug":"internationalization-with-angularjs","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/internationalization-with-angularjs\/","title":{"rendered":"Internationalization  with AngularJS"},"content":{"rendered":"<h3><strong><span style=\"color: #000000;\">Why is internationalization\u00a0support so important?<\/span><\/strong><\/h3>\n<p>These days variety of web projects demand the support for multiple languages\u00a0feature. Most of the internet users do not have English as their first language. Thus there is a need to reach those users as well. There are many modules available to achieve multilingual feature in <a title=\"AngularJS Development Company\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">AngularJs<\/a> like angular-gettext, angular-translate. We&#8217;ll cover the basics of angular-translate, configurations with some\u00a0examples.<\/p>\n<p><strong>What is angular-translate?<\/strong><\/p>\n<p>It is a module which helps to implement internationalization\u00a0features in AngularJs. It provides us with components like filters, <a href=\"http:\/\/www.tothenew.com\/blog\/deep-dive-into-angular-directives\/\">directives<\/a>, and functions to load i18n data asynchronously. It uses JSON format messages to achieve ease of implementation.<\/p>\n<h3><\/h3>\n<h3><strong>Configuration<\/strong><\/h3>\n<p>You can use bower to install &#8216;angular-translate&#8217; and &#8216;angular&#8217;.<\/p>\n<p>[code lang=&#8221;html&#8221;]<br \/>\nbower install angular<br \/>\nbower install angular-translate<br \/>\n[\/code]<\/p>\n<p>Here is the CDN\u00a0link for angular and angular-translate:<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nhttps:\/\/cdnjs.cloudflare.com\/ajax\/libs\/angular.js\/1.3.16\/angular.min.js<br \/>\nhttps:\/\/cdnjs.cloudflare.com\/ajax\/libs\/bower-angular-translate\/2.7.1\/angular-translate.min.js<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n<h3>Setting up your page<\/h3>\n<p>When setting up your page, you have to include angular.js and angular-translate.js file (index.html).<\/p>\n<p><!-- cdn link https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/angular.js\/1.4.2\/angular.min.js --><\/p>\n<p>[code lang=&#8221;html&#8221;]&lt;div&gt;<br \/>\n  &lt;h3&gt; Hello&lt;\/h3&gt;<br \/>\n  &lt;p&gt;This is a demo app for multilingual support.&lt;\/p&gt;<br \/>\n  &lt;label&gt;Click here for switching language between spanish to english &lt;\/label&gt;<br \/>\n  &lt;button&gt; Change&lt;\/button&gt;<br \/>\n&lt;\/div&gt;<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>Adding dependency in your app\u00a0<\/strong><\/h3>\n<p>Inject &#8216;pascalprecht.translate&#8217; module in your app:<\/p>\n<p><code>var MultilingualApp = angular.module('MultilingualApp', ['pascalprecht.translate']);<\/code><\/p>\n<p>&nbsp;<\/p>\n<h3>\u00a0Configure $translateProvider in your app<\/h3>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nMultilingualApp.config(function ($translateProvider) {<br \/>\n  $translateProvider.translations(&#8216;US_EN&#8217;, {<br \/>\n    &#8216;GREETING&#8217;: &#8216;Hello !&#8217;,<br \/>\n    &#8216;DESCRIPTION&#8217;: &#8216;This is a demo app for multilingual support.&#8217;,<br \/>\n    &#8216;LBL_SWITCH&#8217;:&#8217;Click here for switching language between spanish to english&#8217;,<br \/>\n    &#8216;CLICK&#8217;: &#8216;Click&#8217;<br \/>\n  });<\/p>\n<p>  $translateProvider.translations(&#8216;SPANISH&#8217;, {<br \/>\n    &#8216;GREETING&#8217;: &#8216;Hola !&#8217;,<br \/>\n    &#8216;DESCRIPTION&#8217;: &#8216;Esta es una aplicaci\u00f3n de demostraci\u00f3n para soporte multiling\u00fce.&#8217;,<br \/>\n    &#8216;LBL_SWITCH&#8217;: &#8216;Haga clic aqu\u00ed para cambiar el idioma entre espa\u00f1ol al Ingl\u00e9s&#8217;,<br \/>\n    &#8216;CLICK&#8217;: &#8216;Clic&#8217;<br \/>\n  });<\/p>\n<p>\/\/It sets default language for your app<br \/>\n$translateProvider.preferredLanguage(&#8216;US_EN&#8217;);<br \/>\n});<br \/>\n[\/code]<\/p>\n<h3><\/h3>\n<h3>Add dependency in your controller<\/h3>\n<p>In your controller inject &#8216;$translate&#8217; service.<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nMultilingualApp.controller(&#8216;MyCtrl&#8217;, function ($scope, $translate) {<br \/>\n  $scope.changeLanguage = function () {<br \/>\n    var language = $translate.use();<br \/>\n    if (language === &#8216;US_EN&#8217;) {<br \/>\n      $translate.use(&#8216;SPANISH&#8217;);<br \/>\n    } else {<br \/>\n      $translate.use(&#8216;US_EN&#8217;);<br \/>\n    }<br \/>\n  };<br \/>\n});<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<p>$translate.use() : When it is called without parameter it is treated as getter function. It will return current key (language). If you pass parameter then it is acted like setter function and it will reset the language.<br \/>\nYour angular\u00a0application\u00a0should resemble to following piece of code:<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nvar MultilingualApp = angular.module(&quot;MultilingualApp&quot;, [&quot;pascalprecht.translate&quot;]);<\/p>\n<p>MultilingualApp.config(function ($translateProvider) {<br \/>\n  $translateProvider.translations(&quot;US_EN&quot;, {<br \/>\n    &quot;GREETING&quot;: &quot;Hello !&quot;,<br \/>\n    &quot;DESCRIPTION&quot;: &quot;This is a demo app for multilingual support.&quot;,<br \/>\n    &quot;LBL_SWITCH&quot;: &quot;Click here for switching language between spanish to english&quot;,<br \/>\n    &quot;CLICK&quot;: &quot;Click&quot;<br \/>\n  });<br \/>\n  $translateProvider.translations(&quot;SPANISH&quot;, {<br \/>\n    &quot;GREETING&quot;: &quot;Hola !&quot;,<br \/>\n    &quot;DESCRIPTION&quot;: &quot;Esta es una aplicacion de demostracion para soporte multilingue&quot;,<br \/>\n    &quot;LBL_SWITCH&quot;: &quot;Haga clic aqu\u00ed para cambiar el idioma entre espanol al Ingles&quot;,<br \/>\n    &quot;CLICK&quot;: &quot;Click&quot;<br \/>\n  });<br \/>\n  $translateProvider.preferredLanguage(&quot;US_EN&quot;);<br \/>\n});<\/p>\n<p>MultilingualApp.controller(&quot;MyCtrl&quot;, function ($scope, $translate) {<br \/>\n  $scope.changeLanguage = function () {<br \/>\n    var language = $translate.use();<br \/>\n    if (language === &quot;US_EN&quot;) {<br \/>\n      $translate.use(&quot;SPANISH&quot;);<br \/>\n    } else {<br \/>\n      $translate.use(&quot;US_EN&quot;);<br \/>\n    }<br \/>\n  };<br \/>\n});<br \/>\n[\/code]<\/p>\n<h3><\/h3>\n<h3>Update \u00a0view<\/h3>\n<p>There are three ways to update your view to enable internationalization.<\/p>\n<ul>\n<li><strong>Using Filter<\/strong><\/li>\n<li><strong>Using Directive<\/strong><\/li>\n<li><strong>Using Service<\/strong><\/li>\n<\/ul>\n<h4><\/h4>\n<h4>Using Filter:<\/h4>\n<p>Update your view file\u00a0using &#8216;translate&#8217; filter. It will convert your static text into i18n messages.<\/p>\n<p>[code lang=&#8221;html&#8221;]<br \/>\n&lt;h3&gt;{{&#8216;GREETING&#8217;|translate}} &lt;\/h3&gt;<br \/>\nwill converted into<br \/>\n&lt;h3&gt; Hello&lt;\/h3&gt;<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<h4>Using\u00a0Directive:<\/h4>\n<p>[code lang=&#8221;html&#8221;]<br \/>\n&lt;h3&gt; &lt;pre translate=&quot;GREETING&quot;&gt;&lt;\/pre&gt;&lt;\/h3&gt;<br \/>\nwill translated into<br \/>\n &lt;h3&gt; Hello&lt;\/h3&gt;<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<h4>Using Service:<\/h4>\n<p>If you are using service to\u00a0convert your static text into i18n messages. Then first you have to inject $translate service into your controller.<\/p>\n<p>[code lang=&#8221;javascript&#8221;]<br \/>\nMultilingualApp.controller(&quot;MyCtrl&quot;, function ($scope, $translate) {<br \/>\n  $translate(&#8216;GREETING&#8217;).then(function (translation) {<br \/>\n    $scope.greetingText = translation;<br \/>\n   });<br \/>\n});<br \/>\n[\/code]<\/p>\n<p>View should be look like this:<\/p>\n<p>[code lang=&#8221;html&#8221;]<br \/>\n &lt;div ng-controller=&quot;MyCtrl&quot;&gt; &lt;h3&gt;{{greetingText}}&lt;\/h3&gt; &lt;\/div&gt;<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<p>Here I have used filter for enabling\u00a0\u00a0view for internationalization which is mostly used.<\/p>\n<p>[code lang=&#8221;html&#8221;]<br \/>\n&lt;html&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;script src=&quot;angular.min.js&quot;&gt;&lt;\/script&gt;<br \/>\n&lt;script src=&quot;angular-translate.min.js&quot;&gt;&lt;\/script&gt;<br \/>\n&lt;script src=&quot;app.js&quot;&gt;&lt;\/script&gt;<br \/>\n&lt;div ng-app=&#8217;MultilingualApp&#8217;&gt;<br \/>\n    &lt;div ng-controller=&#8217;MyCtrl&#8217;&gt;<br \/>\n        &lt;h3&gt;{{&#8216;GREETING&#8217;|translate}}&lt;\/h3&gt;<br \/>\n        &lt;p&gt;{{&#8216;DESCRIPTION&#8217;|translate}}&lt;\/p&gt;<br \/>\n        &lt;label&gt;{{&#8216;LBL_SWITCH&#8217;|translate}} &lt;\/label&gt;<br \/>\n        &lt;button ng-click=&#8217;changeLanguage()&#8217;&gt; {{&#8216;CLICK&#8217;|translate}}&lt;\/button&gt;<br \/>\n    &lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt;<br \/>\n[\/code]<\/p>\n<p>&nbsp;<\/p>\n<p>You can try it out yourself on JSFiddle\u00a0<a title=\"multilingualDemoApp\" href=\"https:\/\/jsfiddle.net\/dhirajsharma072\/8yrez1v5\/\" target=\"_blank\">multilingualDemoApp<\/a>.<\/p>\n<p>To learn more\u00a0about angular-translate visit\u00a0<a title=\"angular-translate\" href=\"https:\/\/github.com\/angular-translate\/angular-translate\" target=\"_blank\">angular-translate<\/a>.<\/p>\n<p><span style=\"font-size: 20px; color: blue;\">Read Further on AngularJS Series<\/span><\/p>\n<ul>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-a-write-less-do-more-javascript-framework\/\">AngularJS :\u00a0A \u201cWrite Less Do More\u201d JavaScript Framework<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-updating-a-label-dynamically-with-user-input\/\">AngularJS :\u00a0Updating a Label Dynamically with User Input<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-adding-items-to-a-javascript-list-and-updating-corresponding-dom-dynamically\/\">AngularJS :\u00a0Adding Items to a JavaScript List and updating corresponding DOM Dynamically<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-text-suggestions\/\">AngularJS :\u00a0Text Suggestions<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-sorting-objects-on-various-attributes\/\">AngularJS :\u00a0Sorting objects on various attributes<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-fetching-data-from-the-server\/\">AngularJS :\u00a0Fetching data from the server<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-multiple-views-layout-template-and-routing\/\">AngularJS :\u00a0Multiple Views, Layout\u00a0Template\u00a0and Routing<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/angularjs-implementing-routes-and-multiple-views\/\">AngularJS :\u00a0Implementing Routes And Multiple Views<\/a><\/li>\n<li><a href=\"http:\/\/www.tothenew.com\/blog\/building-intuitive-frontend-interfaces-with-angularjs\/\">Building Intuitive Frontend Interfaces with AngularJS<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Why is internationalization\u00a0support so important? These days variety of web projects demand the support for multiple languages\u00a0feature. Most of the internet users do not have English as their first language. Thus there is a need to reach those users as well. There are many modules available to achieve multilingual feature in AngularJs like angular-gettext, angular-translate. [&hellip;]<\/p>\n","protected":false},"author":95,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":2},"categories":[1439,1],"tags":[3955,4697],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23064"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/95"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23064"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23064\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}