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

klot

v0.1.0-alpha.1

Published

The translation function factory & translation function for Klot.

Downloads

480

Readme

Klot

Klot is a incredibly lightweight (600 bytes gzip+min) translation module with built in variable interpolation written in Typescript. Meet typed translations. It's just a simple factory function that produces a translate function. Translations are supplied using objects or JSON and can either be a simple string or a complex object that can be transformed using transformation function.

Installation

NPM

npm install klot

Yarn

yarn add klot

Usage

To start translating, Klot must be initialized. Import Klot and supply it a locale and a locale map that defines your translations per locale. Use the returned translation function to start translating.

A more thorough documentation is given later on in this README.

Basic usage

import klot from "klot";
import nl from "./nl.json";

const translate = klot("en", {
  // Object literal
  en: {
    "Hello, world": "Hello, beautiful world",
    Strawberries: "Strawberries"
  },
  // JSON import
  nl
});

const helloWorld = translate("Hello, world"); // => "Hello, beautiful world"

const strawberries = translate("Strawberries"); // => "Strawberries"

const unknown = translate("Watermelons"); // TS: Type Error, => "Watermelons" - JS: => "Watermelons"

Interpolation

Lets say you need to use one or more variables inside of your translation. Simply add values to the options object argument to replace the variable name with an actual variable. The syntax is {variableName}, but can be changed to a different RegExp.

const translate = klot("en", {
  en: { "T-shirt: {color}": "T-shirt: {color}" }
});

const tshirtOne = translate("T-shirt: {color}", { values: { color: "Red" } }); // => "T-shirt: Red

const tshirtTwo = translate("T-shirt: {color}", { values: { color: "Blue" } }); // => "T-shirt: Blue

You can also choose to use global variables. Note: local variables will overwrite global variables.

const translate = klot(
  "en",
  {
    en: { "Copyright {brand} {currentYear}": "Copyright {brand} {currentYear}" }
  },
  { values: { brand: "My Brand", currentYear: new Date().getFullYear() } }
);

const copyright = translate("Copyright {brand} {currentYear}"); // => "Copyright My Brand 2019" for example

const copyrightShorthand = translate("T-shirt: {color}", {
  values: { brand: "M.B." }
}); // => "Copyright M.B. 2019" for example

Transformations

Transformations are functions in Klot that can be used to apply logic to your translations. They make translations extensible and smart. A translation's value can be either a simple string or an object of strings. Whenever you use an object as a translation value, the translate function returns function instead of a string. The translation becomes curryable with a transformation function that can be used to determine which translation within the object should be used.

const translate = klot("en", {
  // Object literal
  en: {
    "{amount} Strawberries": {
      none: "No strawberries",
      singular: "One strawberry",
      plural: "{amount} strawberries"
    }
  }
});

function pluralize<
  I extends { none: string; singular: string; plural: string },
  O extends KlotOptions & { values: { amount: number } }
>(input: I, options: O) {
  if (options.values.amount > 0) {
    return input.plural;
  }

  if (options.values.amount === 1) {
    return input.singular;
  }

  return input.none;
}

const noStrawberries = translate("{amount} strawberries", {
  values: { amount: 0 }
})(pluralize); // => "No strawberries"

const oneStrawberries = translate("{amount} strawberries", {
  values: { amount: 1 }
})(pluralize); // => "One strawberry"

const multipleStrawberries = translate("{amount} strawberries", {
  values: { amount: 18 }
})(pluralize); // => "18 strawberries"

Options

TBD

Contributing

TBD