svelte-intl
Advanced tools
Comparing version
@@ -125,15 +125,15 @@ (function (global, factory) { | ||
var text = parseText(current, parentType); | ||
if (text) elements.push(text); | ||
if (text && current.tokens) current.tokens.push([ 'text', pattern.slice(start, current.index) ]); | ||
if (text) { elements.push(text); } | ||
if (text && current.tokens) { current.tokens.push([ 'text', pattern.slice(start, current.index) ]); } | ||
while (current.index < length) { | ||
if (pattern[current.index] === ARG_CLS) { | ||
if (!parentType) throw expected(current) | ||
if (!parentType) { throw expected(current) } | ||
break | ||
} | ||
if (parentType && current.tagsType && pattern.slice(current.index, current.index + TAG_END.length) === TAG_END) break | ||
if (parentType && current.tagsType && pattern.slice(current.index, current.index + TAG_END.length) === TAG_END) { break } | ||
elements.push(parsePlaceholder(current)); | ||
start = current.index; | ||
text = parseText(current, parentType); | ||
if (text) elements.push(text); | ||
if (text && current.tokens) current.tokens.push([ 'text', pattern.slice(start, current.index) ]); | ||
if (text) { elements.push(text); } | ||
if (text && current.tokens) { current.tokens.push([ 'text', pattern.slice(start, current.index) ]); } | ||
} | ||
@@ -221,3 +221,3 @@ return elements | ||
if (pattern[current.index] === NUM_ARG) { | ||
if (current.tokens) current.tokens.push([ 'syntax', NUM_ARG ]); | ||
if (current.tokens) { current.tokens.push([ 'syntax', NUM_ARG ]); } | ||
++current.index; // move passed # | ||
@@ -228,7 +228,7 @@ return [ NUM_ARG ] | ||
var tag = parseTag(current); | ||
if (tag) return tag | ||
if (tag) { return tag } | ||
/* istanbul ignore if should be unreachable if parseAST and parseText are right */ | ||
if (pattern[current.index] !== ARG_OPN) throw expected(current, ARG_OPN) | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_OPN ]); | ||
if (pattern[current.index] !== ARG_OPN) { throw expected(current, ARG_OPN) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_OPN ]); } | ||
++current.index; // move passed { | ||
@@ -238,4 +238,4 @@ skipWhitespace(current); | ||
var id = parseId(current); | ||
if (!id) throw expected(current, 'placeholder id') | ||
if (current.tokens) current.tokens.push([ 'id', id ]); | ||
if (!id) { throw expected(current, 'placeholder id') } | ||
if (current.tokens) { current.tokens.push([ 'id', id ]); } | ||
skipWhitespace(current); | ||
@@ -245,3 +245,3 @@ | ||
if (char === ARG_CLS) { // end placeholder | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_CLS ]); | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_CLS ]); } | ||
++current.index; // move passed } | ||
@@ -251,4 +251,4 @@ return [ id ] | ||
if (char !== ARG_SEP) throw expected(current, ARG_SEP + ' or ' + ARG_CLS) | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_SEP ]); | ||
if (char !== ARG_SEP) { throw expected(current, ARG_SEP + ' or ' + ARG_CLS) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_SEP ]); } | ||
++current.index; // move passed , | ||
@@ -258,8 +258,8 @@ skipWhitespace(current); | ||
var type = parseId(current); | ||
if (!type) throw expected(current, 'placeholder type') | ||
if (current.tokens) current.tokens.push([ 'type', type ]); | ||
if (!type) { throw expected(current, 'placeholder type') } | ||
if (current.tokens) { current.tokens.push([ 'type', type ]); } | ||
skipWhitespace(current); | ||
char = pattern[current.index]; | ||
if (char === ARG_CLS) { // end placeholder | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_CLS ]); | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_CLS ]); } | ||
if (type === 'plural' || type === 'selectordinal' || type === 'select') { | ||
@@ -272,4 +272,4 @@ throw expected(current, type + ' sub-messages') | ||
if (char !== ARG_SEP) throw expected(current, ARG_SEP + ' or ' + ARG_CLS) | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_SEP ]); | ||
if (char !== ARG_SEP) { throw expected(current, ARG_SEP + ' or ' + ARG_CLS) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_SEP ]); } | ||
++current.index; // move passed , | ||
@@ -299,4 +299,4 @@ skipWhitespace(current); | ||
skipWhitespace(current); | ||
if (pattern[current.index] !== ARG_CLS) throw expected(current, ARG_CLS) | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_CLS ]); | ||
if (pattern[current.index] !== ARG_CLS) { throw expected(current, ARG_CLS) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_CLS ]); } | ||
++current.index; // move passed } | ||
@@ -308,3 +308,3 @@ return arg | ||
var tagsType = current.tagsType; | ||
if (!tagsType || current.pattern[current.index] !== TAG_OPN) return | ||
if (!tagsType || current.pattern[current.index] !== TAG_OPN) { return } | ||
@@ -314,17 +314,17 @@ if (current.pattern.slice(current.index, current.index + TAG_END.length) === TAG_END) { | ||
} | ||
if (current.tokens) current.tokens.push([ 'syntax', TAG_OPN ]); | ||
if (current.tokens) { current.tokens.push([ 'syntax', TAG_OPN ]); } | ||
++current.index; // move passed < | ||
var id = parseId(current, true); | ||
if (!id) throw expected(current, 'placeholder id') | ||
if (current.tokens) current.tokens.push([ 'id', id ]); | ||
if (!id) { throw expected(current, 'placeholder id') } | ||
if (current.tokens) { current.tokens.push([ 'id', id ]); } | ||
skipWhitespace(current); | ||
if (current.pattern.slice(current.index, current.index + TAG_SELF_CLS.length) === TAG_SELF_CLS) { | ||
if (current.tokens) current.tokens.push([ 'syntax', TAG_SELF_CLS ]); | ||
if (current.tokens) { current.tokens.push([ 'syntax', TAG_SELF_CLS ]); } | ||
current.index += TAG_SELF_CLS.length; | ||
return [ id, tagsType ] | ||
} | ||
if (current.pattern[current.index] !== TAG_CLS) throw expected(current, TAG_CLS) | ||
if (current.tokens) current.tokens.push([ 'syntax', TAG_CLS ]); | ||
if (current.pattern[current.index] !== TAG_CLS) { throw expected(current, TAG_CLS) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', TAG_CLS ]); } | ||
++current.index; // move passed > | ||
@@ -335,7 +335,7 @@ | ||
var end = current.index; | ||
if (current.pattern.slice(current.index, current.index + TAG_END.length) !== TAG_END) throw expected(current, TAG_END + id + TAG_CLS) | ||
if (current.tokens) current.tokens.push([ 'syntax', TAG_END ]); | ||
if (current.pattern.slice(current.index, current.index + TAG_END.length) !== TAG_END) { throw expected(current, TAG_END + id + TAG_CLS) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', TAG_END ]); } | ||
current.index += TAG_END.length; | ||
var closeId = parseId(current, true); | ||
if (closeId && current.tokens) current.tokens.push([ 'id', closeId ]); | ||
if (closeId && current.tokens) { current.tokens.push([ 'id', closeId ]); } | ||
if (id !== closeId) { | ||
@@ -346,4 +346,4 @@ current.index = end; // rewind for better error message | ||
skipWhitespace(current); | ||
if (current.pattern[current.index] !== TAG_CLS) throw expected(current, TAG_CLS) | ||
if (current.tokens) current.tokens.push([ 'syntax', TAG_CLS ]); | ||
if (current.pattern[current.index] !== TAG_CLS) { throw expected(current, TAG_CLS) } | ||
if (current.tokens) { current.tokens.push([ 'syntax', TAG_CLS ]); } | ||
++current.index; // move passed > | ||
@@ -364,3 +364,3 @@ | ||
(isTag && (char === TAG_OPN || char === TAG_CLS || char === '/')) | ||
) break | ||
) { break } | ||
id += char; | ||
@@ -375,4 +375,4 @@ ++current.index; | ||
var style = parseText(current, '{style}'); | ||
if (!style) throw expected(current, 'placeholder style name') | ||
if (current.tokens) current.tokens.push([ 'style', current.pattern.slice(start, current.index) ]); | ||
if (!style) { throw expected(current, 'placeholder style name') } | ||
if (current.tokens) { current.tokens.push([ 'style', current.pattern.slice(start, current.index) ]); } | ||
return style | ||
@@ -386,3 +386,3 @@ } | ||
if (pattern.slice(current.index, current.index + OFFSET.length) === OFFSET) { | ||
if (current.tokens) current.tokens.push([ 'offset', 'offset' ], [ 'syntax', ':' ]); | ||
if (current.tokens) { current.tokens.push([ 'offset', 'offset' ], [ 'syntax', ':' ]); } | ||
current.index += OFFSET.length; // move passed offset: | ||
@@ -394,4 +394,4 @@ skipWhitespace(current); | ||
} | ||
if (start === current.index) throw expected(current, 'offset number') | ||
if (current.tokens) current.tokens.push([ 'number', pattern.slice(start, current.index) ]); | ||
if (start === current.index) { throw expected(current, 'offset number') } | ||
if (current.tokens) { current.tokens.push([ 'number', pattern.slice(start, current.index) ]); } | ||
offset = +pattern.slice(start, current.index); | ||
@@ -412,4 +412,4 @@ } | ||
var selector = parseId(current); | ||
if (!selector) throw expected(current, 'sub-message selector') | ||
if (current.tokens) current.tokens.push([ 'selector', selector ]); | ||
if (!selector) { throw expected(current, 'sub-message selector') } | ||
if (current.tokens) { current.tokens.push([ 'selector', selector ]); } | ||
skipWhitespace(current); | ||
@@ -426,8 +426,8 @@ options[selector] = parseSubMessage(current, parentType); | ||
function parseSubMessage (current/*: Context */, parentType/*: string */)/*: AST */ { | ||
if (current.pattern[current.index] !== ARG_OPN) throw expected(current, ARG_OPN + ' to start sub-message') | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_OPN ]); | ||
if (current.pattern[current.index] !== ARG_OPN) { throw expected(current, ARG_OPN + ' to start sub-message') } | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_OPN ]); } | ||
++current.index; // move passed { | ||
var message = parseAST(current, parentType); | ||
if (current.pattern[current.index] !== ARG_CLS) throw expected(current, ARG_CLS + ' to end sub-message') | ||
if (current.tokens) current.tokens.push([ 'syntax', ARG_CLS ]); | ||
if (current.pattern[current.index] !== ARG_CLS) { throw expected(current, ARG_CLS + ' to end sub-message') } | ||
if (current.tokens) { current.tokens.push([ 'syntax', ARG_CLS ]); } | ||
++current.index; // move passed } | ||
@@ -447,3 +447,3 @@ return message | ||
); | ||
if (!message) message = errorMessage(expected, found); | ||
if (!message) { message = errorMessage(expected, found); } | ||
message += ' in ' + pattern.replace(/\r?\n/g, '\n'); | ||
@@ -454,3 +454,3 @@ return new SyntaxError(message, expected, found, offset, line, column) | ||
function errorMessage (expected/*: ?string */, found/* string */) { | ||
if (!expected) return 'Unexpected ' + found + ' found' | ||
if (!expected) { return 'Unexpected ' + found + ' found' } | ||
return 'Expected ' + expected + ' but found ' + found | ||
@@ -581,7 +581,7 @@ } | ||
parseNumberPattern: function (pattern/*: ?string */) { | ||
if (!pattern) return | ||
if (!pattern) { return } | ||
var options = {}; | ||
var currency = pattern.match(/\b[A-Z]{3}\b/i); | ||
var syms = pattern.replace(/[^¤]/g, '').length; | ||
if (!syms && currency) syms = 1; | ||
if (!syms && currency) { syms = 1; } | ||
if (syms) { | ||
@@ -594,3 +594,3 @@ options.style = 'currency'; | ||
} | ||
if (!/[@#0]/.test(pattern)) return options.style ? options : undefined | ||
if (!/[@#0]/.test(pattern)) { return options.style ? options : undefined } | ||
options.useGrouping = pattern.indexOf(',') >= 0; | ||
@@ -605,9 +605,9 @@ if (/E\+?[@#0]+/i.test(pattern) || pattern.indexOf('@') >= 0) { | ||
var n = integer.length - 1; | ||
while (integer[n] === '0') --n; | ||
while (integer[n] === '0') { --n; } | ||
options.minimumIntegerDigits = Math.min(Math.max(integer.length - 1 - n, 1), 21); | ||
var fraction = parts[1] || ''; | ||
n = 0; | ||
while (fraction[n] === '0') ++n; | ||
while (fraction[n] === '0') { ++n; } | ||
options.minimumFractionDigits = Math.min(Math.max(n, 0), 20); | ||
while (fraction[n] === '#') ++n; | ||
while (fraction[n] === '#') { ++n; } | ||
options.maximumFractionDigits = Math.min(Math.max(n, 0), 20); | ||
@@ -618,3 +618,3 @@ } | ||
parseDatePattern: function (pattern/*: ?string */) { | ||
if (!pattern) return | ||
if (!pattern) { return } | ||
var options = {}; | ||
@@ -624,3 +624,3 @@ for (var i = 0; i < pattern.length;) { | ||
var n = 1; | ||
while (pattern[++i] === current) ++n; | ||
while (pattern[++i] === current) { ++n; } | ||
switch (current) { | ||
@@ -681,3 +681,3 @@ case 'G': | ||
var lookupClosestLocale = function lookupClosestLocale (locale/*: string | string[] | void */, available/*: { [string]: any } */)/*: ?string */ { | ||
if (typeof locale === 'string' && available[locale]) return locale | ||
if (typeof locale === 'string' && available[locale]) { return locale } | ||
var locales = [].concat(locale || []); | ||
@@ -688,3 +688,3 @@ for (var l = 0, ll = locales.length; l < ll; ++l) { | ||
var candidate = current.join('-'); | ||
if (available[candidate]) return candidate | ||
if (available[candidate]) { return candidate } | ||
current.pop(); | ||
@@ -1297,3 +1297,3 @@ } | ||
if (parts.length === 1) return parts[0] | ||
if (parts.length === 1) { return parts[0] } | ||
return function format (args) { | ||
@@ -1362,3 +1362,3 @@ var message = ''; | ||
function getArg (id/*: string */, args/*: ?Object */)/*: any */ { | ||
if (args && (id in args)) return args[id] | ||
if (args && (id in args)) { return args[id] } | ||
var parts = id.split('.'); | ||
@@ -1388,3 +1388,3 @@ var a = args; | ||
s = +s; | ||
if (!isFinite(s)) return fs(s) | ||
if (!isFinite(s)) { return fs(s) } | ||
var h = ~~(s / 60 / 60); // ~~ acts much like Math.trunc | ||
@@ -1583,3 +1583,3 @@ var m = ~~(s / 60 % 60); | ||
/* istanbul ignore else */ | ||
if (typeof console !== 'undefined') console.warn(message); | ||
if (typeof console !== 'undefined') { console.warn(message); } | ||
} else if (missingTranslation !== 'ignore') { // 'error' | ||
@@ -1598,11 +1598,11 @@ throw new Error(message) | ||
opt = opt || {}; | ||
if (opt.locale) currentLocales = opt.locale; | ||
if ('translations' in opt) translations = opt.translations || {}; | ||
if (opt.generateId) generateId = opt.generateId; | ||
if ('missingReplacement' in opt) missingReplacement = opt.missingReplacement; | ||
if (opt.missingTranslation) missingTranslation = opt.missingTranslation; | ||
if (opt.locale) { currentLocales = opt.locale; } | ||
if ('translations' in opt) { translations = opt.translations || {}; } | ||
if (opt.generateId) { generateId = opt.generateId; } | ||
if ('missingReplacement' in opt) { missingReplacement = opt.missingReplacement; } | ||
if (opt.missingTranslation) { missingTranslation = opt.missingTranslation; } | ||
if (opt.formats) { | ||
if (opt.formats.number) assign(formats.number, opt.formats.number); | ||
if (opt.formats.date) assign(formats.date, opt.formats.date); | ||
if (opt.formats.time) assign(formats.time, opt.formats.time); | ||
if (opt.formats.number) { assign(formats.number, opt.formats.number); } | ||
if (opt.formats.date) { assign(formats.date, opt.formats.date); } | ||
if (opt.formats.time) { assign(formats.time, opt.formats.time); } | ||
} | ||
@@ -1650,3 +1650,3 @@ if (opt.types) { | ||
formatMessage.custom = function (placeholder/*: any[] */, locales/*: Locales */, value/*: any */, args/*: Object */) { | ||
if (!(placeholder[1] in types)) return value | ||
if (!(placeholder[1] in types)) { return value } | ||
return types[placeholder[1]](placeholder, locales)(value, args) | ||
@@ -1683,8 +1683,12 @@ }; | ||
const getPath = (obj, path) => path.split('.').reduce((acc, key) => ( | ||
var getPath = function (obj, path) { return path.split('.').reduce(function (acc, key) { return ( | ||
key in acc ? acc[key] : null | ||
), obj); | ||
); }, obj); }; | ||
const merge = (obj1 = {}, obj2 = {}) => ( | ||
Object.keys(obj2).reduce((acc, key) => { | ||
var merge = function (obj1, obj2) { | ||
if ( obj1 === void 0 ) obj1 = {}; | ||
if ( obj2 === void 0 ) obj2 = {}; | ||
return ( | ||
Object.keys(obj2).reduce(function (acc, key) { | ||
if (obj2[key] !== null && typeof obj2[key] === 'object') { | ||
@@ -1698,14 +1702,17 @@ acc[key] = merge(acc[key], obj2[key]); | ||
); | ||
}; | ||
function intl(store, { | ||
locales, locale | ||
} = { locales: {} }) { | ||
let currentLocale; | ||
function intl(store, ref) { | ||
if ( ref === void 0 ) ref = { locales: {} }; | ||
var locales = ref.locales; | ||
var locale = ref.locale; | ||
const getFormattedMessage = ( | ||
var currentLocale; | ||
var getFormattedMessage = function ( | ||
path, | ||
interpolations | ||
) => { | ||
const message = getPath(locales[currentLocale], path); | ||
if (!message) return path; | ||
) { | ||
var message = getPath(locales[currentLocale], path); | ||
if (!message) { return path; } | ||
@@ -1715,5 +1722,5 @@ return formatMessage(message, interpolations); | ||
store.on('locale', newLocale => { | ||
store.on('locale', function (newLocale) { | ||
if (!(newLocale in locales)) { | ||
console.error(`[svelte-intl] Couldn't find the "${newLocale}" locale.`); | ||
console.error(("[svelte-intl] Couldn't find the \"" + newLocale + "\" locale.")); | ||
return; | ||
@@ -1736,6 +1743,6 @@ } | ||
store.intl = { | ||
setLocale(locale) { | ||
store.fire('locale', locale); | ||
setLocale: function setLocale(newLocale) { | ||
store.fire('locale', newLocale); | ||
}, | ||
extendLocales(newLocales) { | ||
extendLocales: function extendLocales(newLocales) { | ||
locales = merge(locales, newLocales); | ||
@@ -1742,0 +1749,0 @@ } |
@@ -53,4 +53,4 @@ import formatMessage from 'format-message'; | ||
store.intl = { | ||
setLocale(locale) { | ||
store.fire('locale', locale); | ||
setLocale(newLocale) { | ||
store.fire('locale', newLocale); | ||
}, | ||
@@ -57,0 +57,0 @@ extendLocales(newLocales) { |
{ | ||
"name": "svelte-intl", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Internationalize your Svelte apps", | ||
@@ -10,5 +10,6 @@ "main": "dist/svelte-intl.js", | ||
"build": "npm run clean && rollup -c", | ||
"prepublish": "npm run lint && npm run build", | ||
"prepublish": "npm run lint && npm run size && npm run build", | ||
"test": "npm run build && jest", | ||
"lint": "eslint module.js" | ||
"lint": "eslint module.js", | ||
"size": "size-limit" | ||
}, | ||
@@ -43,6 +44,14 @@ "keywords": [ | ||
"rollup": "1.1.0", | ||
"rollup-plugin-buble": "0.19.6", | ||
"rollup-plugin-commonjs": "9.2.0", | ||
"rollup-plugin-node-resolve": "4.0.0", | ||
"size-limit": "0.21.1", | ||
"svelte": "2.16.0" | ||
} | ||
}, | ||
"size-limit": [ | ||
{ | ||
"path": "module.js", | ||
"limit": "6.5 KB" | ||
} | ||
] | ||
} |
@@ -5,2 +5,3 @@ ## svelte-intl | ||
[](https://npm.im/svelte-intl) | ||
[](https://bundlephobia.com/result?p=svelte-intl@latest) | ||
@@ -13,15 +14,24 @@ Internationalize your Svelte apps using [format-message](https://github.com/format-message/format-message). | ||
const store = intl(new Store()); | ||
const store = intl(new Store(), { | ||
locale: 'en', | ||
locales: { | ||
en: { | ||
hello: 'Hello, {name}' | ||
} | ||
} | ||
}); | ||
store.intl.extendLocales({ | ||
en: { | ||
hello: 'Hello, {name}' | ||
ru: { | ||
hello: 'Привет, {name}' | ||
} | ||
}); | ||
store.intl.setLocale('en'); | ||
const { _ } = store.get(); | ||
_('hello', { name: 'John' }) // => 'Hello, John' | ||
console.log(_('hello', { name: 'John' })); // => 'Hello, John' | ||
store.intl.setLocale('ru'); | ||
console.log(_('hello', { name: 'Вася' })); // => 'Привет, Вася' | ||
``` |
62088
1.73%1673
0.3%36
38.46%9
28.57%