lingui-i18n
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -8,2 +8,4 @@ 'use strict'; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _messageformat = require('messageformat'); | ||
@@ -19,39 +21,63 @@ | ||
var _plurals = require('./plurals'); | ||
var _plurals2 = _interopRequireDefault(_plurals); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function I18n() { | ||
this.language = ''; | ||
this.messages = {}; | ||
class I18n { | ||
this.t = this.bindFormat(_t2.default); | ||
this.select = this.bindFormat(_select.select); | ||
this.plural = this.bindFormat(_select.plural); | ||
} | ||
constructor(language = '', messages = {}) { | ||
this._language = language; | ||
this._messages = messages; | ||
I18n.prototype.use = function (language, messages = {}) { | ||
this.language = language; | ||
this.messages = messages; | ||
}; | ||
this.t = (0, _t2.default)(this); | ||
this.plural = (0, _select.plural)(this); | ||
this.select = _select.select; | ||
this.selectOrdinal = (0, _select.selectOrdinal)(this); | ||
} | ||
I18n.prototype.translate = function (message, params = {}) { | ||
return this.compile(message)(params); | ||
}; | ||
get messages() { | ||
return this._messages[this._language] || {}; | ||
} | ||
I18n.prototype.compile = function (message) { | ||
const translation = this.messages[message] || message; | ||
return new _messageformat2.default(this.language).compile(translation); | ||
}; | ||
get language() { | ||
return this._language; | ||
} | ||
I18n.prototype.bindFormat = function (format) { | ||
return (...args) => { | ||
var _format = format(...args); | ||
load(messages) { | ||
if (!messages) return; | ||
const message = _format.message, | ||
params = _format.params; | ||
Object.keys(_extends({}, this._messages, messages)).forEach(language => { | ||
if (!this._messages[language]) this._messages[language] = {}; | ||
return this.translate(message, params); | ||
}; | ||
}; | ||
Object.assign(this._messages[language], messages[language] || {}); | ||
}); | ||
} | ||
activate(language) { | ||
if (language) this._language = language; | ||
} | ||
use(language) { | ||
return new I18n(language, this._messages); | ||
} | ||
translate({ id, defaults, params = {} }) { | ||
const translation = this.messages[id] || defaults || id; | ||
return this.compile(translation)(params); | ||
} | ||
compile(message) { | ||
return new _messageformat2.default(this.language).compile(message); | ||
} | ||
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'; | ||
}); | ||
exports.select = exports.plural = exports.t = exports.I18n = undefined; | ||
exports.I18n = undefined; | ||
@@ -13,14 +13,5 @@ var _i18n = require('./i18n'); | ||
var _t = require('./t'); | ||
var _t2 = _interopRequireDefault(_t); | ||
var _select = require('./select'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.default = _i18n2.default; | ||
exports.I18n = _i18n.I18n; | ||
exports.t = _t2.default; | ||
exports.plural = _select.plural; | ||
exports.select = _select.select; | ||
exports.I18n = _i18n.I18n; |
@@ -6,57 +6,39 @@ 'use strict'; | ||
}); | ||
exports.select = exports.plural = undefined; | ||
var _variables = require('./variables'); | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
const plural = (_ref) => { | ||
let value = _ref.value, | ||
offset = _ref.offset, | ||
pluralForms = _objectWithoutProperties(_ref, ['value', 'offset']); | ||
const plural = i18n => (_ref) => { | ||
let value = _ref.value; | ||
var _ref$offset = _ref.offset; | ||
const variable = (0, _variables.variableName)(value); | ||
const params = { | ||
[variable]: value[variable] | ||
}; | ||
let offset = _ref$offset === undefined ? 0 : _ref$offset, | ||
other = _ref.other, | ||
pluralForms = _objectWithoutProperties(_ref, ['value', 'offset', 'other']); | ||
const forms = Object.keys(pluralForms).map(key => { | ||
const formKey = /^\d+$/.test(key) ? `=${key}` : key; | ||
const form = pluralForms[key]; | ||
const translation = pluralForms[(value - offset).toString()] || pluralForms[i18n.pluralForm(value - offset)] || other; | ||
return translation.replace('#', value.toString()); | ||
}; | ||
let message; | ||
if (typeof form === 'object') { | ||
message = form.message; | ||
Object.assign(params, form.params); | ||
} else { | ||
message = form; | ||
} | ||
const selectOrdinal = i18n => (_ref2) => { | ||
let value = _ref2.value; | ||
var _ref2$offset = _ref2.offset; | ||
return `${formKey} {${message}}`; | ||
}); | ||
let offset = _ref2$offset === undefined ? 0 : _ref2$offset, | ||
other = _ref2.other, | ||
pluralForms = _objectWithoutProperties(_ref2, ['value', 'offset', 'other']); | ||
if (offset) { | ||
forms.unshift(`offset:${offset}`); | ||
} | ||
const message = `{${variable}, plural, ${forms.join(" ")}}`; | ||
return { message, params }; | ||
const translation = pluralForms[(value - offset).toString()] || pluralForms[i18n.pluralForm(value - offset, 'ordinal')] || other; | ||
return translation.replace('#', value.toString()); | ||
}; | ||
const select = (_ref2) => { | ||
let value = _ref2.value, | ||
selectForms = _objectWithoutProperties(_ref2, ['value']); | ||
function select(_ref3) { | ||
let value = _ref3.value, | ||
other = _ref3.other, | ||
selectForms = _objectWithoutProperties(_ref3, ['value', 'other']); | ||
const variable = (0, _variables.variableName)(value); | ||
const params = { | ||
[variable]: value[variable] | ||
}; | ||
return selectForms[value] || other; | ||
} | ||
const forms = Object.keys(selectForms).map(key => `${key} {${selectForms[key].toString()}}`); | ||
const message = `{${variable}, select, ${forms.join(" ")}}`; | ||
return { message, params }; | ||
}; | ||
exports.plural = plural; | ||
exports.select = select; | ||
exports.select = select; | ||
exports.selectOrdinal = selectOrdinal; |
21
lib/t.js
@@ -7,3 +7,2 @@ 'use strict'; | ||
var _variables = require('./variables'); | ||
@@ -13,19 +12,13 @@ const flatten = arrays => [].concat.apply([], arrays); | ||
const t = (strings, ...values) => { | ||
if (typeof strings === 'string') { | ||
const message = strings; | ||
const params = values[0] || {}; | ||
return { message, params }; | ||
const t = i18n => (strings, ...values) => { | ||
if (!Array.isArray(strings)) { | ||
const id = strings.id, | ||
params = strings.params; | ||
return i18n.translate({ id, params }); | ||
} | ||
const params = values.reduce((acc, variable, index) => { | ||
Object.assign(acc, (0, _variables.annotateVariable)(variable, index)); | ||
return acc; | ||
}, {}); | ||
const keys = Object.keys(params).map(key => `{${key}}`); | ||
const message = flatten(zip(strings, keys)).join(''); | ||
return { message, params }; | ||
return flatten(zip(strings, values)).join(''); | ||
}; | ||
exports.default = t; |
{ | ||
"name": "lingui-i18n", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "I18n tools for javascript", | ||
"main": "lib/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lingui/js-lingui/" | ||
"author": { | ||
"name": "Tomáš Ehrlich", | ||
"email": "tomas.ehrlich@gmail.com" | ||
}, | ||
"license": "MIT", | ||
"keywords": [ | ||
@@ -14,7 +15,9 @@ "i18n", | ||
], | ||
"author": { | ||
"name": "Tomáš Ehrlich", | ||
"email": "tomas.ehrlich@gmail.com" | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/lingui/js-lingui.git" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/lingui/js-lingui/issues" | ||
}, | ||
"dependencies": { | ||
@@ -34,3 +37,4 @@ "messageformat": "^1.0.2" | ||
"build": "babel src/ --out-dir lib/ --ignore *.test.js", | ||
"prepublish": "npm run clean && npm run build" | ||
"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" | ||
}, | ||
@@ -37,0 +41,0 @@ "babel": { |
@@ -22,8 +22,14 @@ # lingui-i18n | ||
i18n.use('fr', { | ||
"Hello World!": "Salut le monde!", | ||
"My name is {name}": "Je m'appelle {name}", | ||
"{count, plural, one {# book} others {# books}}": "{count, plural, one {# livre} others {# livres}}" | ||
// load messages | ||
i18n.load({ | ||
fr: { | ||
"Hello World!": "Salut le monde!", | ||
"My name is {name}": "Je m'appelle {name}", | ||
"{count, plural, one {# book} other {# books}}": "{count, plural, one {# livre} other {# livres}}" | ||
} | ||
}) | ||
// set active language | ||
i18n.use('fr') | ||
``` | ||
@@ -51,3 +57,3 @@ | ||
one: "# book", | ||
others: "# books" | ||
other: "# books" | ||
}) | ||
@@ -77,4 +83,4 @@ // becomes "42 livres" | ||
male: plural({...}), | ||
others: plural({...}), | ||
other: plural({...}), | ||
}) | ||
``` |
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
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
43973
13
439
1
84
1