@gullerya/i18n
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -98,4 +98,3 @@ import * as DataTier from './data-tier/data-tier.min.js'; | ||
// apply global styling | ||
document.documentElement.lang = currentLocale.key; | ||
document.body.dir = currentLocale.dir; | ||
Object.assign(document.documentElement, { lang: currentLocale.lang, dir: currentLocale.dir }); | ||
@@ -165,3 +164,3 @@ // apply new locale to any registered component | ||
if (typeof packLocaleData === 'object') { | ||
result = packMeta[localeKey]; | ||
result = packLocaleData; | ||
} else if (typeof packLocaleData === 'function') { | ||
@@ -188,3 +187,5 @@ result = await Promise.resolve(packLocaleData()); | ||
function ensureI18nDataStore() { | ||
return i18nData || DataTier.ties.create(namespace); | ||
if (!i18nData) { | ||
i18nData = DataTier.ties.create(namespace); | ||
} | ||
} |
@@ -1,1 +0,1 @@ | ||
import*as DataTier from"./data-tier/data-tier.min.js";class e{constructor(e,t,r,o){if(!e)throw new Error(`invalid key param "${e}"`);if("ltr"!==t&&"rtl"!==t)throw new Error(`invalid dir param "${t}" (only "ltr" or "rtl" allowed)`);if(!r||"string"!=typeof r)throw new Error(`invalid lang param "${r}"`);if(!o||"string"!=typeof o)throw new Error(`invalid label param "${o}"`);this.key=e,this.dir=t,this.lang=r,this.label=o,Object.freeze(this)}}const t=DataTier.ties.get("i18nLocales")||DataTier.ties.create("i18nLocales",[]),r=new EventTarget,o="localeSet",n="localeApplied",a={},i=Symbol("default.pack.locale");let l,c,s="i18n";export{f as setNamespace,t as locales,y as definePack,d as addLocale,w as getActiveLocale,u as setActiveLocale,r as events,o as LOCALE_SET_EVENT,n as LOCALE_APPLIED_EVENT};function f(e){if(!e||"string"!=typeof e)throw new Error(`invalid namespace '${e}'`);e!==s&&(l=DataTier.ties.create(e,l||{}),s=e)}function d(r,o,n,a){if(t.some(e=>e.key===r))throw new Error(`locale '${r}' already exists`);const i=new e(r,o,n,a);t.push(i)}function w(){return c}async function u(e){if(!e)throw new Error(`invalid locale "${e}"`);const i="object"==typeof e?t.find(t=>t===e):t.find(t=>t.key===e);if(!i)throw new Error(`"${e}" unmatchable to any of known locales`);if(!c||c.key!==i.key){const e={current:i,previous:c};c=i,r.dispatchEvent(new CustomEvent(o,{detail:e})),document.documentElement.lang=c.key,document.body.dir=c.dir;const t=[];Object.keys(a).forEach(e=>t.push(h(e))),await Promise.all(t),r.dispatchEvent(new CustomEvent(n,{detail:e}))}}async function y(e,t,r){const o=Object.assign({defaultLocale:"en"},r);if(!e||"string"!=typeof e)throw new Error(`invalid pack key "${e}"`);if(e in a)throw new Error(`key "${e}" is already defined`);if(!t||"object"!=typeof t)throw new Error(`invalid source "${t}"`);if(!Object.keys(t).length)throw new Error(`invalid source "${t}" (no locale keys found, at least one expected)`);t[i]=o.defaultLocale in t?o.defaultLocale:Object.keys(t)[0],a[e]=t,await h(e)}async function h(e){const t=c?c.key:null;try{l||DataTier.ties.create(s),l[e]=t?await async function(e,t){let r=null;const o=a[t];if(o){let n=o[e];if(!n){const r=o[i];n=o[r],console.warn(`no i18n data found for "${e}" of "${t}" pack, fallen back to "${r}"`)}if("object"==typeof n)r=o[e];else if("function"==typeof n)r=await Promise.resolve(n());else if("string"==typeof n){console.info(`fetching i18n data for "${e}" of "${t}" pack from "${n}"...`);try{const a=await fetch(o[e]);if(!a.ok)throw new Error(`unexpected status '${a.status}`);r=await a.json(),o[e]=r}catch(r){console.error(`failed to fetch i18n data for "${e}" of "${t}" from "${n}", error:`,r)}}}else console.warn(`no i18n data found for "${t}" pack`);return r}(t,e):null}catch(r){console.error(`failed to apply i18n data for "${t}" of "${e}" pack, error:`,r)}}t.length||(d("en","ltr","en","English"),d("he","rtl","he","עברית"),d("ru","ltr","ru","Русский"),d("ar","rtl","ar","عربى")),u("en"); | ||
import*as DataTier from"./data-tier/data-tier.min.js";class e{constructor(e,t,r,o){if(!e)throw new Error(`invalid key param "${e}"`);if("ltr"!==t&&"rtl"!==t)throw new Error(`invalid dir param "${t}" (only "ltr" or "rtl" allowed)`);if(!r||"string"!=typeof r)throw new Error(`invalid lang param "${r}"`);if(!o||"string"!=typeof o)throw new Error(`invalid label param "${o}"`);this.key=e,this.dir=t,this.lang=r,this.label=o,Object.freeze(this)}}const t=DataTier.ties.get("i18nLocales")||DataTier.ties.create("i18nLocales",[]),r=new EventTarget,o="localeSet",n="localeApplied",a={},i=Symbol("default.pack.locale");let l,c,s="i18n";export{f as setNamespace,t as locales,h as definePack,d as addLocale,w as getActiveLocale,u as setActiveLocale,r as events,o as LOCALE_SET_EVENT,n as LOCALE_APPLIED_EVENT};function f(e){if(!e||"string"!=typeof e)throw new Error(`invalid namespace '${e}'`);e!==s&&(l=DataTier.ties.create(e,l||{}),s=e)}function d(r,o,n,a){if(t.some(e=>e.key===r))throw new Error(`locale '${r}' already exists`);const i=new e(r,o,n,a);t.push(i)}function w(){return c}async function u(e){if(!e)throw new Error(`invalid locale "${e}"`);const i="object"==typeof e?t.find(t=>t===e):t.find(t=>t.key===e);if(!i)throw new Error(`"${e}" unmatchable to any of known locales`);if(!c||c.key!==i.key){const e={current:i,previous:c};c=i,r.dispatchEvent(new CustomEvent(o,{detail:e})),Object.assign(document.documentElement,{lang:c.lang,dir:c.dir});const t=[];Object.keys(a).forEach(e=>t.push(y(e))),await Promise.all(t),r.dispatchEvent(new CustomEvent(n,{detail:e}))}}async function h(e,t,r){const o=Object.assign({defaultLocale:"en"},r);if(!e||"string"!=typeof e)throw new Error(`invalid pack key "${e}"`);if(e in a)throw new Error(`key "${e}" is already defined`);if(!t||"object"!=typeof t)throw new Error(`invalid source "${t}"`);if(!Object.keys(t).length)throw new Error(`invalid source "${t}" (no locale keys found, at least one expected)`);t[i]=o.defaultLocale in t?o.defaultLocale:Object.keys(t)[0],a[e]=t,await y(e)}async function y(e){const t=c?c.key:null;try{l||(l=DataTier.ties.create(s)),l[e]=t?await async function(e,t){let r=null;const o=a[t];if(o){let n=o[e];if(!n){const r=o[i];n=o[r],console.warn(`no i18n data found for "${e}" of "${t}" pack, fallen back to "${r}"`)}if("object"==typeof n)r=n;else if("function"==typeof n)r=await Promise.resolve(n());else if("string"==typeof n){console.info(`fetching i18n data for "${e}" of "${t}" pack from "${n}"...`);try{const a=await fetch(o[e]);if(!a.ok)throw new Error(`unexpected status '${a.status}`);r=await a.json(),o[e]=r}catch(r){console.error(`failed to fetch i18n data for "${e}" of "${t}" from "${n}", error:`,r)}}}else console.warn(`no i18n data found for "${t}" pack`);return r}(t,e):null}catch(r){console.error(`failed to apply i18n data for "${t}" of "${e}" pack, error:`,r)}}t.length||(d("en","ltr","en","English"),d("he","rtl","he","עברית"),d("ru","ltr","ru","Русский"),d("ar","rtl","ar","عربى")),u("en"); |
{ | ||
"name": "@gullerya/i18n", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "client (browser) oriented internationalization library", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -20,5 +20,8 @@ [![GitHub](https://img.shields.io/github/license/gullerya/i18n.svg)](https://github.com/gullerya/i18n) | ||
* JSON resource format supported | ||
* contact me for other formats support | ||
#### Support matrix: ![CHROME](docs/browser_icons/chrome.png)<sub>61+</sub> | ![FIREFOX](docs/browser_icons/firefox.png)<sub>60+</sub> | ![EDGE](docs/browser_icons/edge.png)<sub>16+</sub> | ||
#### Support matrix | ||
![CHROME](docs/browser_icons/chrome.png)<sub>61+</sub> | ![FIREFOX](docs/browser_icons/firefox.png)<sub>60+</sub> | ![EDGE](docs/browser_icons/edge-chromium.png)<sub>79+</sub> | ||
#### Last versions (full changelog is [here](docs/changelog.md)) | ||
@@ -33,6 +36,7 @@ | ||
# API | ||
Refer to this [documentation](docs/api.md). | ||
# Usage | ||
APIs described in this [documentation](docs/api.md). | ||
# Basic usage example | ||
Generally, it is highly advised to read some of the [`data-tier`](https://www.npmjs.com/package/data-tier) documentation. Being an underlying engine, it has everything needed to understand usage and internals of `i18n`. | ||
@@ -48,3 +52,3 @@ | ||
i18n.definePack(packKey, { | ||
en: { ...}, // inlining data | ||
en: { ... }, // inlining data | ||
he: '/i18n/about-he.json', // fetched resource | ||
@@ -73,2 +77,7 @@ ru: () => { ... } // function returning data | ||
* `packKey` - first token in the path to the localized text is the component / pack key, used when the internatiolization resources was defined | ||
* `path.to.text` - rest of the path is just a path within the localization data graph | ||
* `path.to.text` - rest of the path is just a path within the localization data graph | ||
# Links | ||
* [API definitions](docs/api.md) | ||
* [Example 1 - end to end working example](docs/examples/example-a/example-a.md) |
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
33670
159
79