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

lstrings

v4.1.1

Published

Localization for block

Downloads

2

Readme

lstrings

lstrings is a module created to facilitate the localization of block and its command applications.

Usage

import Localize from "lstrings";
import { getScriptDir } from "own-location";

const localize = new Localize(getScriptDir(import.meta.url), {
	locale: "en-US",
	fallbackLocale: "en-US",
});

async () => {
	console.log(await localize.find("welcome", ["Santiago"]));
	// If certain characteristics of the file system are satisfied:
	// → Thanks for installing this program, Santiago!
};

The Localize constructor has two parameters: lstringsDir, the directory lstrings will be searched in; and options, an object that defines the locale and the fallback locale.

The find method of Localize instances has two parameters: lstringIdentifier and substitutes.

Upon calls to the find method of Localize instances, it is checked whether a file whose name begins with the locale, and ends with .lstrings.js, exists.

If it exists, it is executed, and a property whose name is lstringIdentifier is looked for in the default export.

  • If the property is a function, subtitutes, which is to be an array or other iterable, is passed as its arguments. Then, a Promise is returned that resolves to that function's return value.
  • If the property is not a function, a Promise is returned that resolves to the property.

If it does not exist, the locale is removed all characters that come after its hyphen. It is then checked whether a file whose name begins with the resulting string, and ends with .lstrings.js, exists. If it does, it is subject of an execution of the process described above.

If it does not exist, it is checked whether a file whose name begins with the fallback locale, and ends with .lstrings.js, exists. If it does, it is subject of an execution of the process described above.

Order

  • lstrings file of the locale requested
  • lstrings file whose name is indicative of being in the same language as that requested
  • lstrings file of the fallbackLocale

Examples

If, for example, the lstrings directory contains these files:

.
├── en-US.lstrings.js
├── en-GB.lstrings.js
├── es.lstrings.js
└── script.js

Of which en-US.lstrings.js is of these contents:

export default {
	welcome: (name) => `Thanks for installing this program, ${name}!`,
};

And the locale provided is en-US, the following call of find would return a promise that resolves to "Thanks for installing this program, Santiago!":

localize.find("welcome", ["Santiago"]);

The second argument is an array of values passed to the property whose name is the first argument.

If welcome weren't a function, this array would have been ignored.

Note that the directory lstrings will be searched in is determined by the first argument of the Localize constructor.

Fallbacks

If the requested locale is not available, one which matches its language will be used:

.
├── zh-CN.lstrings.js
├── en-GB.lstrings.js
├── es.lstrings.js
└── script.js

en-GB strings will be provided if en-US ones are requested.

If no available locale is of the requested one's language, the locale corresponding to the fallbackLocale property of the object provided to the Localize constructor is accessed.