Skip to content

Commit

Permalink
fix(rt): default interpolation as fallback
Browse files Browse the repository at this point in the history
if no prefix and suffix have been provided use the default double curly braces

fixes issue #205
  • Loading branch information
zewa666 committed Apr 6, 2017
1 parent fdddded commit 9407c58
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/relativeTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class RelativeTime {

if (options.interpolation && options.interpolation.prefix !== '__' || options.interpolation.suffix !== '__') {
for (let subkey in translation) {
translation[subkey] = translation[subkey].replace('__count__', options.interpolation.prefix + 'count' + options.interpolation.suffix);
translation[subkey] = translation[subkey].replace('__count__', `${options.interpolation.prefix || '{{'}count${options.interpolation.suffix || '}}'}`);
}
}

Expand Down
86 changes: 54 additions & 32 deletions test/unit/relative.time.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,51 +110,73 @@ describe('testing relative time support', () => {
});

it('should try to find the language of the locale when the full locale is not found', (done) => {
expect(translations['nl-BE']).toBe(undefined); //If this fails, someone added translations for nl-BE
i18n.setLocale('nl-BE').then( () => {
let expectedDate = new Date();
expectedDate.setHours(new Date().getHours() + 2);
expect(translations['nl-BE']).toBe(undefined); //If this fails, someone added translations for nl-BE
i18n.setLocale('nl-BE').then( () => {
let expectedDate = new Date();
expectedDate.setHours(new Date().getHours() + 2);

expect(sut.getRelativeTime(expectedDate)).toBe('in 2 uren');
done();
});
expect(sut.getRelativeTime(expectedDate)).toBe('in 2 uren');
done();
});
});

it('should provide the translation for the full locale when available', (done) => {
translations['nl-XX'] = { translation: { 'hour_in_plural': 'in __count__ periods of an hourly length', 'hour_in': 'in __count__ uur'} };
i18n.setLocale('nl-XX').then( () => {
let expectedDate = new Date();
expectedDate.setHours(new Date().getHours() + 2);

expect(sut.getRelativeTime(expectedDate)).toBe('in 2 periods of an hourly length');
done();
});
it('should provide the translation for the full locale when available', (done) => {
translations['nl-XX'] = { translation: { 'hour_in_plural': 'in __count__ periods of an hourly length', 'hour_in': 'in __count__ uur'} };
i18n.setLocale('nl-XX').then( () => {
let expectedDate = new Date();
expectedDate.setHours(new Date().getHours() + 2);

expect(sut.getRelativeTime(expectedDate)).toBe('in 2 periods of an hourly length');
done();
});
});

it('should provide the translation for the base locale when a key is not found in the full locale', (done) => {
translations['nl-XX'] = { translation: { 'hour_in_plural': 'in __count__ periods of an hourly length', 'hour_in': 'in __count__ uur'} };
i18n.setLocale('nl-XX').then( () => {
let expectedDate = new Date();
expectedDate.setMinutes(new Date().getMinutes() + 2);

expect(sut.getRelativeTime(expectedDate)).toBe('in 2 minuten');
done();
});
it('should provide the translation for the base locale when a key is not found in the full locale', (done) => {
translations['nl-XX'] = { translation: { 'hour_in_plural': 'in __count__ periods of an hourly length', 'hour_in': 'in __count__ uur'} };
i18n.setLocale('nl-XX').then( () => {
let expectedDate = new Date();
expectedDate.setMinutes(new Date().getMinutes() + 2);

expect(sut.getRelativeTime(expectedDate)).toBe('in 2 minuten');
done();
});
});


it('should handle non-defined interpolation prefix and suffix', done => {
let customInterpolationSettings = new I18N(ea, new BindingSignaler());

customInterpolationSettings.setup({
lng: 'en',
getAsync: false,
sendMissing: false,
fallbackLng: 'en',
debug: false
}).then((instance) => {
instance.options.interpolation.prefix = undefined;
instance.options.interpolation.suffix = undefined;
let customSut = new RelativeTime(customInterpolationSettings, ea);
let expectedDate = new Date();
expectedDate.setHours(new Date().getHours() - 3);

expect(customSut.getRelativeTime(expectedDate)).toBe('3 hours ago');

done();
});
});

it('should respect interpolation settings', done => {
let ea = new EventAggregator();
let customInterpolationSettings = new I18N(ea, new BindingSignaler());
let customSut = new RelativeTime(customInterpolationSettings, ea);

customInterpolationSettings.setup({
lng: 'en',
getAsync: false,
sendMissing: false,
fallbackLng: 'en',
debug: false,
interpolationPrefix: '${',
interpolationSuffix: '}'
}).then(() => {
debug: false
}).then((instance) => {
instance.options.interpolation.prefix = '${';
instance.options.interpolation.suffix = '}';
let customSut = new RelativeTime(customInterpolationSettings, ea);
let expectedDate = new Date();
expectedDate.setHours(new Date().getHours() - 1);

Expand Down

0 comments on commit 9407c58

Please sign in to comment.