@appnest/lit-translate
Advanced tools
Comparing version
{ | ||
"name": "@appnest/lit-translate", | ||
"version": "1.1.9", | ||
"version": "1.1.10", | ||
"license": "MIT", | ||
@@ -57,7 +57,8 @@ "module": "index.js", | ||
"devDependencies": { | ||
"@appnest/web-config": "0.3.19", | ||
"@appnest/readme": "^1.0.2", | ||
"@appnest/web-config": "0.3.21", | ||
"@appnest/readme": "^1.0.18", | ||
"lit-element": "^2.0.1" | ||
}, | ||
"readme": { | ||
"line": "rainbow", | ||
"demo": "https://appnest-demo.firebaseapp.com/lit-translate", | ||
@@ -64,0 +65,0 @@ "toc": true, |
<h1 align="center">@appnest/lit-translate</h1> | ||
<p align="center"> | ||
<a href="https://github.com/web-padawan/awesome-lit-html"><img alt="undefined" src="https://awesome.re/badge.svg" height="20"/></a> | ||
<a href="https://npmcharts.com/compare/@appnest/lit-translate?minimal=true"><img alt="Downloads per month" src="https://img.shields.io/npm/dm/@appnest/lit-translate.svg" height="20"/></a> | ||
<a href="https://npmcharts.com/compare/@appnest/lit-translate?minimal=true"><img alt="Downloads per month" src="https://img.shields.io/npm/dm/@appnest/lit-translate.svg" height="20"/></a> | ||
<a href="https://www.npmjs.com/package/@appnest/lit-translate"><img alt="NPM Version" src="https://img.shields.io/npm/v/@appnest/lit-translate.svg" height="20"/></a> | ||
@@ -9,3 +8,5 @@ <a href="https://david-dm.org/andreasbm/lit-translate"><img alt="Dependencies" src="https://img.shields.io/david/andreasbm/lit-translate.svg" height="20"/></a> | ||
<a href="https://www.webcomponents.org/element/@appnest/lit-translate"><img alt="Published on webcomponents.org" src="https://img.shields.io/badge/webcomponents.org-published-blue.svg" height="20"/></a> | ||
<a href="https://github.com/web-padawan/awesome-lit-html"><img alt="undefined" src="https://awesome.re/badge.svg" height="20"/></a> | ||
</p> | ||
<p align="center"> | ||
@@ -18,2 +19,3 @@ <b>A lightweight blazing-fast internationalization (i18n) library for your next web-based project</b></br> | ||
* Simple API that can return a translation for a given key (out of the box you can use the dot notation eg. `get("home.header.title")`) | ||
@@ -29,26 +31,31 @@ * Works very well with JSON based translation data-structures | ||
## Table of Contents | ||
* [❯ Installation](#-installation) | ||
* [❯ 1. Define the translations](#-1-define-the-translations) | ||
* [❯ 2. Register the translate config](#-2-register-the-translate-config) | ||
* [❯ 3. Set the language](#-3-set-the-language) | ||
* [❯ 4. Get the translations](#-4-get-the-translations) | ||
* [❯ 5. Interpolate values](#-5-interpolate-values) | ||
* [❯ 6. Use the `translate` directive together with `lit-html`](#-6-use-the-translate-directive-together-with-lit-html) | ||
* [❯ Customize! (advanced)](#-customize-advanced) | ||
* [❯ Wait for strings to be loaded before displaying the component](#-wait-for-strings-to-be-loaded-before-displaying-the-component) | ||
* [❯ License](#-license) | ||
 | ||
 | ||
## ➤ Table of Contents | ||
## ❯ Installation | ||
* [➤ Installation](#-installation) | ||
* [➤ 1. Define the translations](#-1-define-the-translations) | ||
* [➤ 2. Register the translate config](#-2-register-the-translate-config) | ||
* [➤ 3. Set the language](#-3-set-the-language) | ||
* [➤ 4. Get the translations](#-4-get-the-translations) | ||
* [➤ 5. Interpolate values](#-5-interpolate-values) | ||
* [➤ 6. Use the `translate` directive together with `lit-html`](#-6-use-the-translate-directive-together-with-lit-html) | ||
* [➤ Customize! (advanced)](#-customize-advanced) | ||
* [➤ Wait for strings to be loaded before displaying the component](#-wait-for-strings-to-be-loaded-before-displaying-the-component) | ||
* [➤ License](#-license) | ||
 | ||
## ➤ Installation | ||
```javascript | ||
npm i @appnest/lit-translate | ||
``` | ||
 | ||
## ❯ 1. Define the translations | ||
 | ||
## ➤ 1. Define the translations | ||
To take advantage of the translation features you need to be able to provide your translations as a JSON structure. You are able to configure how these strings are loaded, but to make things simple we encourage you to maintain your translations in `.json` files - one for each language you support. | ||
@@ -70,6 +77,7 @@ | ||
 | ||
## ❯ 2. Register the translate config | ||
 | ||
## ➤ 2. Register the translate config | ||
Use the `registerTranslateConfig` function to register a loader that loads and parses the translations based on a language identifier. In the example below, a loader is registered which loads a `.json` file with translations for a given language. | ||
@@ -85,6 +93,7 @@ | ||
 | ||
## ❯ 3. Set the language | ||
 | ||
## ➤ 3. Set the language | ||
Invoke the `use` function to set a language. This function will use the registered loader from step 1 to load the strings for the language and dispatch a global `langChanged` event. | ||
@@ -98,6 +107,7 @@ | ||
 | ||
## ❯ 4. Get the translations | ||
 | ||
## ➤ 4. Get the translations | ||
To get a translation use the `get` function. Give this function a string of keys (using the dot notation) that points to the desired translation in the JSON structure. The example below is based on the translations defined in `step 1`. | ||
@@ -112,6 +122,7 @@ | ||
 | ||
## ❯ 5. Interpolate values | ||
 | ||
## ➤ 5. Interpolate values | ||
When using the `get` function it is possible to interpolate values (eg. replace the placeholders with content). As default, you can simply use the `key` syntax in your translations and provide an object with values replacing those defined in the translations when using the `get` function. The example below is based on the strings defined in `step 1`. | ||
@@ -126,6 +137,7 @@ | ||
 | ||
## ❯ 6. Use the `translate` directive together with `lit-html` | ||
 | ||
## ➤ 6. Use the `translate` directive together with `lit-html` | ||
If you are using `lit-html` you might want to use the `translate` directive. This directive makes sure to automatically update all of the translated parts when the `use` function is called and the global `langChanged` event is dispatched. Note that values have to be returned from callbacks to refresh the translated values. | ||
@@ -147,6 +159,7 @@ | ||
 | ||
## ❯ Customize! (advanced) | ||
 | ||
## ➤ Customize! (advanced) | ||
If you want you can customize just about anything by overwriting the configuration hooks. Below is an example on what you might want to customize. | ||
@@ -194,6 +207,7 @@ | ||
 | ||
## ❯ Wait for strings to be loaded before displaying the component | ||
 | ||
## ➤ Wait for strings to be loaded before displaying the component | ||
Sometimes you want to avoid the empty placeholders being shown initially before any of the translation strings has been loaded. To avoid this issue you might want to defer the first update of the component. Here's an example of what you could do if using `lit-element`. | ||
@@ -230,6 +244,7 @@ | ||
 | ||
## ❯ License | ||
 | ||
## ➤ License | ||
Licensed under [MIT](https://opensource.org/licenses/MIT). |
33867
2.31%239
6.7%