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

i18n-string-templates

v2.0.0

Published

Simple i18n handler using string templates and JSON.

Downloads

67

Readme

i18n-string-templates

This package started as a module in several projects,
becoming a standalone more out of a desire to not reproduce code as much as possible!

This module is a PureESM package,
desiring to stay as current as possible in addition to maintaining maximum coverage & code exercise.

This is an implementation and refactor of this project.s

Usage

There are 2 parameters:

  • map of locales w/ string,string objects.
  • locale you want to use right now

What this lets you do is feed in all the values you use, maybe from a shareable json, shared config, or some sort of dynamically imported list.

Some things we support:

  • Template replacement for dynamic labels
  • Strings or Numbers being replaced
  • Allows you to still "i18n" strings that aren't yet added to your list
  • Passing an object to populate with warnings
  • Type suggesting & fractional digit bounding.

Some goals:

  • Staying as small as possible. I want to do this with no extraneous (or ideally no at all) dependencies.
  • Keeping your experience consistent. As part of this, I leverage semantic-release

Some things I want to add:

  • ~~adding a warning.json or some sort of shareable output for strings that have been run through the system that haven't been translated~~
  • ~~Even more coverage~~
  • Anything the community needs to make your i18n experience easier!

Some examples are in the src/test/i18n.spec.js file.

Example:

import use from 'i18n-string-templates';

const locales = {
  en: {
    sample: 'sample',
    '{0} debates {1}': '{0} debattes {1}',
  },
  zh: {
    sample: '样本',
    '{0} debates {1}': '{0}与{1}辩论',
  },
};

// with english
let i18n = use(locales, 'en');
console.log(i18n`sample`); // outputs: sample
console.log(i18n`${'joe'} debates ${'tom'}`); // outputs: joe debattes tom
console.log(i18n`john needs ${241/12}:n(3) units of blood`) //outputs: john needs 20.083 units of blood

// with something else, like Simplified Chinese
i18n = use(locales, 'zh');
console.log(i18n`sample`); // outputs: 样本
console.log(i18n`${'约翰'} debates ${'刘易斯'}`); // outputs: 约翰与刘易斯辩论
console.log(i18n`john needs ${241/12}:n(3) units of blood`) //outputs: john needs 20.083 units of blood

// with a warnings object that you can access
const warnings = { untranslated: {} };
i18n = use(locales, 'en', { warnings });
console.log(i18n`non-extant`); // outputs: non-extant
console.log(JSON.stringify(warnings.untranslated)); //outputs: {"non-extant":"non-extant"}

Specifying "types"

We include a rudimentary "type" forcing syntax for forcing a type on a template parameter.

| Suffix | Type | | :---: | :---: | |:s | String | |:n(x) | Number |

Requirements

  • Node 14+
  • You're using a Pure ESM package. You cannot require() this package.

Super awesome badges

Actions semantic-release Coverage Status npm version