Translate the content of page in angularJS
Posted By : Monu Thakur | 26-Jun-2017
We can translate the content from one language to another language using angular-translate. This is javascript translation library. We can use angular-translate in our project with the help of NPM and Bower. NPM and Bower are package manager. Below are syntax which is use to download angular-translate in our project.
NPM npm install --save-dev angular-translate
Bower bower install --save-dev angular-translate
On the click of Button execute the changeLanguage() function in controller It convert the one language to another language.
Example:
Firstly you add angularjs file in your project
https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js
Create myApp.js file
var app = angular.module('myApp', ["pascalprecht.translate"]).config(function ($translateProvider) {
$translateProvider.translations('en', {
TITLE: 'Hello',
FOO: 'This is a paragraph.',
BUTTON_LANG_EN: 'english',
BUTTON_LANG_DE: 'german'
});
$translateProvider.translations('de', {
TITLE: 'Hallo',
FOO: 'Dies ist ein Paragraph.',
BUTTON_LANG_EN: 'englisch',
BUTTON_LANG_DE: 'deutsch'
});
$translateProvider.preferredLanguage('en');
});
Create main Controller file.
angular.module('myApp').controller('mainController',function($scope,$translate){
$scope.changeLanguage = function (key) {
$translate.use(key);
};
});
HTML Part
Hello
This is a paragraph.
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
Monu Thakur
Monu is an experienced Frontend Developer, having good knowledge of Photoshop, illustrator, HTML5, CSS3, Less, Sass, Javascript, Jquery, Angular 4, Angular 4 theme integration.