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 🙏

© 2026 – Pkg Stats / Ryan Hefner

fluent-localize-mixin

v0.5.0

Published

A mixin for custom elements that provides easy l10n out of the box.

Downloads

8

Readme

Fluent Localize Mixin

A mixin for custom elements that provides easy l10n out of the box.

Note: this package was previously known as localized-lit-element

Fluent Localize Mixin is a simple mixin that allows to easily localize your custom elements using Mozilla's Project Fluent syntax. It allows you to load .ftl files and pass Fluent strings directly to your element. The translations can then be used by calling the provided method localize.

Getting started

If you have already created your element extending LitElement, adding l10n is incredibly simple. For example, given this LitElement:

<script src="node_modules/@webcomponents/webcomponents-bundle.js"></script>
<script type="module">
  import {LitElement, html} from 'lit-element';

  class MyElement extends LitElement {

    static get properties() { return { mood: { type: String }; }

    render({mood}) {
      return html`<h1>Web Components are ${mood}!</h1>`;
    }

  }

  customElements.define('my-element', MyElement);
</script>

<my-element mood="happy"></my-element>

The same element with l10n added would look like this:

<script src="node_modules/@webcomponents/webcomponents-bundle.js"></script>
<script type="module">
  import {LitElement, html} from 'lit-element';
  import {localize} from 'fluent-localize-mixin';

  class MyElement extends localize(LitElement) {

    static get properties() { return { mood: { type: String }; }

    render({mood}) {
      return html`<h1>${this.localize('my-phrase', {mood})}</h1>`;
    }
  
    constructor() {
      super();

      // Set the component locale
      this.locale = 'en-US';
      // You can also set `this.globalLocale` to share the locale between your
      // elements

      // Add resources to the 'en-US' locale
      this.addResourceForLocale(ftl`
        my-phrase = Web Components are { $mood }!
      `, 'en-US');

      // You can also load resources from an .ftl file using the
      // `loadResourceForLocale` method
    }

  }

  customElements.define('my-element', MyElement);
</script>

<my-element mood="happy"></my-element>

Done!

Available fields and methods

Fields

locale: string

The locale field is used to set and get the default locale for the component. Note that this value is per instance. If a different locale is specified when calling one of the provided methods, that value will take the precedence over this one.

globalLocale: string

The globalLocale field is used to set and get the default global locale. This value is shared across all elements that extend from LocalizedLitElement. You can this value to set the locale at once all over your application. Note that if a locale is specified inside an element, or a provided method is called passing a locale, those values will take the precedence over this one.

Methods

getLocaleContext([locale]: string): MessageContext

Gets the MessageContext for the given locale. If the context for the locale does not exists, a new context is created and associated with the locale. If no locale is provided, the current locale will be used. If no current locale is set, the global locale will be used. If no locale is available at all, an error will be thrown.

addResourceForLocale(fluentTemplate: string, [locale]: string): MessageContext

Adds the given fluent template resource to the given locale context. Returns the locale context with the new resource added.

loadResourceForLocale(path: string, [locale]: string): Promise<MessageContext>

Loads the FTL resource at the given path for the given locale. The resource at the given path will be fetched using the Fetch API. This method returns a promise that resolves after the path is fetched and its values are added to the specified locale context. If fetching the specified path fails, the promise is rejected.

localize(key: string, [params]: any, [locale]: string): string | undefined

Localizes a string based on the current language context. The only mandatory field is the key. The params parameter can be used to pass parameters to the Fluent message. If the string at the given key does not exist, undefined will be returned. You can call this method directly inside your _render method to localize your element template.