Socket
Socket
Sign inDemoInstall

lingui-i18n

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lingui-i18n - npm Package Compare versions

Comparing version 0.3.0 to 0.5.0

124

lib/compile.js

@@ -20,6 +20,10 @@ 'use strict';

const isString = s => typeof s === 'string';
var isString = function isString(s) {
return typeof s === 'string';
};
const defaultFormats = (language, formatStyles = {}) => {
const pluralRules = new _makePlural2.default(language, {
var defaultFormats = function defaultFormats(language) {
var formatStyles = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var pluralRules = new _makePlural2.default(language, {
cardinals: true,

@@ -29,16 +33,37 @@ ordinals: true

const style = format => isString(format) ? formatStyles[format] || { style: format } : format;
var style = function style(format) {
return isString(format) ? formatStyles[format] || { style: format } : format;
};
return {
plural: (value, { offset = 0, rules }) => rules[value] || rules[pluralRules(value - offset)] || rules.other,
plural: function plural(value, _ref) {
var _ref$offset = _ref.offset,
offset = _ref$offset === undefined ? 0 : _ref$offset,
rules = _ref.rules;
return rules[value] || rules[pluralRules(value - offset)] || rules.other;
},
selectordinal: (value, { offset = 0, rules }) => rules[value] || rules[pluralRules(value - offset, true)] || rules.other,
selectordinal: function selectordinal(value, _ref2) {
var _ref2$offset = _ref2.offset,
offset = _ref2$offset === undefined ? 0 : _ref2$offset,
rules = _ref2.rules;
return rules[value] || rules[pluralRules(value - offset, true)] || rules.other;
},
select: (value, { rules }) => rules[value] || rules.other,
select: function select(value, _ref3) {
var rules = _ref3.rules;
return rules[value] || rules.other;
},
number: (value, format) => (0, _linguiFormats.number)(language, style(format))(value),
number: function number(value, format) {
return (0, _linguiFormats.number)(language, style(format))(value);
},
date: (value, format) => (0, _linguiFormats.date)(language, style(format))(value),
date: function date(value, format) {
return (0, _linguiFormats.date)(language, style(format))(value);
},
undefined: value => value
undefined: function undefined(value) {
return value;
}
};

@@ -48,12 +73,18 @@ };

function compile(language, message, formatStyles) {
const formattedMessage = processTokens((0, _messageformatParser.parse)(message));
return params => formattedMessage(context({ language, params, formatStyles }));
var formattedMessage = processTokens((0, _messageformatParser.parse)(message));
return function (params) {
return formattedMessage(context({ language, params, formatStyles }));
};
}
function context({ language, params, formatStyles }) {
const formats = defaultFormats(language, formatStyles);
function context(_ref4) {
var language = _ref4.language,
params = _ref4.params,
formatStyles = _ref4.formatStyles;
const ctx = (name, type, format) => {
const value = params[name];
const formatted = formats[type](value, format);
var formats = defaultFormats(language, formatStyles);
var ctx = function ctx(name, type, format) {
var value = params[name];
var formatted = formats[type](value, format);
return typeof formatted === 'function' ? formatted(ctx) : formatted;

@@ -66,33 +97,42 @@ };

function processTokens(tokens, octothorpe) {
if (!tokens.filter(token => !isString(token)).length) {
return () => tokens.join('').trim();
if (!tokens.filter(function (token) {
return !isString(token);
}).length) {
return function () {
return tokens.join('').trim();
};
}
return ctx => tokens.map(token => {
if (isString(token)) {
return token;
} else if (token.type === 'octothorpe') {
const { name, offset = 0 } = octothorpe;
return ctx(name) - offset;
} else if (token.type === 'argument') {
return ctx(token.arg);
} else if (token.type === 'function') {
return ctx(token.arg, token.key, token.params[0]);
}
return function (ctx) {
return tokens.map(function (token) {
if (isString(token)) {
return token;
} else if (token.type === 'octothorpe') {
var name = octothorpe.name,
_octothorpe$offset = octothorpe.offset,
_offset = _octothorpe$offset === undefined ? 0 : _octothorpe$offset;
const offset = token.offset ? parseInt(token.offset) : undefined;
return ctx(name) - _offset;
} else if (token.type === 'argument') {
return ctx(token.arg);
} else if (token.type === 'function') {
return ctx(token.arg, token.key, token.params[0]);
}
const formatProps = {};
token.cases.forEach(item => {
formatProps[item.key] = processTokens(item.tokens, {
name: token.arg,
offset
var offset = token.offset ? parseInt(token.offset) : undefined;
var formatProps = {};
token.cases.forEach(function (item) {
formatProps[item.key] = processTokens(item.tokens, {
name: token.arg,
offset
});
});
});
return ctx(token.arg, token.type, {
offset,
rules: formatProps
});
}).join('').trim();
return ctx(token.arg, token.type, {
offset,
rules: formatProps
});
}).join('').trim();
};
}

@@ -10,2 +10,4 @@ 'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _t = require('./t');

@@ -21,11 +23,17 @@

var _compile = require('./compile');
var _compile2 = require('./compile');
var _compile2 = _interopRequireDefault(_compile);
var _compile3 = _interopRequireDefault(_compile2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class I18n {
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
constructor(language = '', messages = {}) {
var I18n = function () {
function I18n() {
var language = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
var messages = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, I18n);
this._messages = messages;

@@ -40,51 +48,74 @@ this.activate(language);

get messages() {
return this._messages[this._language] || {};
}
_createClass(I18n, [{
key: 'load',
value: function load(messages) {
var _this = this;
get language() {
return this._language;
}
if (!messages) return;
load(messages) {
if (!messages) return;
Object.keys(_extends({}, this._messages, messages)).forEach(function (language) {
if (!_this._messages[language]) _this._messages[language] = {};
Object.keys(_extends({}, this._messages, messages)).forEach(language => {
if (!this._messages[language]) this._messages[language] = {};
Object.assign(_this._messages[language], messages[language] || {});
});
}
}, {
key: 'activate',
value: function activate(language) {
if (!language) return;
Object.assign(this._messages[language], messages[language] || {});
});
}
if (!(language in _plurals2.default)) {
throw new Error(`Unknown locale "${language}".`);
}
activate(language) {
if (!language) return;
this._language = language;
}
}, {
key: 'use',
value: function use(language) {
return new I18n(language, this._messages);
}
}, {
key: 'translate',
value: function translate(_ref) {
var id = _ref.id,
defaults = _ref.defaults,
_ref$params = _ref.params,
params = _ref$params === undefined ? {} : _ref$params,
_ref$formats = _ref.formats,
formats = _ref$formats === undefined ? {} : _ref$formats;
if (!(language in _plurals2.default)) {
throw new Error(`Unknown locale "${language}".`);
var translation = this.messages[id] || defaults || id;
return this.compile(translation, formats)(params);
}
}, {
key: 'compile',
value: function compile(message, formats) {
return (0, _compile3.default)(this.language, message, formats);
}
}, {
key: 'pluralForm',
value: function pluralForm(n) {
var cardinal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cardinal';
this._language = language;
}
var forms = _plurals2.default[this._language];
var form = forms[cardinal];
return form ? form(n) : 'other';
}
}, {
key: 'messages',
get: function get() {
return this._messages[this._language] || {};
}
}, {
key: 'language',
get: function get() {
return this._language;
}
}]);
use(language) {
return new I18n(language, this._messages);
}
return I18n;
}();
translate({ id, defaults, params = {}, formats = {} }) {
const translation = this.messages[id] || defaults || id;
return this.compile(translation, formats)(params);
}
compile(message, formats) {
return (0, _compile2.default)(this.language, message, formats);
}
pluralForm(n, cardinal = 'cardinal') {
const forms = _plurals2.default[this._language];
const form = forms[cardinal];
return form ? form(n) : 'other';
}
}
exports.default = new I18n();
exports.I18n = I18n;

@@ -6,3 +6,3 @@ 'use strict';

});
const formats = [function (n) {
var formats = [function (n) {
return n === 1 ? 'one' : 'other';

@@ -12,3 +12,3 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));

@@ -19,4 +19,4 @@ return i === 0 || n === 1 ? 'one' : 'other';

}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return i === 1 && v === 0 ? 'one' : 'other';

@@ -28,9 +28,9 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
const f = +(n + '.').split('.')[1];
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
var f = +(n + '.').split('.')[1];
return v === 0 && i % 10 === 1 && i % 100 !== 11 || f % 10 === 1 && f % 100 !== 11 ? 'one' : v === 0 && i % 10 >= 2 && i % 10 <= 4 && (i % 100 < 12 || i % 100 > 14) || f % 10 >= 2 && f % 10 <= 4 && (f % 100 < 12 || f % 100 > 14) ? 'few' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return i === 1 && v === 0 ? 'one' : i >= 2 && i <= 4 && v === 0 ? 'few' : v !== 0 ? 'many' : 'other';

@@ -40,18 +40,18 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
const t = +('' + n).replace(/^[^.]*.?|0+$/g, '');
var i = Math.floor(Math.abs(n));
var t = +('' + n).replace(/^[^.]*.?|0+$/g, '');
return n === 1 || t !== 0 && (i === 0 || i === 1) ? 'one' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
const f = +(n + '.').split('.')[1];
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
var f = +(n + '.').split('.')[1];
return v === 0 && i % 100 === 1 || f % 100 === 1 ? 'one' : v === 0 && i % 100 === 2 || f % 100 === 2 ? 'two' : v === 0 && i % 100 >= 3 && i % 100 <= 4 || f % 100 >= 3 && f % 100 <= 4 ? 'few' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));
return i === 0 || i === 1 ? 'one' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
const f = +(n + '.').split('.')[1];
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
var f = +(n + '.').split('.')[1];
return v === 0 && (i === 1 || i === 2 || i === 3) || v === 0 && i % 10 !== 4 && i % 10 !== 6 && i % 10 !== 9 || v !== 0 && f % 10 !== 4 && f % 10 !== 6 && f % 10 !== 9 ? 'one' : 'other';

@@ -63,13 +63,13 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return v === 0 && i % 10 === 1 ? 'one' : v === 0 && i % 10 === 2 ? 'two' : v === 0 && (i % 100 === 0 || i % 100 === 20 || i % 100 === 40 || i % 100 === 60 || i % 100 === 80) ? 'few' : v !== 0 ? 'many' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return i === 1 && v === 0 ? 'one' : i === 2 && v === 0 ? 'two' : v === 0 && (n < 0 || n > 10) && n % 10 === 0 ? 'many' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const t = +('' + n).replace(/^[^.]*.?|0+$/g, '');
var i = Math.floor(Math.abs(n));
var t = +('' + n).replace(/^[^.]*.?|0+$/g, '');
return t === 0 && i % 10 === 1 && i % 100 !== 11 || t !== 0 ? 'one' : 'other';

@@ -81,22 +81,22 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));
return n === 0 ? 'zero' : (i === 0 || i === 1) && n !== 0 ? 'one' : 'other';
}, function (n) {
const f = +(n + '.').split('.')[1];
var f = +(n + '.').split('.')[1];
return n % 10 === 1 && (n % 100 < 11 || n % 100 > 19) ? 'one' : n % 10 >= 2 && n % 10 <= 9 && (n % 100 < 11 || n % 100 > 19) ? 'few' : f !== 0 ? 'many' : 'other';
}, function (n) {
const v = (n + '.').split('.')[1].length;
const f = +(n + '.').split('.')[1];
var v = (n + '.').split('.')[1].length;
var f = +(n + '.').split('.')[1];
return n % 10 === 0 || n % 100 >= 11 && n % 100 <= 19 || v === 2 && f % 100 >= 11 && f % 100 <= 19 ? 'zero' : n % 10 === 1 && n % 100 !== 11 || v === 2 && f % 10 === 1 && f % 100 !== 11 || v !== 2 && f % 10 === 1 ? 'one' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
const f = +(n + '.').split('.')[1];
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
var f = +(n + '.').split('.')[1];
return v === 0 && i % 10 === 1 || f % 10 === 1 ? 'one' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;

@@ -107,4 +107,4 @@ return i === 1 && v === 0 ? 'one' : v !== 0 || n === 0 || n !== 1 && n % 100 >= 1 && n % 100 <= 19 ? 'few' : 'other';

}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return i === 1 && v === 0 ? 'one' : v === 0 && i % 10 >= 2 && i % 10 <= 4 && (i % 100 < 12 || i % 100 > 14) ? 'few' : v === 0 && i !== 1 && i % 10 >= 0 && i % 10 <= 1 || v === 0 && i % 10 >= 5 && i % 10 <= 9 || v === 0 && i % 100 >= 12 && i % 100 <= 14 ? 'many' : 'other';

@@ -114,21 +114,21 @@ }, function (n) {

}, function (n) {
const v = (n + '.').split('.')[1].length;
var v = (n + '.').split('.')[1].length;
return n === 1 && v === 0 ? 'one' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return v === 0 && i % 10 === 1 && i % 100 !== 11 ? 'one' : v === 0 && i % 10 >= 2 && i % 10 <= 4 && (i % 100 < 12 || i % 100 > 14) ? 'few' : v === 0 && i % 10 === 0 || v === 0 && i % 10 >= 5 && i % 10 <= 9 || v === 0 && i % 100 >= 11 && i % 100 <= 14 ? 'many' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));
return i === 0 || n === 1 ? 'one' : n >= 2 && n <= 10 ? 'few' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const f = +(n + '.').split('.')[1];
var i = Math.floor(Math.abs(n));
var f = +(n + '.').split('.')[1];
return n === 0 || n === 1 || i === 0 && f === 1 ? 'one' : 'other';
}, function (n) {
const i = Math.floor(Math.abs(n));
const v = (n + '.').split('.')[1].length;
var i = Math.floor(Math.abs(n));
var v = (n + '.').split('.')[1].length;
return v === 0 && i % 100 === 1 ? 'one' : v === 0 && i % 100 === 2 ? 'two' : v === 0 && i % 100 >= 3 && i % 100 <= 4 || v !== 0 ? 'few' : 'other';

@@ -140,3 +140,3 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));
return i % 10 === 1 || i % 10 === 2 || i % 10 === 5 || i % 10 === 7 || i % 10 === 8 || i % 100 === 20 || i % 100 === 50 || i % 100 === 70 || i % 100 === 80 ? 'one' : i % 10 === 3 || i % 10 === 4 || i % 1000 === 100 || i % 1000 === 200 || i % 1000 === 300 || i % 1000 === 400 || i % 1000 === 500 || i % 1000 === 600 || i % 1000 === 700 || i % 1000 === 800 || i % 1000 === 900 ? 'few' : i === 0 || i % 10 === 6 || i % 100 === 40 || i % 100 === 60 || i % 100 === 90 ? 'many' : 'other';

@@ -158,3 +158,3 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));
return i === 1 ? 'one' : i === 0 || i % 100 >= 2 && i % 100 <= 20 || i % 100 === 40 || i % 100 === 60 || i % 100 === 80 ? 'many' : 'other';

@@ -164,3 +164,3 @@ }, function (n) {

}, function (n) {
const i = Math.floor(Math.abs(n));
var i = Math.floor(Math.abs(n));
return i % 10 === 1 && i % 100 !== 11 ? 'one' : i % 10 === 2 && i % 100 !== 12 ? 'two' : (i % 10 === 7 || i % 10 === 8) && i % 100 !== 17 && i % 100 !== 18 ? 'many' : 'other';

@@ -167,0 +167,0 @@ }, function (n) {

@@ -9,31 +9,31 @@ 'use strict';

const plural = i18n => (_ref) => {
let {
value,
offset = 0,
other
} = _ref,
pluralForms = _objectWithoutProperties(_ref, ['value', 'offset', 'other']);
var plural = function plural(i18n) {
return function (_ref) {
var value = _ref.value,
_ref$offset = _ref.offset,
offset = _ref$offset === undefined ? 0 : _ref$offset,
other = _ref.other,
pluralForms = _objectWithoutProperties(_ref, ['value', 'offset', 'other']);
const translation = pluralForms[(value - offset).toString()] || pluralForms[i18n.pluralForm(value - offset)] || other;
return translation.replace('#', value.toString());
var translation = pluralForms[(value - offset).toString()] || pluralForms[i18n.pluralForm(value - offset)] || other;
return translation.replace('#', value.toString());
};
};
const selectOrdinal = i18n => (_ref2) => {
let {
value,
offset = 0,
other
} = _ref2,
pluralForms = _objectWithoutProperties(_ref2, ['value', 'offset', 'other']);
var selectOrdinal = function selectOrdinal(i18n) {
return function (_ref2) {
var value = _ref2.value,
_ref2$offset = _ref2.offset,
offset = _ref2$offset === undefined ? 0 : _ref2$offset,
other = _ref2.other,
pluralForms = _objectWithoutProperties(_ref2, ['value', 'offset', 'other']);
const translation = pluralForms[(value - offset).toString()] || pluralForms[i18n.pluralForm(value - offset, 'ordinal')] || other;
return translation.replace('#', value.toString());
var translation = pluralForms[(value - offset).toString()] || pluralForms[i18n.pluralForm(value - offset, 'ordinal')] || other;
return translation.replace('#', value.toString());
};
};
function select(_ref3) {
let {
value,
other
} = _ref3,
var value = _ref3.value,
other = _ref3.other,
selectForms = _objectWithoutProperties(_ref3, ['value', 'other']);

@@ -40,0 +40,0 @@

@@ -8,14 +8,29 @@ 'use strict';

const flatten = arrays => [].concat.apply([], arrays);
const zip = (a, b) => a.map((item, index) => [item, b[index]]);
var flatten = function flatten(arrays) {
return [].concat.apply([], arrays);
};
var zip = function zip(a, b) {
return a.map(function (item, index) {
return [item, b[index]];
});
};
const t = i18n => (strings, ...values) => {
if (!Array.isArray(strings)) {
const { id, params, formats } = strings;
return i18n.translate({ id, params, formats });
}
var t = function t(i18n) {
return function (strings) {
for (var _len = arguments.length, values = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
values[_key - 1] = arguments[_key];
}
return flatten(zip(strings, values)).join('');
if (!Array.isArray(strings)) {
var id = strings.id,
params = strings.params,
formats = strings.formats;
return i18n.translate({ id, params, formats });
}
return flatten(zip(strings, values)).join('');
};
};
exports.default = t;
{
"name": "lingui-i18n",
"version": "0.3.0",
"version": "0.5.0",
"description": "I18n tools for javascript",

@@ -22,39 +22,13 @@ "main": "lib/index.js",

},
"engines": {
"node": ">=4.0"
},
"dependencies": {
"lingui-formats": "^0.1.0",
"lingui-formats": "^0.2.0",
"make-plural": "^4.0.1",
"messageformat-parser": "^1.0.0"
},
"devDependencies": {
"babel-cli": "^6.23.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "^6.23.0",
"babel-polyfill": "^6.23.0",
"babel-preset-env": "^1.1.8",
"rimraf": "^2.5.4"
},
"scripts": {
"clean": "rimraf lib/",
"build": "babel src/ --out-dir lib/ --ignore *.test.js",
"copy-flow": "find src/ -type f -name '*.js' ! -name '*.test.js' -exec sh -c 'cp -f $0 ${0%.js}.js.flow' {} \\; && mv src/*.js.flow lib/",
"prepublish": "npm run clean && npm run build && npm run copy-flow"
},
"babel": {
"presets": [
[
"env",
{
"targets": {
"node": "current"
},
"useBuiltIns": true
}
]
],
"plugins": [
"transform-flow-strip-types",
"transform-object-rest-spread"
],
"comments": false
"prepublish": "../../scripts/prepublish.sh"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc