Comparing version 4.0.0-alpha.3 to 4.0.0-alpha.4
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toNumber = exports.toFixed = exports.strftime = exports.pluralize = exports.parseDate = exports.lookup = exports.isSet = exports.interpolate = exports.inferType = exports.getFullScope = exports.createTranslationOptions = void 0; | ||
exports.toNumber = exports.toFixed = exports.strftime = exports.pluralize = exports.parseDate = exports.lookup = exports.isSet = exports.interpolate = exports.inferType = exports.getFullScope = exports.flatMap = exports.createTranslationOptions = void 0; | ||
var createTranslationOptions_1 = require("./createTranslationOptions"); | ||
Object.defineProperty(exports, "createTranslationOptions", { enumerable: true, get: function () { return createTranslationOptions_1.createTranslationOptions; } }); | ||
var flatMap_1 = require("./flatMap"); | ||
Object.defineProperty(exports, "flatMap", { enumerable: true, get: function () { return flatMap_1.flatMap; } }); | ||
var getFullScope_1 = require("./getFullScope"); | ||
@@ -7,0 +9,0 @@ Object.defineProperty(exports, "getFullScope", { enumerable: true, get: function () { return getFullScope_1.getFullScope; } }); |
@@ -59,3 +59,3 @@ "use strict"; | ||
class I18n { | ||
constructor(translations, options) { | ||
constructor(translations = {}, options) { | ||
this.t = this.translate; | ||
@@ -81,2 +81,6 @@ this.p = this.pluralize; | ||
} | ||
store(translations) { | ||
const map = helpers_1.flatMap(translations); | ||
map.forEach((path) => lodash_1.set(this.translations, path, lodash_1.get(translations, path))); | ||
} | ||
get locale() { | ||
@@ -83,0 +87,0 @@ return this._locale || this.defaultLocale || "en"; |
export { createTranslationOptions } from "./createTranslationOptions"; | ||
export { flatMap } from "./flatMap"; | ||
export { getFullScope } from "./getFullScope"; | ||
@@ -3,0 +4,0 @@ export { inferType } from "./inferType"; |
@@ -14,3 +14,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { MissingTranslation } from "./MissingTranslation"; | ||
import { createTranslationOptions, inferType, interpolate, isSet, lookup, parseDate, pluralize, strftime, toNumber, } from "./helpers"; | ||
import { createTranslationOptions, flatMap, inferType, interpolate, isSet, lookup, parseDate, pluralize, strftime, toNumber, } from "./helpers"; | ||
const within = (start, end, actual) => actual >= start && actual <= end; | ||
@@ -57,3 +57,3 @@ const DEFAULT_I18N_OPTIONS = { | ||
export class I18n { | ||
constructor(translations, options) { | ||
constructor(translations = {}, options) { | ||
this.t = this.translate; | ||
@@ -79,2 +79,6 @@ this.p = this.pluralize; | ||
} | ||
store(translations) { | ||
const map = flatMap(translations); | ||
map.forEach((path) => set(this.translations, path, get(translations, path))); | ||
} | ||
get locale() { | ||
@@ -81,0 +85,0 @@ return this._locale || this.defaultLocale || "en"; |
{ | ||
"name": "i18n-js", | ||
"version": "4.0.0-alpha.3", | ||
"version": "4.0.0-alpha.4", | ||
"description": "A small library to provide I18n on JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/import/index.js", |
@@ -11,3 +11,3 @@ <p align="center"> | ||
<a href="https://github.com/fnando/i18n/actions?query=workflow%3Atests"><img src="https://github.com/fnando/i18n/workflows/tests/badge.svg" alt="Tests"></a> | ||
<a href="https://www.npmjs.com/package/i18n"><img src="https://img.shields.io/npm/v/i18n-js.svg" alt="npm version"></a> | ||
<a href="https://www.npmjs.com/package/i18n-js"><img src="https://img.shields.io/npm/v/i18n-js.svg" alt="npm version"></a> | ||
<a href="https://www.npmjs.com/package/i18n-js"><img src="https://img.shields.io/npm/dt/i18n-js.svg" alt="npm downloads"></a> | ||
@@ -87,2 +87,43 @@ <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"></a> | ||
### Updating translation store | ||
Updating the translation store is trivial. All you have to do is calling | ||
`I18n#store` with the translations that need to be merged. Let's assume you've | ||
exported all your app's translations using | ||
[i18n-js](https://github.com/fnando/i18n-js) CLI, using a separate file for each | ||
language, like this: | ||
- `translations/en.json` | ||
- `translations/pt-BR.json` | ||
This is how you could update the store: | ||
```js | ||
import { I18n } from "i18n-js"; | ||
import ptBR from "translations/pt-BR.json"; | ||
import en from "translations/en.json"; | ||
const i18n = new I18n(); | ||
i18n.store(en); | ||
i18n.store(ptBR); | ||
``` | ||
This method will allow you to lazy load translations and them updating the store | ||
as needed. | ||
```js | ||
import { I18n } from "i18n-js"; | ||
async function loadTranslations(i18n, locale) { | ||
const response = await fetch(`/translations/${locale}.json`); | ||
const translations = await response.json(); | ||
i18n.store(translations); | ||
} | ||
const i18n = new I18n(); | ||
loadTranslations(i18n, "es"); | ||
``` | ||
### Translating messages | ||
@@ -89,0 +130,0 @@ |
export { createTranslationOptions } from "./createTranslationOptions"; | ||
export { flatMap } from "./flatMap"; | ||
export { getFullScope } from "./getFullScope"; | ||
@@ -3,0 +4,0 @@ export { inferType } from "./inferType"; |
@@ -19,3 +19,4 @@ import { DateTime, Dict, I18nOptions, MissingPlaceholderHandler, NullPlaceholderHandler, Scope, TimeAgoInWordsOptions, ToNumberOptions, ToSentenceOptions, TranslateOptions } from "../index.d"; | ||
nullPlaceholder: NullPlaceholderHandler; | ||
constructor(translations: Dict, options?: Partial<I18nOptions>); | ||
constructor(translations?: Dict, options?: Partial<I18nOptions>); | ||
store(translations: Dict): void; | ||
get locale(): string; | ||
@@ -22,0 +23,0 @@ set locale(newLocale: string); |
Sorry, the diff of this file is too big to display
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
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
1125641
106
2192
565