{{amount | currency}}
{{amount | currency}}
, connect the necessary locale and not think about how the purchase amount will look for Russia and the United States. AngularJS will do it for you:
1 234,56 rub - in Russia
$ 1,234.56 - in the USA
Sep 23 2014 - in Russia
Sep 23, 2014 - USA
{{amount | currency:"USD$"}}
{{amount | currency:"USD$"}}
.
<!doctype html> <html ng-app="testApp"> <head> <meta charset="utf-8"> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.2/angular.js"></script> <!-- --> <!-- --> <!-- <script src="https://code.angularjs.org/1.3.0-rc.2/i18n/angular-locale_en-us.js"></script>--> <!-- --> <script src="https://code.angularjs.org/1.3.0-rc.2/i18n/angular-locale_ru-ru.js"></script> <script> angular.module('testApp', []) .controller('mainController', ['$scope', function($scope){ $scope.amount = 1234.56; $scope.currentDate = new Date(); }]) </script> </head> <body ng-controller="mainController"> <input type="number" ng-model="amount"> <br> : <span id="currency-default">{{amount | currency}}</span><br> (USD$): <span>{{amount | currency:"USD$"}}</span><br> : <span>{{currentDate | date}}</span> </body> </html>
{ "COLOR" : "", "HELLO" : "", "HELLO_HABR" : " !" }
{ "COLOR" : "Color", "HELLO" : "Hello", "HELLO_HABR" : "Hello Habrahabr!" }
myApp
.
var app = angular.module('myApp', ['ngResource']);
translationService
, which will download us these files.
app.service('translationService', function($resource) { this.getTranslation = function($scope, language) { var languageFilePath = 'translation_' + language + '.json'; console.log(languageFilePath); $resource(languageFilePath).get(function (data) { $scope.translation = data; }); }; });
myController
controller to which we will connect this service:
app.controller('myController',['$scope', 'translationService', function ($scope, translationService){ // , $scope.translate = function(){ translationService.getTranslation($scope, $scope.selectedLanguage); }; // $scope.selectedLanguage = 'en'; $scope.translate(); }]);
index.html
, in which we include all the scripts.
{{translation.HELLO_HABR}}
angular.js
we also connect angular-resource.js
to work with resources.
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.2/angular.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.2/angular-resource.min.js"></script> <script src="angularApp.js"></script> <script src="angularController.js"></script> <script src="angularTranslationService.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myController"> <section> <h4>Select language for translation:</h4> <select ng-change="translate()" ng-model="selectedLanguage"> <option value="en">English</option> <option value="ru"></option> </select> </section> <h4> : <strong>{{selectedLanguage}}</strong></h4> <h4> :</h4> <p> {{translation.HELLO_HABR}} </p> </div> </body> </html>
var app = angular.module('at', ['pascalprecht.translate']); app.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'); }); app.controller('Ctrl', function ($scope, $translate) { $scope.changeLanguage = function (key) { $translate.use(key); }; });
angular.module("myApp").controller("MyCtrl", function ($scope, gettextCatalog) { // Do things with gettextCatalog });
translate
in html next to the lines that need to be translated;angular-gettext tools
to generate the “po” template;Source: https://habr.com/ru/post/237867/