npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stefanholzapfel/lit-translate

v3.5.0

Published

A lightweight translation directive for Lit

Downloads

269

Readme

lit-translate

A very basic and lightweight translation directive for Lit

npm install @stefanholzapfel/lit-translate

Before using the directive register a "loader" with the service.

The loader is an async function telling the translator how to load any specific language.

It receives a language identifier (string) and must return a Strings object which is an arbitrarily deeply nested object with only string values.

import { TranslateService } from '@stefanholzapfel/lit-translate';

TranslateService.init(
    language => {
        // load and return translations for language here (e.g. fetch from a JSON file)
    });

Set the language to use with use().

The function is async and can be awaited if you want to avoid translation identifiers flashing up in your app.

The translation identifier can be whatever string you want, but I suggest to stick to a standard like ISO639.

await TranslateService.use('en-GB');

The translate directive accepts an identifier in dot notation. For this Strings object:

{
    app: {
        my_string: "TEST"
    }
}
<span>translate('app.my_string')</span>

resolves to:

<span>TEST</span>

The directive will automatically listen for language changes and change all directive values accordingly.

Use translateUnsafeHTML() if you want HTML in translations to be interpreted (otherwise same interface). Use translateUppercase() if you want the output in uppercase letters.

You can have dynamic parts in your translations. Just mark them with {{ name }} e.g.:

{
    app: {
        my_string: "TEST {{ test_var }}"
    }
}
<span>translate('app.my_string', { test_var: "another test" })</span>

resolves to:

<span>TEST another test</span>

Use as many interpolation values as you want. Just add them as properties to the interpolation object.

Interpolations can be strings, TemplateResults or DirectiveResults. That means you can e.g. nest translate directives in each other:

<span>translate('app.my_string', { test_var: translate('app.nested_string') })</span>
TranslateService.clearStrings();

This library also offers a way to provide a translations object in-place and a directive that automatically renders the language currently active in the TranslateService from it:

A sample translations object:

const myTranslations = {
    'en-GB': 'Hi, I'm a translationsObject!',
    'de-DE': 'Hallo, ich bin ein translationsObject!'
};

Usage with the directive:

translateObject(myTranslations);

A fallback language identifier can be provided as second parameter to the directive.

Use translateObjectUnsafeHTML(myTranslations) to interpret HTML in translation strings.