preact-i18n
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -164,4 +164,2 @@ import { Component, h, cloneElement } from 'preact'; | ||
var currentFields; | ||
/** Populate {{template.fields}} within a given string. | ||
@@ -175,5 +173,4 @@ * | ||
*/ | ||
function template(template, fields) { | ||
currentFields = fields || EMPTY; | ||
return template && template.replace(/\{\{([\w.-]+)\}\}/g, replacer); | ||
function template(template, fields, scope, dictionary) { | ||
return template && template.replace(/\{\{([\w.-]+)\}\}/g, replacer.bind(null, fields || EMPTY, scope, dictionary)); | ||
} | ||
@@ -184,3 +181,3 @@ | ||
*/ | ||
function replacer(s, field) { | ||
function replacer(currentFields, scope, dictionary, s, field) { | ||
var parts = field.split('.'), | ||
@@ -191,2 +188,7 @@ v = currentFields; | ||
if (v == null) { return ''; } // eslint-disable-line eqeqeq | ||
//allow field values to be <Text /> nodes | ||
if (v && v.nodeName === Text) { | ||
return translate(v.attributes.id, scope, dictionary, v.attributes.fields, v.attributes.plural, v.attributes.fallback); | ||
} | ||
} | ||
@@ -237,3 +239,3 @@ // allow for recursive {{config.xx}} references: | ||
return value && template(value, fields) || fallback || null; | ||
return value && template(value, fields, scope, dictionary) || fallback || null; | ||
} | ||
@@ -280,3 +282,3 @@ | ||
* @param {String} props.id Key to look up in intl dictionary, within any parent scopes (`$scope1.$scope2.$id`) | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}` | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}`. Values in the `fields` object will be coerced to strings, with the exception of `<Text/>` nodes which will be resolved to their translated value | ||
* @param {Number} [props.plural] Integer "count", used to select plural forms | ||
@@ -405,3 +407,3 @@ * @param {Object} context | ||
* @param {String} props.id Key to look up in intl dictionary, within any parent scopes (`$scope1.$scope2.$id`) | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}` | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}`. Values in the `fields` object will be coerced to strings, with the exception of `<Text/>` nodes which will be resolved to their translated value | ||
* @param {Number} [props.plural] Integer "count", used to select plural forms | ||
@@ -408,0 +410,0 @@ * @param {Object} context |
@@ -1,1 +0,1 @@ | ||
function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var p=require("preact"),a=t(require("dlv"));function l(t){return null!=t}function c(t,n){for(var r in n)t[r]=n[r];return t}function i(t,n){var r=c({},t);for(var e in n)n.hasOwnProperty(e)&&(t[e]&&n[e]&&"object"==typeof t[e]&&"object"==typeof n[e]?r[e]=i(t[e],n[e]):r[e]=t[e]||n[e]);return r}function f(t){if("string"==typeof(t=t||{})&&(t=t.split(",")),"join"in t){for(var n={},r=0;r<t.length;r++){var e=t[r].trim();e&&(n[e.split(".").pop()]=e)}return n}return t}var u=/[?&#]intl=show/,e=function(t){function n(){t.apply(this,arguments)}return t&&(n.__proto__=t),((n.prototype=Object.create(t&&t.prototype)).constructor=n).prototype.getChildContext=function(){var t=this.props,n=t.scope,r=t.definition,e=t.mark,o=c({},this.context.intl||{});return n&&(o.scope=n),r&&(o.dictionary=i(o.dictionary||{},r)),(e||"undefined"!=typeof location&&String(location).match(u))&&(o.mark=!0),{intl:o}},n.prototype.render=function(t){var n=t.children;return n&&n[0]||null},n}(p.Component);function o(n,r){if(arguments.length<2)return r=n,function(t){return o(t,r)};function t(t){return p.h(e,r||{},p.h(n,t))}return t.getWrappedComponent=n&&n.getWrappedComponent||function(){return n},t}var s,r={};function d(t,n){return s=n||r,t&&t.replace(/\{\{([\w.-]+)\}\}/g,h)}function h(t,n){for(var r=n.split("."),e=s,o=0;o<r.length;o++)if(null==(e=e[r[o]]))return"";return"string"==typeof e&&e.match(/\{\{/)&&(e=d(e,s)),e}function v(t,n,r,e,o,i){n&&(t=n+"."+t);var u=r&&a(r,t);return(o||0===o)&&u&&"object"==typeof u&&(u=u.splice?u[o]||u[0]:0===o&&l(u.none)?u.none:1===o&&l(u.one||u.singular)?u.one||u.singular:u.some||u.many||u.plural||u.other||u),u&&d(u,e)||i||null}function y(t,n){var r=t.value,e=t.id,o=n.intl;if(o&&o.mark){var i="dictionary"+(o&&o.scope?"."+o.scope:"")+"."+e;return p.h("mark",{style:"background: "+(r?a(o,i)?"rgba(119,231,117,.5)":"rgba(229,226,41,.5)":"rgba(228,147,51,.5)"),title:e},r)}return r}function g(t,n){var r=t.id,e=t.children,o=t.plural,i=t.fields,u=n.intl,a=e&&e[0],l=v(r,u&&u.scope,u&&u.dictionary,i,o,a);return p.h(y,{id:r,value:l})}function m(t,n,r){var e={};for(var o in n=n||{},t=f(t))if(t.hasOwnProperty(o)&&t[o]){var i=t[o];if(r||"string"!=typeof i){if(i.nodeName===g){var u=i.children;i=c({fallback:u.length&&(1===u.length?u[0]:u)},i.attributes),e[o]=v(i.id,n.scope,n.dictionary,i.fields,i.plural,i.fallback)}}else e[o]=v(i,n.scope,n.dictionary)}return e}function n(t,n){var r=t.children,e=n.intl,o=r&&r[0];return o&&p.cloneElement(o,m(o.attributes,e,!0))}function x(t){return p.h(n,null,p.h(b,{html:p.h(g,t),id:t.id}))}function b(t){var n=t.html,r=t.id,e=n?"string"==typeof n?p.h("span",{dangerouslySetInnerHTML:{__html:n}}):p.h("span",null,n):n;return p.h(y,{id:r,value:e})}function k(o){return function(e){function t(t,n){var r=m("function"==typeof o?o(t,n):o,n.intl);return p.h(e,c(c({},t),r))}return t.getWrappedComponent=e&&e.getWrappedComponent||function(){return e},t}}(o.intl=o).IntlProvider=e,o.Text=g,o.MarkupText=x,o.Localizer=n,o.withText=k,exports.IntlProvider=e,exports.Localizer=n,exports.MarkupText=x,exports.Text=g,exports.default=o,exports.intl=o,exports.withText=k; | ||
function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var p=require("preact"),a=t(require("dlv"));function l(t){return null!=t}function c(t,n){for(var r in n)t[r]=n[r];return t}function i(t,n){var r=c({},t);for(var e in n)n.hasOwnProperty(e)&&(t[e]&&n[e]&&"object"==typeof t[e]&&"object"==typeof n[e]?r[e]=i(t[e],n[e]):r[e]=t[e]||n[e]);return r}function f(t){if("string"==typeof(t=t||{})&&(t=t.split(",")),"join"in t){for(var n={},r=0;r<t.length;r++){var e=t[r].trim();e&&(n[e.split(".").pop()]=e)}return n}return t}var u=/[?&#]intl=show/,e=function(t){function n(){t.apply(this,arguments)}return t&&(n.__proto__=t),((n.prototype=Object.create(t&&t.prototype)).constructor=n).prototype.getChildContext=function(){var t=this.props,n=t.scope,r=t.definition,e=t.mark,o=c({},this.context.intl||{});return n&&(o.scope=n),r&&(o.dictionary=i(o.dictionary||{},r)),(e||"undefined"!=typeof location&&String(location).match(u))&&(o.mark=!0),{intl:o}},n.prototype.render=function(t){var n=t.children;return n&&n[0]||null},n}(p.Component);function o(n,r){if(arguments.length<2)return r=n,function(t){return o(t,r)};function t(t){return p.h(e,r||{},p.h(n,t))}return t.getWrappedComponent=n&&n.getWrappedComponent||function(){return n},t}var s={};function d(t,n,r,e){return t&&t.replace(/\{\{([\w.-]+)\}\}/g,h.bind(null,n||s,r,e))}function h(t,n,r,e,o){for(var i=o.split("."),u=t,a=0;a<i.length;a++){if(null==(u=u[i[a]]))return"";if(u&&u.nodeName===g)return v(u.attributes.id,n,r,u.attributes.fields,u.attributes.plural,u.attributes.fallback)}return"string"==typeof u&&u.match(/\{\{/)&&(u=d(u,t)),u}function v(t,n,r,e,o,i){n&&(t=n+"."+t);var u=r&&a(r,t);return(o||0===o)&&u&&"object"==typeof u&&(u=u.splice?u[o]||u[0]:0===o&&l(u.none)?u.none:1===o&&l(u.one||u.singular)?u.one||u.singular:u.some||u.many||u.plural||u.other||u),u&&d(u,e,n,r)||i||null}function y(t,n){var r=t.value,e=t.id,o=n.intl;if(o&&o.mark){var i="dictionary"+(o&&o.scope?"."+o.scope:"")+"."+e;return p.h("mark",{style:"background: "+(r?a(o,i)?"rgba(119,231,117,.5)":"rgba(229,226,41,.5)":"rgba(228,147,51,.5)"),title:e},r)}return r}function g(t,n){var r=t.id,e=t.children,o=t.plural,i=t.fields,u=n.intl,a=e&&e[0],l=v(r,u&&u.scope,u&&u.dictionary,i,o,a);return p.h(y,{id:r,value:l})}function b(t,n,r){var e={};for(var o in n=n||{},t=f(t))if(t.hasOwnProperty(o)&&t[o]){var i=t[o];if(r||"string"!=typeof i){if(i.nodeName===g){var u=i.children;i=c({fallback:u.length&&(1===u.length?u[0]:u)},i.attributes),e[o]=v(i.id,n.scope,n.dictionary,i.fields,i.plural,i.fallback)}}else e[o]=v(i,n.scope,n.dictionary)}return e}function n(t,n){var r=t.children,e=n.intl,o=r&&r[0];return o&&p.cloneElement(o,b(o.attributes,e,!0))}function r(t){return p.h(n,null,p.h(m,{html:p.h(g,t),id:t.id}))}function m(t){var n=t.html,r=t.id,e=n?"string"==typeof n?p.h("span",{dangerouslySetInnerHTML:{__html:n}}):p.h("span",null,n):n;return p.h(y,{id:r,value:e})}function x(o){return function(e){function t(t,n){var r=b("function"==typeof o?o(t,n):o,n.intl);return p.h(e,c(c({},t),r))}return t.getWrappedComponent=e&&e.getWrappedComponent||function(){return e},t}}(o.intl=o).IntlProvider=e,o.Text=g,o.MarkupText=r,o.Localizer=n,o.withText=x,exports.IntlProvider=e,exports.Localizer=n,exports.MarkupText=r,exports.Text=g,exports.default=o,exports.intl=o,exports.withText=x; |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("preact"),require("dlv")):"function"==typeof define&&define.amd?define(["exports","preact","dlv"],n):n((t=t||self).preactLocalize={},t.preact,t.dlv)}(this,function(t,c,a){function l(t){return null!=t}function p(t,n){for(var e in n)t[e]=n[e];return t}a=a&&a.hasOwnProperty("default")?a.default:a;var i=/[?&#]intl=show/,r=function(t){function n(){t.apply(this,arguments)}return t&&(n.__proto__=t),((n.prototype=Object.create(t&&t.prototype)).constructor=n).prototype.getChildContext=function(){var t=this.props,n=t.scope,e=t.definition,r=t.mark,o=p({},this.context.intl||{});return n&&(o.scope=n),e&&(o.dictionary=function t(n,e){var r=p({},n);for(var o in e)e.hasOwnProperty(o)&&(n[o]&&e[o]&&"object"==typeof n[o]&&"object"==typeof e[o]?r[o]=t(n[o],e[o]):r[o]=n[o]||e[o]);return r}(o.dictionary||{},e)),(r||"undefined"!=typeof location&&String(location).match(i))&&(o.mark=!0),{intl:o}},n.prototype.render=function(t){var n=t.children;return n&&n[0]||null},n}(c.Component);function o(n,e){if(arguments.length<2)return e=n,function(t){return o(t,e)};function t(t){return c.h(r,e||{},c.h(n,t))}return t.getWrappedComponent=n&&n.getWrappedComponent||function(){return n},t}var u,e={};function f(t,n){return u=n||e,t&&t.replace(/\{\{([\w.-]+)\}\}/g,d)}function d(t,n){for(var e=n.split("."),r=u,o=0;o<e.length;o++)if(null==(r=r[e[o]]))return"";return"string"==typeof r&&r.match(/\{\{/)&&(r=f(r,u)),r}function s(t,n,e,r,o,i){n&&(t=n+"."+t);var u=e&&a(e,t);return(o||0===o)&&u&&"object"==typeof u&&(u=u.splice?u[o]||u[0]:0===o&&l(u.none)?u.none:1===o&&l(u.one||u.singular)?u.one||u.singular:u.some||u.many||u.plural||u.other||u),u&&f(u,r)||i||null}function h(t,n){var e=t.value,r=t.id,o=n.intl;if(o&&o.mark){var i="dictionary"+(o&&o.scope?"."+o.scope:"")+"."+r;return c.h("mark",{style:"background: "+(e?a(o,i)?"rgba(119,231,117,.5)":"rgba(229,226,41,.5)":"rgba(228,147,51,.5)"),title:r},e)}return e}function v(t,n){var e=t.id,r=t.children,o=t.plural,i=t.fields,u=n.intl,a=r&&r[0],l=s(e,u&&u.scope,u&&u.dictionary,i,o,a);return c.h(h,{id:e,value:l})}function y(t,n,e){var r={};for(var o in n=n||{},t=function(t){if("string"==typeof(t=t||{})&&(t=t.split(",")),"join"in t){for(var n={},e=0;e<t.length;e++){var r=t[e].trim();r&&(n[r.split(".").pop()]=r)}return n}return t}(t))if(t.hasOwnProperty(o)&&t[o]){var i=t[o];if(e||"string"!=typeof i){if(i.nodeName===v){var u=i.children;i=p({fallback:u.length&&(1===u.length?u[0]:u)},i.attributes),r[o]=s(i.id,n.scope,n.dictionary,i.fields,i.plural,i.fallback)}}else r[o]=s(i,n.scope,n.dictionary)}return r}function n(t,n){var e=t.children,r=n.intl,o=e&&e[0];return o&&c.cloneElement(o,y(o.attributes,r,!0))}function g(t){return c.h(n,null,c.h(m,{html:c.h(v,t),id:t.id}))}function m(t){var n=t.html,e=t.id,r=n?"string"==typeof n?c.h("span",{dangerouslySetInnerHTML:{__html:n}}):c.h("span",null,n):n;return c.h(h,{id:e,value:r})}function b(o){return function(r){function t(t,n){var e=y("function"==typeof o?o(t,n):o,n.intl);return c.h(r,p(p({},t),e))}return t.getWrappedComponent=r&&r.getWrappedComponent||function(){return r},t}}(o.intl=o).IntlProvider=r,o.Text=v,o.MarkupText=g,o.Localizer=n,o.withText=b,t.IntlProvider=r,t.Localizer=n,t.MarkupText=g,t.Text=v,t.default=o,t.intl=o,t.withText=b,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("preact"),require("dlv")):"function"==typeof define&&define.amd?define(["exports","preact","dlv"],n):n((t=t||self).preactLocalize={},t.preact,t.dlv)}(this,function(t,c,a){function l(t){return null!=t}function f(t,n){for(var e in n)t[e]=n[e];return t}a=a&&a.hasOwnProperty("default")?a.default:a;var o=/[?&#]intl=show/,r=function(t){function n(){t.apply(this,arguments)}return t&&(n.__proto__=t),((n.prototype=Object.create(t&&t.prototype)).constructor=n).prototype.getChildContext=function(){var t=this.props,n=t.scope,e=t.definition,r=t.mark,i=f({},this.context.intl||{});return n&&(i.scope=n),e&&(i.dictionary=function t(n,e){var r=f({},n);for(var i in e)e.hasOwnProperty(i)&&(n[i]&&e[i]&&"object"==typeof n[i]&&"object"==typeof e[i]?r[i]=t(n[i],e[i]):r[i]=n[i]||e[i]);return r}(i.dictionary||{},e)),(r||"undefined"!=typeof location&&String(location).match(o))&&(i.mark=!0),{intl:i}},n.prototype.render=function(t){var n=t.children;return n&&n[0]||null},n}(c.Component);function i(n,e){if(arguments.length<2)return e=n,function(t){return i(t,e)};function t(t){return c.h(r,e||{},c.h(n,t))}return t.getWrappedComponent=n&&n.getWrappedComponent||function(){return n},t}var u={};function p(t,n,e,r){return t&&t.replace(/\{\{([\w.-]+)\}\}/g,function(t,n,e,r,i){for(var o=i.split("."),u=t,a=0;a<o.length;a++){if(null==(u=u[o[a]]))return"";if(u&&u.nodeName===h)return d(u.attributes.id,n,e,u.attributes.fields,u.attributes.plural,u.attributes.fallback)}"string"==typeof u&&u.match(/\{\{/)&&(u=p(u,t));return u}.bind(null,n||u,e,r))}function d(t,n,e,r,i,o){n&&(t=n+"."+t);var u=e&&a(e,t);return(i||0===i)&&u&&"object"==typeof u&&(u=u.splice?u[i]||u[0]:0===i&&l(u.none)?u.none:1===i&&l(u.one||u.singular)?u.one||u.singular:u.some||u.many||u.plural||u.other||u),u&&p(u,r,n,e)||o||null}function s(t,n){var e=t.value,r=t.id,i=n.intl;if(i&&i.mark){var o="dictionary"+(i&&i.scope?"."+i.scope:"")+"."+r;return c.h("mark",{style:"background: "+(e?a(i,o)?"rgba(119,231,117,.5)":"rgba(229,226,41,.5)":"rgba(228,147,51,.5)"),title:r},e)}return e}function h(t,n){var e=t.id,r=t.children,i=t.plural,o=t.fields,u=n.intl,a=r&&r[0],l=d(e,u&&u.scope,u&&u.dictionary,o,i,a);return c.h(s,{id:e,value:l})}function v(t,n,e){var r={};for(var i in n=n||{},t=function(t){if("string"==typeof(t=t||{})&&(t=t.split(",")),"join"in t){for(var n={},e=0;e<t.length;e++){var r=t[e].trim();r&&(n[r.split(".").pop()]=r)}return n}return t}(t))if(t.hasOwnProperty(i)&&t[i]){var o=t[i];if(e||"string"!=typeof o){if(o.nodeName===h){var u=o.children;o=f({fallback:u.length&&(1===u.length?u[0]:u)},o.attributes),r[i]=d(o.id,n.scope,n.dictionary,o.fields,o.plural,o.fallback)}}else r[i]=d(o,n.scope,n.dictionary)}return r}function n(t,n){var e=t.children,r=n.intl,i=e&&e[0];return i&&c.cloneElement(i,v(i.attributes,r,!0))}function e(t){return c.h(n,null,c.h(y,{html:c.h(h,t),id:t.id}))}function y(t){var n=t.html,e=t.id,r=n?"string"==typeof n?c.h("span",{dangerouslySetInnerHTML:{__html:n}}):c.h("span",null,n):n;return c.h(s,{id:e,value:r})}function g(i){return function(r){function t(t,n){var e=v("function"==typeof i?i(t,n):i,n.intl);return c.h(r,f(f({},t),e))}return t.getWrappedComponent=r&&r.getWrappedComponent||function(){return r},t}}(i.intl=i).IntlProvider=r,i.Text=h,i.MarkupText=e,i.Localizer=n,i.withText=g,t.IntlProvider=r,t.Localizer=n,t.MarkupText=e,t.Text=h,t.default=i,t.intl=i,t.withText=g,Object.defineProperty(t,"__esModule",{value:!0})}); |
{ | ||
"name": "preact-i18n", | ||
"amdName": "preactLocalize", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "Preact internationalization, done simply.", | ||
@@ -24,3 +24,4 @@ "source": "src/index.js", | ||
"docs": "documentation readme -q --section API src", | ||
"size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size --raw)\"" | ||
"size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size --raw)\"", | ||
"prepublishOnly": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags" | ||
}, | ||
@@ -94,4 +95,4 @@ "repository": "synacor/preact-i18n", | ||
"peerDependencies": { | ||
"preact": "*" | ||
"preact": "<10" | ||
} | ||
} |
@@ -18,11 +18,15 @@ # preact-i18n 🌎 [![npm](https://img.shields.io/npm/v/preact-i18n.svg?style=flat)](https://npm.im/preact-i18n) [![travis](https://travis-ci.org/synacor/preact-i18n.svg?branch=master)](https://travis-ci.org/synacor/preact-i18n) [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fsynacor%2Fpreact-i18n.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fsynacor%2Fpreact-i18n?ref=badge_shield) | ||
- [Installation](#installation) | ||
- [Getting Started](#getting-started) | ||
- [Fallback Text](#fallback-text) | ||
- [Pluralization and Templating](#pluralization-and-templating) | ||
- [Installation](#installation) | ||
- [Getting Started](#getting-started) | ||
- [Fallback Text](#fallback-text) | ||
- [Pluralization and Templating](#pluralization-and-templating) | ||
- [ESLint Plugin](#eslint-plugin) | ||
- [API](#api) | ||
- [API](#api) | ||
<!-- /MDTOC --> | ||
## Preact Version Support | ||
By default, the `master` branch of this repo supports preact 9 and below, and is published in normal patch/minor/major releases to the `latest` tag in npm. Support for preact X (versions 10+ of preact) is handled in the `preactX` branch and are always published to the `preactx` tag in npm. When preact X obtains widespread adoption, the `master` branch of this project will support preact X and a new major version under `latest` tag will be published to in npm. | ||
## Installation | ||
@@ -224,3 +228,3 @@ | ||
- `props.id` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Key to look up in intl dictionary, within any parent scopes (`$scope1.$scope2.$id`) | ||
- `props.fields` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Values to inject into template `{{fields}}` (optional, default `{}`) | ||
- `props.fields` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Values to inject into template `{{fields}}`. Values in the `fields` object will be coerced to strings, with the exception of `<Text/>` nodes which will be resolved to their translated value (optional, default `{}`) | ||
- `props.plural` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Integer "count", used to select plural forms | ||
@@ -279,3 +283,3 @@ - `context` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** | ||
- `props.plural` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)?** Integer "count", used to select plural forms | ||
- `props.fields` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Values to inject into template `{{fields}}` (optional, default `{}`) | ||
- `props.fields` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Values to inject into template `{{fields}}`. Values in the `fields` object will be coerced to strings, with the exception of `<Text/>` nodes which will be resolved to their translated value (optional, default `{}`) | ||
- `props.children` | ||
@@ -282,0 +286,0 @@ - `context` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** |
@@ -12,3 +12,3 @@ import { h } from 'preact'; | ||
* @param {String} props.id Key to look up in intl dictionary, within any parent scopes (`$scope1.$scope2.$id`) | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}` | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}`. Values in the `fields` object will be coerced to strings, with the exception of `<Text/>` nodes which will be resolved to their translated value | ||
* @param {Number} [props.plural] Integer "count", used to select plural forms | ||
@@ -15,0 +15,0 @@ * @param {Object} context |
@@ -15,3 +15,3 @@ import { h } from 'preact'; | ||
* @param {String} props.id Key to look up in intl dictionary, within any parent scopes (`$scope1.$scope2.$id`) | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}` | ||
* @param {Object} [props.fields={}] Values to inject into template `{{fields}}`. Values in the `fields` object will be coerced to strings, with the exception of `<Text/>` nodes which will be resolved to their translated value | ||
* @param {Number} [props.plural] Integer "count", used to select plural forms | ||
@@ -18,0 +18,0 @@ * @param {Object} context |
@@ -0,5 +1,6 @@ | ||
import { Text } from '../components/text'; | ||
import translate from './translate'; | ||
const EMPTY = {}; | ||
let currentFields; | ||
/** Populate {{template.fields}} within a given string. | ||
@@ -13,5 +14,4 @@ * | ||
*/ | ||
export default function template(template, fields) { | ||
currentFields = fields || EMPTY; | ||
return template && template.replace(/\{\{([\w.-]+)\}\}/g, replacer); | ||
export default function template(template, fields, scope, dictionary) { | ||
return template && template.replace(/\{\{([\w.-]+)\}\}/g, replacer.bind(null, fields || EMPTY, scope, dictionary)); | ||
} | ||
@@ -22,3 +22,3 @@ | ||
*/ | ||
function replacer(s, field) { | ||
function replacer(currentFields, scope, dictionary, s, field) { | ||
let parts = field.split('.'), | ||
@@ -29,2 +29,7 @@ v = currentFields; | ||
if (v == null) return ''; // eslint-disable-line eqeqeq | ||
//allow field values to be <Text /> nodes | ||
if (v && v.nodeName === Text) { | ||
return translate(v.attributes.id, scope, dictionary, v.attributes.fields, v.attributes.plural, v.attributes.fallback); | ||
} | ||
} | ||
@@ -31,0 +36,0 @@ // allow for recursive {{config.xx}} references: |
@@ -42,3 +42,3 @@ import delve from 'dlv'; | ||
return value && template(value, fields) || fallback || null; | ||
return value && template(value, fields, scope, dictionary) || fallback || null; | ||
} |
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
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
98610
940
396