lingui-i18n
Advanced tools
Comparing version 0.6.2 to 0.7.0-0
404
lib/index.js
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.I18n = undefined; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
var _i18n = require('./i18n'); | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var _i18n2 = _interopRequireDefault(_i18n); | ||
var _Array$includes = _interopDefault(require('babel-runtime/core-js/array/includes')); | ||
var _Object$assign = _interopDefault(require('babel-runtime/core-js/object/assign')); | ||
var _extends = _interopDefault(require('babel-runtime/helpers/extends')); | ||
var _Object$keys = _interopDefault(require('babel-runtime/core-js/object/keys')); | ||
var _classCallCheck = _interopDefault(require('babel-runtime/helpers/classCallCheck')); | ||
var _createClass = _interopDefault(require('babel-runtime/helpers/createClass')); | ||
var _objectWithoutProperties = _interopDefault(require('babel-runtime/helpers/objectWithoutProperties')); | ||
var linguiFormats = require('lingui-formats'); | ||
var messageformatParser = require('messageformat-parser'); | ||
var MakePlural = _interopDefault(require('make-plural/make-plural')); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var flatten = function flatten(arrays) { | ||
return [].concat.apply([], arrays); | ||
}; | ||
exports.default = _i18n2.default; | ||
exports.I18n = _i18n.I18n; | ||
var zip = function zip(a, b) { | ||
return a.map(function (item, index) { | ||
return [item, b[index]]; | ||
}); | ||
}; | ||
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]; | ||
} | ||
// used as a function | ||
if (!Array.isArray(strings)) { | ||
var id = strings.id, | ||
params = strings.params, | ||
formats = strings.formats; | ||
return i18n.translate({ id: id, params: params, formats: formats }); | ||
} | ||
// used as a template tag | ||
return flatten(zip(strings, values)).join(''); | ||
}; | ||
}; | ||
var _plural = function _plural(type) { | ||
return function (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']); | ||
var translation = pluralForms[(value - offset).toString()] || // exact match | ||
pluralForms[i18n.pluralForm(value - offset, type)] || // plural form | ||
other; | ||
return translation.replace('#', value.toString()); | ||
}; | ||
}; | ||
}; | ||
var plural = _plural('cardinal'); | ||
var selectOrdinal = _plural('ordinal'); | ||
function select(_ref2) { | ||
var value = _ref2.value, | ||
other = _ref2.other, | ||
selectForms = _objectWithoutProperties(_ref2, ['value', 'other']); | ||
return selectForms[value] || other; | ||
} | ||
// $FlowIgnore: Missing annotation | ||
MakePlural.load( | ||
// $FlowIgnore: Missing annotation - only json | ||
require('make-plural/data/plurals.json'), | ||
// $FlowIgnore: Missing annotation - only json | ||
require('make-plural/data/ordinals.json')); | ||
var isString$1 = function isString(s) { | ||
return typeof s === 'string'; | ||
}; | ||
var loadLanguageData = function loadLanguageData(language) { | ||
var plurals = new MakePlural(language, { | ||
cardinals: true, | ||
ordinals: true | ||
}); | ||
return { plurals: plurals }; | ||
}; | ||
var compileMessage = function compileMessage(message) { | ||
return processTokens(messageformatParser.parse(message)); | ||
}; | ||
// [Tokens] -> (CTX -> String) | ||
function processTokens(tokens) { | ||
var octothorpe = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (!tokens.filter(function (token) { | ||
return !isString$1(token); | ||
}).length) { | ||
return function () { | ||
return tokens.join('').trim(); | ||
}; | ||
} | ||
return function (ctx) { | ||
return tokens.map(function (token) { | ||
if (isString$1(token)) { | ||
return token; | ||
// # in plural case | ||
} else if (token.type === 'octothorpe') { | ||
var name = octothorpe.name, | ||
_octothorpe$offset = octothorpe.offset, | ||
_offset = _octothorpe$offset === undefined ? 0 : _octothorpe$offset; | ||
return ctx(name) - _offset; | ||
// simple argument | ||
} else if (token.type === 'argument') { | ||
return ctx(token.arg); | ||
// argument with custom format (date, number) | ||
} else if (token.type === 'function') { | ||
return ctx(token.arg, token.key, token.params[0]); | ||
} | ||
var offset = token.offset ? parseInt(token.offset) : undefined; | ||
// complex argument with cases | ||
var formatProps = {}; | ||
token.cases.forEach(function (item) { | ||
formatProps[item.key] = processTokens(item.tokens, { | ||
name: token.arg, | ||
offset: offset | ||
}); | ||
}); | ||
return ctx(token.arg, token.type, _extends({ | ||
offset: offset | ||
}, formatProps)); | ||
}); | ||
}; | ||
} | ||
var isString = function isString(s) { | ||
return typeof s === 'string'; | ||
}; | ||
var isFunction = function isFunction(f) { | ||
return typeof f === 'function'; | ||
}; | ||
var defaultFormats = function defaultFormats(language) { | ||
var languageData = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var formatStyles = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var plurals = languageData.plurals; | ||
var style = function style(format) { | ||
return isString(format) ? formatStyles[format] || { style: format } : format; | ||
}; | ||
var replaceOctothorpe = function replaceOctothorpe(value, message) { | ||
return function (ctx) { | ||
var msg = isFunction(message) ? message(ctx) : message; | ||
var norm = Array.isArray(msg) ? msg : [msg]; | ||
return norm.map(function (m) { | ||
return isString(m) ? m.replace('#', value) : m; | ||
}); | ||
}; | ||
}; | ||
return { | ||
plural: function plural(value, _ref) { | ||
var _ref$offset = _ref.offset, | ||
offset = _ref$offset === undefined ? 0 : _ref$offset, | ||
rules = _objectWithoutProperties(_ref, ['offset']); | ||
var message = rules[value] || rules[plurals(value - offset)]; | ||
return replaceOctothorpe(value - offset, message); | ||
}, | ||
selectordinal: function selectordinal(value, _ref2) { | ||
var _ref2$offset = _ref2.offset, | ||
offset = _ref2$offset === undefined ? 0 : _ref2$offset, | ||
rules = _objectWithoutProperties(_ref2, ['offset']); | ||
var message = rules[value] || rules[plurals(value - offset, true)]; | ||
return replaceOctothorpe(value - offset, message); | ||
}, | ||
select: function select(value, rules) { | ||
return rules[value] || rules.other; | ||
}, | ||
number: function number$$1(value, format) { | ||
return linguiFormats.number(language, style(format))(value); | ||
}, | ||
date: function date$$1(value, format) { | ||
return linguiFormats.date(language, style(format))(value); | ||
}, | ||
undefined: function undefined(value) { | ||
return value; | ||
} | ||
}; | ||
}; | ||
// Message -> (Params -> String) | ||
/** | ||
* Compile message in given language and provide language data (e.g: plural) | ||
* with formatStyles. The result is a function which takes parameters and return | ||
* formatted message. | ||
* | ||
* @param language - Language of message | ||
* @param message - Message to parse and compile | ||
* @param languageData - Language data (e.g: plurals) | ||
* @param formatStyles - Custom format styles | ||
*/ | ||
function compile(language, message, languageData, formatStyles) { | ||
var formattedMessage = message; | ||
if (isString(message)) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
formattedMessage = /*#__PURE__*/compileMessage(message); | ||
languageData = /*#__PURE__*/loadLanguageData(language); | ||
} else { | ||
// constant message | ||
return function (params) { | ||
return message; | ||
}; | ||
} | ||
} | ||
return function () { | ||
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
// $FlowIgnore: formattedMessage is always a function | ||
var message = formattedMessage(context({ | ||
language: language, params: params, formatStyles: formatStyles, languageData: languageData | ||
})); | ||
return Array.isArray(message) ? message.join('').trim() : message; | ||
}; | ||
} | ||
// Params -> CTX | ||
/** | ||
* Creates a context object, which formats ICU MessageFormat arguments based on | ||
* argument type. | ||
* | ||
* @param language - Language of message | ||
* @param params - Parameters for variable interpolation | ||
* @param languageData - Language data (e.g: plurals) | ||
* @param formatStyles - Custom format styles | ||
* @returns {function(string, string, any)} | ||
*/ | ||
function context(_ref3) { | ||
var language = _ref3.language, | ||
params = _ref3.params, | ||
formatStyles = _ref3.formatStyles, | ||
languageData = _ref3.languageData; | ||
var formats = defaultFormats(language, languageData, formatStyles); | ||
var ctx = function ctx(name, type, format) { | ||
var value = params[name]; | ||
var formatted = formats[type](value, format); | ||
var message = isFunction(formatted) ? formatted(ctx) : formatted; | ||
return Array.isArray(message) ? message.join('') : message; | ||
}; | ||
return ctx; | ||
} | ||
var I18n = function () { | ||
function I18n(language) { | ||
var messages = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var languageData = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
_classCallCheck(this, I18n); | ||
this._messages = messages; | ||
this._languageData = languageData; | ||
if (language) { | ||
this.activate(language); | ||
} | ||
this.t = t(this); | ||
this.plural = plural(this); | ||
this.select = select; | ||
this.selectOrdinal = selectOrdinal(this); | ||
} | ||
_createClass(I18n, [{ | ||
key: 'load', | ||
value: function load(messages) { | ||
var _this = this; | ||
if (!messages) return; | ||
// deeply merge Catalogs | ||
_Object$keys(_extends({}, this._messages, messages)).forEach(function (language) { | ||
if (!_this._messages[language]) _this._messages[language] = {}; | ||
_Object$assign(_this._messages[language], messages[language] || {}); | ||
}); | ||
} | ||
}, { | ||
key: 'loadLanguageData', | ||
value: function loadLanguageData$$1(languageData) { | ||
if (!languageData) return; | ||
_Object$assign(this._languageData, languageData); | ||
} | ||
}, { | ||
key: 'activate', | ||
value: function activate(language) { | ||
if (!language) return; | ||
// $FlowIgnore: Array.includes is polyfilled | ||
if (!_Array$includes(this.availableLanguages, language)) { | ||
throw new Error('Unknown locale "' + language + '".'); | ||
} | ||
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; | ||
var translation = this.messages[id] || defaults || id; | ||
return this.compile(translation, formats)(params); | ||
} | ||
}, { | ||
key: 'compile', | ||
value: function compile$$1(message, formats) { | ||
return compile(this.language, message, this.languageData, formats); | ||
} | ||
}, { | ||
key: 'pluralForm', | ||
value: function pluralForm(n) { | ||
var pluralType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'cardinal'; | ||
var forms = this.languageData.plurals; | ||
return forms(n, pluralType === 'ordinal'); | ||
} | ||
}, { | ||
key: 'availableLanguages', | ||
get: function get() { | ||
return _Object$keys(this._messages); | ||
} | ||
}, { | ||
key: 'messages', | ||
get: function get() { | ||
return this._messages[this.language] || {}; | ||
} | ||
}, { | ||
key: 'languageData', | ||
get: function get() { | ||
var data = this._languageData[this.language]; | ||
if (process.env.NODE_ENV !== 'production') { | ||
// Allow overriding data in development, useful for testing | ||
return data || /*#__PURE__*/loadLanguageData(this.language); | ||
} | ||
return data; | ||
} | ||
}, { | ||
key: 'language', | ||
get: function get() { | ||
return this._language; | ||
} | ||
}]); | ||
return I18n; | ||
}(); | ||
var i18n$1 = new I18n(); | ||
exports['default'] = i18n$1; | ||
exports.I18n = I18n; |
{ | ||
"name": "lingui-i18n", | ||
"version": "0.6.2", | ||
"version": "0.7.0-0", | ||
"description": "I18n tools for javascript", | ||
"main": "lib/index.js", | ||
"module": "src/index.js", | ||
"author": { | ||
@@ -26,3 +27,4 @@ "name": "Tomáš Ehrlich", | ||
"dependencies": { | ||
"lingui-formats": "^0.3.0", | ||
"babel-runtime": "^6.23.0", | ||
"lingui-formats": "^0.4.0-0", | ||
"make-plural": "^4.0.1", | ||
@@ -29,0 +31,0 @@ "messageformat-parser": "^1.0.0" |
@@ -10,5 +10,5 @@ # lingui-i18n | ||
```sh | ||
npm install --save-dev lingui-i18n | ||
npm install --save lingui-i18n | ||
# or | ||
yarn add --dev lingui-i18n | ||
yarn add lingui-i18n | ||
``` | ||
@@ -38,3 +38,3 @@ | ||
Wrap you text in `i18n.t` template literal tag so it's translated into active | ||
language. Variables must be wrapped inside object literal, so the name of variable is preserved in message catalog: | ||
language: | ||
@@ -46,7 +46,7 @@ ```js | ||
const name = "Fred" | ||
i18n.t`My name is ${{ name }}` | ||
i18n.t`My name is ${ name }` | ||
// becomes "Je m'appelle Fred" | ||
``` | ||
Plurals and selections are possible using `plural` and `select` methods (value must be again wrapped inside object literal): | ||
Plurals and selections are possible using `plural` and `select` methods: | ||
@@ -57,3 +57,3 @@ ```js | ||
i18n.plural({ | ||
value: { count }, | ||
value: count, | ||
one: "# book", | ||
@@ -72,7 +72,7 @@ other: "# books" | ||
i18n.select({ | ||
value: { gender }, | ||
value: gender, | ||
offset: 1, | ||
// plural, instead of i18n.plural | ||
female: plural({ | ||
value: { numOfGuests }, | ||
value: numOfGuests, | ||
offset: 1, | ||
@@ -79,0 +79,0 @@ // t, instead of i18n.t |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
21
1019
0
46718
4
8
1
+ Addedbabel-runtime@^6.23.0
+ Addedbabel-runtime@6.26.0(transitive)
+ Addedcore-js@2.6.12(transitive)
+ Addedlingui-formats@0.4.6(transitive)
+ Addedregenerator-runtime@0.11.1(transitive)
- Removedlingui-formats@0.3.0(transitive)
Updatedlingui-formats@^0.4.0-0