Internationalzaion using AngularJS
Posted By : Ankit Gupta | 15-Dec-2014
Internationalzaion :
Angularjs provides effective solution for internationalization. Here I am going to explain how you can integrate it in your app.
Download and add following script files :
1. angular-translate.js
2. angular-translate-loader-url.min.js
3. angular-translate-loader-static-files.min.js
Now add 'pascalprecht.translate' dependency in your app module like :
var myApp = angular.module('myApp', ['pascalprecht.translate']);
Now you have to config your app like :
myApp.config(['$provide', '$routeProvider','$translateProvider', function ($provide, $routeProvider,$translateProvider) { $translateProvider.useStaticFilesLoader({ prefix: 'languages/', suffix: '.json' }); $translateProvider.preferredLanguage('en'); }]);
Here prefix and suffix can be anything as you wish to maintain file and directory structure,for above configuration you should store en.json file in webapp/languages.
Write your content in json file like
{ "Dashboard" : "Dashboard" }
Now define a function which can change language,you can put this function in rootscope so that it is available in all pages.
myApp.run(function($rootScope,$translate) { $rootScope.changeLanguage = function (langKey) { $translate.use(langKey); }; });
Now whenever you want to switch language,just call changeLanguage('langKey').
Now you are ready to use i18n in your htmls.
If you want to write Dashboard on html page, then write as
{{ 'Dashboard' | translate }}
Thanks!!!
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankit Gupta
Ankit is a lead java developer well experienced in development of web and enterprise level applications. He has good exposure to enterprise frameworks like Drools , Opta-planner , jBilling and graph databases like Neo4J etc.