![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@appnest/lit-translate
Advanced tools
A lightweight internationalization (i18n) library for your lit-html based projects
This is a lightweight internationalization (i18n) library for your lit-html based project (or any other project for that matter).
Overview
lit-html
directive that automatically updates when the language updates.get("home.header.title")
)npm i @appnest/lit-translate
// en.json
{
"lang": "en",
"header": {
"title": "Hello",
"subtitle": "World"
},
"cta": {
"awesome": "{{ things }} are awesome!",
"cats": "Cats"
}
}
Use the function loadStrings
to load the strings .json file from a path.
const path = `/assets/i18n/en.json`;
const strings = await loadStrings(path);
Optionally if you want to cache the strings for next time you are using the loadStrings
function, you might want to cache the result using the function addStringsToCache
using the path as key.
addStringsToCache(path, strings);
Use the function setStrings
to set the current strings of the language. When this function is invoked, the event stringsChanged
will be dispatched on the window object.
await setStrings(strings);
To get a translated string use the function get
. Give this function a string with the chain of keys that points to the desired string in the JSON structure. The below example is based on the strings defined in step 1
.
get("lang"); // "en"
get("header.title"); // "Hello"
get("header.subtitle"); // "World"
Using the get
function it is possible to interpolate values. Simply use the {{ key }}
syntax in your strings and provide an object with values replacing those defined in the string when using the get
function. The below example is based on the strings defined in step 1
.
get("cta.awesome", { thing: get("cta.cats") )); // Cats are awesome!
translate
directiveIf 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 method setStrings
is called and the stringsChanged
event is dispatched on the window object.
class MyComponent extends LitElement {
render () {
html`
<h1>${translate("header.title")}</h1>
<p>${translate("header.subtitle")}</p>
<span>${translate("cta.awesome", {things: get("cta.cats") })}</span>
`;
}
}
Licensed under MIT.
FAQs
A lightweight blazing-fast internationalization (i18n) library for your next web-based project
The npm package @appnest/lit-translate receives a total of 0 weekly downloads. As such, @appnest/lit-translate popularity was classified as not popular.
We found that @appnest/lit-translate demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.