UNPKG

localized-string

Version:

Fetches and formats translated strings.

52 lines (42 loc) 2.02 kB
'use strict'; var expect = require('chai').expect; describe('localized-string', function() { var localizedString = require('../')({ '': { testKey: 'AWESOMECAKE!', testKeyWithSingleArg: 'look at my {0}', testKeyWithMultipleArgs: 'this {0} is {1} to the max', testNested: 'test {0{1}}', testChoice: 'Output is {0,choice,-1#negative|0#zero|1#positive}' } }); it(' should return text if key exists', function() { expect(localizedString.getText('', 'testKey')).to.equal('AWESOMECAKE!'); }); it(' should return key from input if key does not exist', function() { var input = 'nope =('; expect(localizedString.getText('', input)).to.equal(input); // Check with args as well expect(localizedString.getText('', input, 'AWESOMECAKE!')).to.equal(input); }); it(' should return text with arg if key exists', function() { var args = 'horse'; expect(localizedString.getText('', 'testKeyWithSingleArg', args)).to.equal('look at my ' + args); // Check with array as well expect(localizedString.getText('', 'testKeyWithSingleArg', [args])).to.equal('look at my ' + args); // Check with numbers as well expect(localizedString.getText('', 'testKeyWithSingleArg', 1)).to.equal('look at my ' + 1); }); it(' should handle too many arguments', function() { var args = 'horse'; expect(localizedString.getText('', 'testKeyWithSingleArg', [args, 'cow', 'farm'])).to.equal('look at my ' + args); }); it(' should return text with multiple args if key exists', function() { var args = ['lib', 'awesome']; expect(localizedString.getText('', 'testKeyWithMultipleArgs', args)).to.equal('this ' + args[0] + ' is ' + args[1] + ' to the max'); }); it(' should handle string input for translations', function() { var loc = require('../')('test/test-translations'); expect(loc.getText('', 'magic.key')).to.equal('This is a string'); expect(loc.getText('de_DE', 'magic.key')).to.equal('This is a german string'); }); });