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

Commit

Permalink
feat(translateService): implements usage of different interpolation s…
Browse files Browse the repository at this point in the history
…ervices
  • Loading branch information
0x-r4bbit committed Jul 18, 2013
1 parent 5596e8b commit 5e20e24
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 9 deletions.
6 changes: 3 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
"angular": "1.0.7",
"angular-mocks": "1.0.7",
"ngMidwayTester": "yearofmoo/ngMidwayTester#*",
"angular-translate-interpolation-default": "~0.1.0"
"angular-translate-interpolation-default": "~0.1.1"
},
"devDependencies": {
"angular-mocks": "1.0.7",
"ngMidwayTester": "yearofmoo/ngMidwayTester",
"angular-translate-interpolation-messageformat": "~0.1.0"
"angular-translate-interpolation-messageformat": "~0.1.1"
}
}
}
42 changes: 36 additions & 6 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
$storagePrefix,
$missingTranslationHandlerFactory,
$interpolationFactory,
$interpolatorFactories = [],
$loaderFactory,
$loaderOptions,
$notFoundIndicatorLeft,
Expand Down Expand Up @@ -147,6 +148,20 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',

this.translations = translations;

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#addInterpolation
* @methodOf pascalprecht.$translateProvider
*
* @description
* Adds interpolation services to angular-translate, so it can manage them.
*
* @param {object} factory Interpolation service factory
*/
this.addInterpolation = function (factory) {
$interpolatorFactories.push(factory);
};

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useMessageFormatInterpolation
Expand Down Expand Up @@ -491,8 +506,21 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
function ($log, $injector, $rootScope, $q) {

var Storage,
interpolate = $injector.get($interpolationFactory || '$translateDefaultInterpolation'),
pendingLoader = false;
interpolateFn = $injector.get($interpolationFactory || '$translateDefaultInterpolation').interpolate,
pendingLoader = false,
interpolatorHashMap = {};

// if we have additional interpolations that were added via
// $translateProvider.addInterpolation(), we have to map'em
if ($interpolatorFactories.length) {
angular.forEach($interpolatorFactories, function (interpolatorFactory) {
var interpolator = $injector.get(interpolatorFactory);
// setting initial locale for each interpolation service
interpolator.setLocale($preferredLanguage || $uses);
// make'em recognizable through id
interpolatorHashMap[interpolator.getInterpolationIdentifier()] = interpolator;
});
}

if ($storageFactory) {
Storage = $injector.get($storageFactory);
Expand All @@ -502,17 +530,19 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
}
}

var $translate = function (translationId, interpolateParams) {
var table = $uses ? $translationTable[$uses] : $translationTable;
var $translate = function (translationId, interpolateParams, interpolationId) {

var table = $uses ? $translationTable[$uses] : $translationTable,
interpolate = (interpolationId) ? interpolatorHashMap[interpolationId].interpolate : interpolateFn;

if (table && table.hasOwnProperty(translationId)) {
return interpolate(table[translationId], interpolateParams, $uses);
return interpolate(table[translationId], interpolateParams);
}

if ($uses && $fallbackLanguage && $uses !== $fallbackLanguage){
var translation = $translationTable[$fallbackLanguage][translationId];
if (translation) {
return interpolate(translation, interpolateParams, $fallbackLanguage);
return interpolate(translation, interpolateParams);
}
}

Expand Down
118 changes: 118 additions & 0 deletions test/unit/translateServiceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,4 +643,122 @@ describe('pascalprecht.translate', function () {
});

});

describe('interpolations', function () {

describe('addInterpolation', function () {

var $translate;

beforeEach(module('pascalprecht.translate', function ($translateProvider, $provide) {

// building custom interpolation service
$provide.factory('customInterpolation', function () {

var translateInterpolator = {},
$locale;

// provide a method to set locale
translateInterpolator.setLocale = function (locale) {
$locale = locale;
};

// provide a method to return an interpolation identifier
translateInterpolator.getInterpolationIdentifier = function () {
return 'custom';
}

// defining the actual interpolate function
translateInterpolator.interpolate = function (string, interpolateParams) {
return 'custom interpolation';
};

return translateInterpolator;
});

// tell angular-translate to optionally use customInterpolation
$translateProvider.addInterpolation('customInterpolation');

// register translations
$translateProvider.translations('en', {
'FOO': 'Some text'
});

// set default language
$translateProvider.preferredLanguage('en');
}));

beforeEach(inject(function (_$translate_) {
$translate = _$translate_;
}));

it('should translate', function () {
expect($translate('FOO')).toEqual('Some text');
});

it('should use custom interpolation', function () {
expect($translate('FOO', {}, 'custom')).toEqual('custom interpolation');
});
});

describe('addInterpolation messageformat', function () {

var $translate;

beforeEach(module('pascalprecht.translate', function ($translateProvider) {

$translateProvider.translations('en', {
'REPLACE_VARS': 'Foo bar {value}',
'SELECT_FORMAT': '{GENDER, select, male{He} female{She} other{They}} liked this.',
'PLURAL_FORMAT': 'There {NUM_RESULTS, plural, one{is one result} other{are # results}}.',
'PLURAL_FORMAT_OFFSET': 'You {NUM_ADDS, plural, offset:1' +
'=0{didnt add this to your profile}' + // Number literals, with a `=` do **NOT** use
'zero{added this to your profile}' + // the offset value
'one{and one other person added this to their profile}' +
'other{and # others added this to their profiles}' +
'}.',
});

$translateProvider.addInterpolation('$translateMessageFormatInterpolation');
$translateProvider.preferredLanguage('en');
}));

beforeEach(inject(function (_$translate_) {
$translate = _$translate_;
}));

it('should replace interpolateParams with concrete values', function () {
expect($translate('REPLACE_VARS', { value: 5 }, 'messageformat')).toEqual('Foo bar 5');
});

it('should support SelectFormat', function () {
expect($translate('SELECT_FORMAT', { GENDER: 'male'}, 'messageformat'))
.toEqual('He liked this.');
expect($translate('SELECT_FORMAT', { GENDER: 'female'}, 'messageformat'))
.toEqual('She liked this.');
expect($translate('SELECT_FORMAT', {}, 'messageformat'))
.toEqual('They liked this.');
});

it('should support PluralFormat', function () {
expect($translate('PLURAL_FORMAT', {
'NUM_RESULTS': 0
}, 'messageformat')).toEqual('There are 0 results.');

expect($translate('PLURAL_FORMAT', {
'NUM_RESULTS': 1
}, 'messageformat')).toEqual('There is one result.');

expect($translate('PLURAL_FORMAT', {
'NUM_RESULTS': 100
}, 'messageformat')).toEqual('There are 100 results.');
});

it('should support PluralFormat - offset extension', function () {
expect($translate('PLURAL_FORMAT_OFFSET', { 'NUM_ADDS': 0 }, 'messageformat')).toEqual('You didnt add this to your profile.');
expect($translate('PLURAL_FORMAT_OFFSET', { 'NUM_ADDS': 2 }, 'messageformat')).toEqual('You and one other person added this to their profile.');
expect($translate('PLURAL_FORMAT_OFFSET', { 'NUM_ADDS': 3 }, 'messageformat')).toEqual('You and 2 others added this to their profiles.');
});
});
});
});

0 comments on commit 5e20e24

Please sign in to comment.