A lightweight blazing-fast internationalization (i18n) library for your next web-based project
Go here to see a demo https://appnest-demo.firebaseapp.com/lit-translate.
- Simple API that can return a translation for a given key (out of the box you can use the dot notation eg.
- Works very well with JSON based translation data-structures
- Can interpolate values into the strings
- Customize just about everything (eg. choose your own translations loader, how to interpolate values, empty placeholder and how to look up the strings)
- Caches the translations for maximum performance
- Contains a
lit-htmldirective that automatically updates when the language changes
- Approximately 800 bytes minified & gzipped (2kb without)
➤ Table of Contents
- ➤ Installation
- ➤ 1. Define the translations
- ➤ 2. Register the translate config
- ➤ 3. Set the language
- ➤ 4. Get the translations
- ➤ 5. Interpolate values
- ➤ 6. Use the
- ➤ Customize! (advanced)
- ➤ Wait for strings to be loaded before displaying the component
- ➤ License
npm i @appnest/lit-translate
➤ 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.
➤ 2. Register the translate config
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.
➤ 3. Set the language
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
➤ 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
;get"header.title"; // "Hello"get"header.subtitle"; // "World"
➤ 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
;get"cta.awesome", ; // Cats are awesome!
➤ 6. Use the
translate directive with
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.
➤ 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.
➤ 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
Licensed under MIT.