Comparing version 4.0.0-alpha.11 to 4.0.0-alpha.12
{ | ||
"name": "i18n-js", | ||
"version": "4.0.0-alpha.11", | ||
"version": "4.0.0-alpha.12", | ||
"description": "A small library to provide I18n on JavaScript.", | ||
@@ -12,2 +12,3 @@ "main": "./dist/import/index.js", | ||
"dist/**/*", | ||
"json/**/*", | ||
"typings/**/*" | ||
@@ -14,0 +15,0 @@ ], |
@@ -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-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/v/i18n-js/next.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> | ||
@@ -30,3 +30,3 @@ <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"></a> | ||
```js | ||
import { I18n } from "i18n"; | ||
import { I18n } from "i18n-js"; | ||
import translations from "./translations.json"; | ||
@@ -958,2 +958,55 @@ | ||
## Troubleshooting | ||
### I'm getting an error like `SyntaxError: Unexpected end of JSON input` or `Uncaught SyntaxError: Unexpected token ;` | ||
You may get such error if you're trying to load empty JSON files with | ||
`import data from "file.json"`. This has nothing to do with I18n and is related | ||
to how your JSON file is loaded. **JSON files must contain valid JSON data.** | ||
Similarly, make sure you're writing valid JSON, and not JavaScript. For | ||
instance, if you write something like `{};`, you'll get an error like | ||
`Uncaught SyntaxError: Unexpected token ;`. | ||
### My JSON contains a flat structure. How can I load and use it with I18n.js? | ||
I18n.js expects a nested object to represent the translation tree. For this | ||
reason, you cannot use an object like the following by default: | ||
```json | ||
{ | ||
"en.messages.hello": "hello", | ||
"pt-BR.messages.hello": "olá" | ||
} | ||
``` | ||
One solution is using something like the following to transform your flat into a | ||
nested object: | ||
```js | ||
const { set } = require("lodash"); | ||
const from = { | ||
"en.messages.hello": "hello", | ||
"pt-BR.messages.hello": "olá", | ||
}; | ||
function flatToNestedObject(target) { | ||
const nested = {}; | ||
Object.keys(target).forEach((path) => set(nested, path, target[path])); | ||
return nested; | ||
} | ||
console.log(flatToNestedObject(from)); | ||
// { | ||
// en: { messages: { hello: 'hello' } }, | ||
// 'pt-BR': { messages: { hello: 'olá' } } | ||
// } | ||
``` | ||
You can also use something like [flat](https://github.com/hughsk/flat) to | ||
perform the same transformation. | ||
## License | ||
@@ -960,0 +1013,0 @@ |
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
1774362
255
26697
1029
1