/** * @param {Settings} Settings * @constructor */ function Language(Settings) { this.Settings = Settings; } Language.$inject = ['Settings']; app.service('Language', Language); /** * @param {String} string * @returns {String} */ Language.prototype.translate = function(string) { var translation = __lang[this.Settings.getValue('lang')]; if (typeof translation[string] !== 'undefined') { string = translation[string]; } return string; };
<span>{{ 'some_english_string' | translate }}</span>
<span><%= t('some_english_string') %></span>
__lang.ru = { "some_english_string": " {{string.number}}" };
<div class="input-text-right"> <input type="text" my-num-format="00000.00" my-validate="[ { type: 'notGreaterThen', compareTo: 'somecondition()', message: '<%= t('some_validation_error_message') %>' } ]" ng-model="model.value"> </div>
..... /** * @param {String} template * @returns {String} */ Language.prototype.translateTemplate = function(template) { // this.translate() Lo-Dash t() return _.template(template, { t: angular.bind(this, this.translate) }); };
// angular- , app.factory('$templateCache', [ '$cacheFactory', 'Language', function($cacheFactory, Language) { /** * @constructor */ function MyTemplateCache() { /** * @param {String} key * @param {*} value */ this.put = function(key, value) { // - promise-, $http if (typeof value.then !== 'undefined') { // promise-, value = value.then(function(response) { response.data = Language.translateTemplate(response.data); return response; }); } // - -, angular promise- $http else if (value instanceof Array) { value[1] = Language.translateTemplate(value[1]); } // - // ( , <script type="text/ng-template"></script> // ) else if (typeof value === 'string') { value = Language.translateTemplate(value); } // put() MyTemplateCache.prototype.put(key, value); }; } // $templateCache MyTemplateCache.prototype = $cacheFactory('templates'); return new MyTemplateCache(); } ]);
$templateCache.removeAll(); $route.reload();
Source: https://habr.com/ru/post/191074/
All Articles