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

Commit

Permalink
feat(messageformat-interpolation): implements usage of messageformat
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-r4bbit committed Jul 17, 2013
1 parent 5d8cb56 commit 5596e8b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
3 changes: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"devDependencies": {
"angular-mocks": "1.0.7",
"ngMidwayTester": "yearofmoo/ngMidwayTester"
"ngMidwayTester": "yearofmoo/ngMidwayTester",
"angular-translate-interpolation-messageformat": "~0.1.0"
}
}
2 changes: 2 additions & 0 deletions karma.unit.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ basePath = '';
files = [
JASMINE,
JASMINE_ADAPTER,
'bower_components/messageformat/messageformat.js',
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'src/translate.js',
'bower_components/angular-translate-interpolation-default/angular-translate-interpolation-default.js',
'bower_components/angular-translate-interpolation-messageformat/angular-translate-interpolation-messageformat.js',
'src/**/*.js',
'test/unit/**/*Spec.js'
];
Expand Down
18 changes: 16 additions & 2 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',

this.translations = translations;

/**
* @ngdoc function
* @name pascalprecht.translate.$translateProvider#useMessageFormatInterpolation
* @methodOf pascalprecht.translate.$translateProvider
*
* @description
* Tells angular-translate to use interpolation functionality of messageformat.js.
* This is useful when having high level pluralization and gender selection.
*
*/
this.useMessageFormatInterpolation = function () {
this.useInterpolation('$translateMessageFormatInterpolation');
};

this.useInterpolation = function (factory) {
$interpolationFactory = factory;
};
Expand Down Expand Up @@ -492,13 +506,13 @@ angular.module('pascalprecht.translate').provider('$translate', ['$STORAGE_KEY',
var table = $uses ? $translationTable[$uses] : $translationTable;

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

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

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

describe('#useMessageFormatInterpolation()', 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.useMessageFormatInterpolation();
$translateProvider.preferredLanguage('en');
}));

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

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

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

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

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

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

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

});
});

0 comments on commit 5596e8b

Please sign in to comment.