translate-maker
Version:
Lightweight translation module. Internationalize your great project.
796 lines (759 loc) • 22.8 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _keymirror = _interopRequireDefault(require("keymirror"));
var _index = _interopRequireWildcard(require("./index"));
function getData(_x) {
return _getData.apply(this, arguments);
}
function _getData() {
_getData = (0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee14(path) {
var file;
return _regenerator.default.wrap(function _callee14$(_context14) {
while (1) {
switch (_context14.prev = _context14.next) {
case 0:
file = require(path);
return _context14.abrupt("return", file && file.default ? file.default : file);
case 2:
case "end":
return _context14.stop();
}
}
}, _callee14);
}));
return _getData.apply(this, arguments);
}
describe('Translate', function () {
var t = null;
it('should be able to create instance', function () {
t = new _index.default({
adapter: new _index.FileAdapter({
path: __dirname + "/locales",
getData: getData
})
});
});
it('should be able to load locale',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee() {
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return t.setLocale('en_US');
case 2:
case "end":
return _context.stop();
}
}
}, _callee);
})));
it('set simple translation property', function () {
t.set('varName', 'Adam');
t.set('varName', 'Peter');
});
it('get simple translation with variable', function () {
expect(t.get('name', {
lastName: 'Fedor'
})).toBe('Zlatko Fedor');
});
it('get simple translation with complex variable', function () {
expect(t.get('nameComplex.long')).toBe('Zlatko Fedor');
});
it('get simple translation with default complex variable', function () {
expect(t.get('nameComplex.short')).toBe('Zlatik');
});
it('get simple translation with local translation', function () {
expect(t.get('about')).toBe('About Zlatkove');
});
it('get simple translation with default local translation', function () {
expect(t.get('aboutDefault')).toBe('About Zlatik');
});
it('get array translation', function () {
expect(t.get('user.crashed', {
user1: {
gender: _index.Gender.MALE,
firstName: 'Adam'
},
user2: {
firstName: 'Lisa',
gender: _index.Gender.FEMALE
}
})).toBe('Adam spadol a Lisa spadla');
});
it('get translation with variable and reference', function () {
expect(t.get('greeting', {
daypart: 'evening',
user: {
firstName: 'Zlatko'
}
})).toBe('Good evening Zlatko');
});
it('should be able to escape variable notation', function () {
expect(t.get('escaped')).toBe('Good {dayparts.$daypartVariant} {$user.firstName}');
});
it('should be able to use conditioned translation with complex variables', function () {
expect(t.get('working', {
user1: {
gender: 'male',
name: 'Zlatko'
},
user2: {
gender: 'female',
name: 'Livia'
}
})).toBe('Boy Zlatko working with girl Livia');
expect(t.get('working', {
user1: {
gender: 'male',
name: 'Zlatko'
},
user2: {
name: 'Livia'
}
})).toBe('Boy Zlatko working with boy or girl named Livia');
expect(t.get('working2', {
user1: {
gender: 'male',
name: 'Zlatko'
},
user2: {
gender: 'female',
name: 'Livia'
}
})).toBe('Boy Zlatko working with girl Livia');
});
it('should be able to get translation by dot notation', function () {
expect(t.get('dot.notation.test', {
name: 'Zlatko'
})).toBe('Hello dot notation Zlatko');
});
it('should be able to use plural', function () {
var user = {
name: 'Zlatko',
followers: 15
};
expect(t.get('followers', {
user: user
})).toBe('Zlatko has 15 followers');
});
it('should be able to use plural with smart variable', function () {
var user = {
name: 'Zlatko',
followers: 15
};
expect(t.get('followersSmart', {
user: user
})).toBe('Zlatko has 15 followers');
expect(t.get('followersSmart', {
user: {
name: 'Zlatko',
followers: 1
}
})).toBe('Zlatko has 1 follower');
});
it('should be able to use ordinal', function () {
var user = {
position: 15
};
expect(t.get('ordinal', {
position: 1
})).toBe('Take the 1st right');
expect(t.get('ordinal', {
position: 2
})).toBe('Take the 2nd right');
expect(t.get('ordinal', {
position: 3
})).toBe('Take the 3rd right');
expect(t.get('ordinal', {
position: 4
})).toBe('Take the 4th right');
expect(t.get('ordinal', {
position: 11
})).toBe('Take the 11th right');
expect(t.get('ordinal', {
position: 21
})).toBe('Take the 21st right');
expect(t.get('ordinal', {
position: 22
})).toBe('Take the 22nd right');
expect(t.get('ordinal', {
position: 33
})).toBe('Take the 33rd right');
expect(t.get('ordinal', {
position: 44
})).toBe('Take the 44th right');
});
it('should be not able to get non existing translation', function () {
expect(t.get('notation.test')).toBe('Missing default translation for: notation.test');
});
it('should be not able to get non existing translation', function () {
expect(t.get('dot.notation.testNonExist')).toBe('Missing default translation for: dot.notation.testNonExist');
});
it('should be able to get default value for non existing translation', function () {
expect(t.get('notation.test', 'Default value')).toBe('Default value');
});
it('should be able to get default value for non existing translation with attrs', function () {
expect(t.get('notation.test', {
name: 'Zlatko'
}, 'Default value {$name}')).toBe('Default value Zlatko');
});
it('should be not able to get null', function () {
expect(t.get(null)).toBe(undefined);
});
it('should not be able to get empty variable', function () {
expect(t.get('emptyVariable')).toBe('This is empty {} variable');
});
it('should not be able to get empty variable', function () {
expect(t.get('emptyExternalVariable')).toBe('This is empty {$} external variable');
});
it('should be able to pass ICU test', function () {
expect(t.get('ICU', {
gender_of_host: 'male',
num_guests: 3,
host: 'Zlatko',
guest: 'Livia'
})).toBe('Zlatko invites Livia and 2 other people to his party.');
});
it('should not be able to translate plural without pairs', function () {
expect(t.get('pluralWithoutPairs')).toBe('');
});
it('should not be able to translate select without pairs', function () {
expect(t.get('selectWithoutPairs')).toBe('');
});
it('should not be able to translate bad translation', function () {
expect(t.get('badTranslation')).toBe(undefined);
});
it('should be able to use own filter', function () {
function test() {
return 'test';
}
t.setFilter('test', test);
expect(t.get('customFilter')).toBe('This is test');
});
it('should be able to use trim', function () {
expect(t.get('filter.trim', {
value: ' text '
})).toBe('Trim this text');
expect(t.get('filter.trim2', {
value: ' text '
})).toBe('Trim this text');
});
it('should be able to use trunc', function () {
expect(t.get('filter.trunc', {
value: '123456789123456789'
})).toBe('Trunc this 12345..');
});
it('should be able to use lowerCase', function () {
expect(t.get('filter.lowerCase', {
value: 'LoWERCASe'
})).toBe('This is lowercase');
});
it('should be able to use upperCase', function () {
expect(t.get('filter.upperCase', {
value: 'upperCase'
})).toBe('This is UPPERCASE');
});
it('should be able to load namespace',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee2() {
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 2;
return t.loadNamespace('widget');
case 2:
case "end":
return _context2.stop();
}
}
}, _callee2);
})));
it('should be able to use namespace translation', function () {
expect(t.get('widget.test')).toBe('widget test');
});
it('should be able to init default adapter with data',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee3() {
var translate;
return _regenerator.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
translate = new _index.default({
data: {
sk: {
test: '123'
}
}
});
_context3.next = 3;
return translate.setLocale('sk');
case 3:
expect(translate.get('test')).toBe('123');
case 4:
case "end":
return _context3.stop();
}
}
}, _callee3);
})));
it('should be able to use ICU mode',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee4() {
var t;
return _regenerator.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
t = new _index.default({
mode: _index.Mode.ICU,
data: {
sk: {
icu: 'ICU',
test: 'Hello {name} {$icu}'
}
}
});
_context4.next = 3;
return t.setLocale('sk');
case 3:
expect(t.get('test', {
name: 'Zlatko'
})).toBe('Hello Zlatko ICU');
case 4:
case "end":
return _context4.stop();
}
}
}, _callee4);
})));
it('should be able to translate whole object', function () {
var result = t.get({
first: 'name',
second: 'about'
}, {
lastName: 'Fedor'
});
expect(result.first).toBe('Zlatko Fedor');
expect(result.second).toBe('About Zlatkove');
});
it('should be able to get translation by function', function () {
function getLastName() {
return 'Fedor';
}
expect(t.get('name', {
lastName: getLastName
})).toBe('Zlatko Fedor');
});
it('should be able to get complex translation by function', function () {
var user = {
_lastName: 'Fedor',
lastName: function lastName() {
return this._lastName;
}
};
expect(user.lastName()).toBe('Fedor');
expect(t.get('nameFn', {
user: user
})).toBe('Zlatko Fedor');
});
it('should be able to use correct plural',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee5() {
var t;
return _regenerator.default.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
t = new _index.default({
data: {
en_US: {
test: "{$count, plural,\n one {# item}\n few {# bug}\n =7 {# seven}\n =3 {# three}\n {# items}\n }"
},
sk_SK: {
test: "{$count, plural,\n one {# polozka}\n few {# polozky}\n =7 {# sedem}\n =3 {# tri}\n {# poloziek}\n }"
}
}
});
_context5.next = 3;
return t.setLocale('sk_SK');
case 3:
expect(t.get('test', {
count: 0
})).toBe('0 poloziek');
expect(t.get('test', {
count: 1
})).toBe('1 polozka');
expect(t.get('test', {
count: 2
})).toBe('2 polozky');
expect(t.get('test', {
count: 6
})).toBe('6 poloziek');
expect(t.get('test', {
count: 7
})).toBe('7 sedem');
expect(t.get('test', {
count: 3
})).toBe('3 tri');
_context5.next = 11;
return t.setLocale('en_US');
case 11:
expect(t.get('test', {
count: 0
})).toBe('0 items');
expect(t.get('test', {
count: 1
})).toBe('1 item');
expect(t.get('test', {
count: 2
})).toBe('2 items');
expect(t.get('test', {
count: 6
})).toBe('6 items');
expect(t.get('test', {
count: 7
})).toBe('7 seven');
expect(t.get('test', {
count: 3
})).toBe('3 three');
case 17:
case "end":
return _context5.stop();
}
}
}, _callee5);
})));
});
describe('Catch event', function () {
it('should be able to catch missing event', function (done) {
var tt = new _index.default();
tt.once('missing', function () {
return done();
});
tt.get('missing.translate.path');
});
it('should be able to catch error event', function (done) {
var tt = new _index.default({
data: {
sk: {
badText: 'bad { omg'
}
}
});
tt.once('err', function () {
return done();
});
tt.setLocale('sk').then(function () {
tt.get('badText');
});
});
it('should be able to catch missingdefault event', function (done) {
var tt = new _index.default({
data: {
sk: {
badText: 'bad { omg'
}
}
});
tt.once('missingdefault', function (path) {
expect(path).toBe('badText');
done();
});
tt.setLocale('sk').then(function () {
tt.get('badText');
});
});
});
describe('Dummy cache', function () {
var cache = null;
var t = null;
it('should be able to create instance', function () {
cache = new _index.DummyCache();
});
it('should be able to use cache',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee6() {
return _regenerator.default.wrap(function _callee6$(_context6) {
while (1) {
switch (_context6.prev = _context6.next) {
case 0:
t = new _index.default({
cache: cache,
data: {
sk: {
test: '123'
},
en: {
test: '222'
}
}
});
_context6.next = 3;
return t.setLocale('sk');
case 3:
expect(t.get('test')).toBe('123');
expect(t.get('test')).toBe('123');
case 5:
case "end":
return _context6.stop();
}
}
}, _callee6);
})));
it('should be able to change locale',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee7() {
return _regenerator.default.wrap(function _callee7$(_context7) {
while (1) {
switch (_context7.prev = _context7.next) {
case 0:
_context7.next = 2;
return t.setLocale('en');
case 2:
expect(t.get('test')).toBe('222');
expect(t.get('test')).toBe('222');
case 4:
case "end":
return _context7.stop();
}
}
}, _callee7);
})));
it('should be able to use dehydrate', function () {
var cache = t.getCache();
var data = cache.dehydrate();
expect(data).toBe(undefined);
});
it('should be able to use rehydrate', function () {
var cache = t.getCache();
cache.rehydrate({});
});
});
describe('Memory cache', function () {
var cache = null;
var t = null;
it('should be able to create instance', function () {
cache = new _index.MemoryCache();
});
it('should be able to use cache',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee8() {
return _regenerator.default.wrap(function _callee8$(_context8) {
while (1) {
switch (_context8.prev = _context8.next) {
case 0:
t = new _index.default({
cache: cache,
data: {
sk: {
test: '123'
},
en: {
test: '222'
}
}
});
_context8.next = 3;
return t.setLocale('sk');
case 3:
expect(t.get('test')).toBe('123');
expect(t.get('test')).toBe('123');
case 5:
case "end":
return _context8.stop();
}
}
}, _callee8);
})));
it('should be able to change locale',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee9() {
return _regenerator.default.wrap(function _callee9$(_context9) {
while (1) {
switch (_context9.prev = _context9.next) {
case 0:
_context9.next = 2;
return t.setLocale('en');
case 2:
expect(t.get('test')).toBe('222');
expect(t.get('test')).toBe('222');
case 4:
case "end":
return _context9.stop();
}
}
}, _callee9);
})));
it('should be able to use dehydrate and rehydrate', function () {
var cache = t.getCache();
var data = cache.dehydrate();
expect(data).not.toBe(undefined);
expect(data['222']).not.toBe(undefined);
cache.rehydrate(data);
expect(t.get('test')).toBe('222');
expect(t.get('test')).toBe('222');
});
it('should be able to use cache', function () {
var t = new _index.default();
t.set({
en_US: {
menu: {
title: 'Welcome!'
}
},
de_DE: {
menu: {
title: 'Willkommen!'
}
}
});
var translated = t.get('de_DE.menu.title');
expect(translated).toBe('Willkommen!');
});
});
describe('Async namespaces', function () {
it('should be able to create instance',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee10() {
var t;
return _regenerator.default.wrap(function _callee10$(_context10) {
while (1) {
switch (_context10.prev = _context10.next) {
case 0:
t = new _index.default({
adapter: new _index.FileAdapter({
path: __dirname + "/locales",
getData: getData
})
});
t.setLocale('en_US');
_context10.next = 4;
return t.setLocale('en_US', 'widget');
case 4:
expect(t.get('widget.test')).toBe('widget test');
case 5:
case "end":
return _context10.stop();
}
}
}, _callee10);
})));
it('should be able to change locale',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee11() {
var t;
return _regenerator.default.wrap(function _callee11$(_context11) {
while (1) {
switch (_context11.prev = _context11.next) {
case 0:
t = new _index.default({
adapter: new _index.FileAdapter({
path: __dirname + "/locales",
getData: getData
})
});
t.setLocale('en_US');
t.setLocale('en_US', 'widget');
t.setLocale('sk_SK');
_context11.next = 6;
return t.setLocale('sk_SK', 'widget');
case 6:
expect(t.get('widget.test')).toBe('widget test sk');
case 7:
case "end":
return _context11.stop();
}
}
}, _callee11);
})));
it('should be able to use loadNamespace',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee12() {
var t;
return _regenerator.default.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
t = new _index.default({
adapter: new _index.FileAdapter({
path: __dirname + "/locales",
getData: getData
})
});
t.setLocale('en_US');
_context12.next = 4;
return t.loadNamespace('widget');
case 4:
expect(t.get('widget.test')).toBe('widget test');
case 5:
case "end":
return _context12.stop();
}
}
}, _callee12);
})));
it('should be able to change locale',
/*#__PURE__*/
(0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee13() {
var t;
return _regenerator.default.wrap(function _callee13$(_context13) {
while (1) {
switch (_context13.prev = _context13.next) {
case 0:
t = new _index.default({
adapter: new _index.FileAdapter({
path: __dirname + "/locales",
getData: getData
})
});
t.setLocale('en_US');
t.loadNamespace('widget');
t.setLocale('sk_SK');
_context13.next = 6;
return t.loadNamespace('widget');
case 6:
expect(t.get('widget.test')).toBe('widget test sk');
case 7:
case "end":
return _context13.stop();
}
}
}, _callee13);
})));
});
//# sourceMappingURL=Translate.test.js.map