Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
feat(service): add mf configurer #1619
Browse files Browse the repository at this point in the history
This adds `$translateMessageFormatInterpolationProvider.messageFormatConfigurer(fn))` configurering all message format instances.

Sample

```
angular.module(‘app’, [‘pascalprecht.translate’])
       .config(($translateMessageFormatInterpolationProvider) => {
         $translateMessageFormatInterpolationProvider.messageFormatConfigurer((mf) => {
           mf.setIntlSupport(true);
         });
       });
```
  • Loading branch information
knalli committed Oct 30, 2016
1 parent 85cef61 commit 676114b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 14 deletions.
69 changes: 55 additions & 14 deletions src/service/messageformat-interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,57 @@ angular.module('pascalprecht.translate')

/**
* @ngdoc object
* @name pascalprecht.translate.$translateMessageFormatInterpolation
* @requires pascalprecht.translate.TRANSLATE_MF_INTERPOLATION_CACHE
* @name pascalprecht.translate.$translateMessageFormatInterpolationProvider
*
* @description
* Uses MessageFormat.js to interpolate strings against some values.
*
* Be aware to configure a proper sanitization strategy.
*
* See also:
* * {@link pascalprecht.translate.$translateSanitization}
* * {@link https://github.com/SlexAxton/messageformat.js}
*
* @return {object} $translateMessageFormatInterpolation Interpolator service
* Configurations for $translateMessageFormatInterpolation
*/
.factory('$translateMessageFormatInterpolation', $translateMessageFormatInterpolation);
.provider('$translateMessageFormatInterpolation', $translateMessageFormatInterpolationProvider);

function $translateMessageFormatInterpolationProvider() {

'use strict';

var configurer;

/**
* @ngdoc function
* @name pascalprecht.translate.$translateMessageFormatInterpolationProvider#messageFormatConfigurer
* @methodOf pascalprecht.translate.$translateMessageFormatInterpolationProvider
*
* @description
* Defines an optional configurer for the MessageFormat instance.
*
* Note: This hook will be called whenever a new instance of MessageFormat will be created.
*
* @param {function} fn callback with the instance as argument
*/
this.messageFormatConfigurer = function (fn) {
configurer = fn;
};

/**
* @ngdoc object
* @name pascalprecht.translate.$translateMessageFormatInterpolation
* @requires pascalprecht.translate.TRANSLATE_MF_INTERPOLATION_CACHE
*
* @description
* Uses MessageFormat.js to interpolate strings against some values.
*
* Be aware to configure a proper sanitization strategy.
*
* See also:
* * {@link pascalprecht.translate.$translateSanitization}
* * {@link https://github.com/SlexAxton/messageformat.js}
*
* @return {object} $translateMessageFormatInterpolation Interpolator service
*/
this.$get = function ($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE) {
return $translateMessageFormatInterpolation($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE, configurer);
};

}

function $translateMessageFormatInterpolation($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE) {
function $translateMessageFormatInterpolation($translateSanitization, $cacheFactory, TRANSLATE_MF_INTERPOLATION_CACHE, messageFormatConfigurer) {

'use strict';

Expand All @@ -38,6 +72,10 @@ function $translateMessageFormatInterpolation($translateSanitization, $cacheFact
$mf = new MessageFormat('en'),
$identifier = 'messageformat';

if (angular.isFunction(messageFormatConfigurer)) {
messageFormatConfigurer($mf);
}

if (!$cache) {
// create cache if it doesn't exist already
$cache = $cacheFactory(TRANSLATE_MF_INTERPOLATION_CACHE);
Expand All @@ -59,6 +97,9 @@ function $translateMessageFormatInterpolation($translateSanitization, $cacheFact
$mf = $cache.get(locale);
if (!$mf) {
$mf = new MessageFormat(locale);
if (angular.isFunction(messageFormatConfigurer)) {
messageFormatConfigurer($mf);
}
$cache.put(locale, $mf);
}
};
Expand Down
31 changes: 31 additions & 0 deletions test/unit/service/messageformat-interpolation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,34 @@ describe('pascalprecht.translate', function () {
}));
});
});

describe('pascalprecht.translate', function () {

var $provider;
var called = false;
var calledWithPayload = false;

beforeEach(module('pascalprecht.translate', function ($translateMessageFormatInterpolationProvider) {
$provider = $translateMessageFormatInterpolationProvider;
$provider.messageFormatConfigurer(function (mf) {
called = true;
calledWithPayload = !!mf;
});
}));

describe('$translateMessageFormatInterpolationProvider', function () {

describe('$translateMessageFormatInterpolationProvider#configurer', function () {

it('should be invoked', function () {
inject(function ($translateMessageFormatInterpolation) {
expect($translateMessageFormatInterpolation).toBeDefined();
expect(called).toBe(true);
expect(calledWithPayload).toBe(true);
});
});

});
});

});

0 comments on commit 676114b

Please sign in to comment.