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!!!

 

About Author

Author Image
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.

Request for Proposal

Name is required

Comment is required

Sending message..