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

piral-translate

v1.5.4

Published

Plugin for providing translated messages in Piral.

Downloads

4,900

Readme

Piral Logo

Piral Translate · GitHub License npm version tested with jest Community Chat

This is a plugin that only has a peer dependency to piral-core. What piral-translate brings to the table is a set of Pilet API extensions that is used by piral. The set features a simple yet sufficient translation system.

Why and When

One of the key non-functional requirements of many applications is to be localizable. While this area contains many parts such as potential conversion of currency, number formats etc., one elementary part is the translation of labels and text fragments. There are many solutions out there, but piral-translate brings a quite simplified model to the table that works in a distributed way, yet is simple and allows going from proof of concept (language snippets included in the pilets) to massive application later on (language snippets gathered from a dedicated service).

Alternatives: Use a library such as i18next as a shared library. Choose one that makes working with your content authors or translation team easier.

Video

We also have a video for this plugin:

@youtube

Documentation

The following functions are brought to the Pilet API.

translate()

Returns the translation (i.e., a string defined for the currently selected language) of a provided language key.

Variables can also be provided using an object as the second argument. The variable replacement looks for occurrences of double curly braces, e.g., {{foo}}.

setTranslations()

Sets the translations (custom language to key to string mapping) for the pilet.

getTranslations()

Gets the translations defined in the pilet.

addTranslations()

Adds a list of translations (custom language to key to string mapping) to the existing translations for the pilet.

Usage

::: summary: For pilet authors

You can use the setTranslations function from the Pilet API to set predefined translation snippets.

Example use:

import { PiletApi } from '<name-of-piral-instance>';

export function setup(piral: PiletApi) {
  piral.setTranslations({
    en: {
      home: 'home',
    },
    de: {
      home: 'zuhause',
    },
  });
}

You can use the getTranslations function from the Pilet API to get the currently defined translation snippets.

Example use:

import { PiletApi } from '<name-of-piral-instance>';

export function setup(piral: PiletApi) {
  const translations = piral.getTranslations();
  // received: { "en": { "home": "home" }, "de": { "home": "zuhause" }}
}

You can use the translate function from the Pilet API to obtain a translation string for a given key using the currently selected language.

Example use:

import { PiletApi } from '<name-of-piral-instance>';

export function setup(piral: PiletApi) {
  const homeStr = piral.translate('home'); // if in "de" -> "zuhause"
}

:::

::: summary: For Piral instance developers

The provided library only brings API extensions for pilets to a Piral instance.

For the setup of the library itself you'll need to import createLocaleApi from the piral-translate package.

import { createLocaleApi } from 'piral-translate';

The integration looks like:

const instance = createInstance({
  // important part
  plugins: [createLocaleApi()],
  // ...
});

Via the options the available languages, translations, as well as the currently selected language can be chosen.

For example:

const localizer = setupLocalizer({
  language: 'en',
  messages: {
    en: {
      'greeting': 'Hello',
    },
    de: {
      'greeting': 'Hallo',
    },
  },
});

const instance = createInstance({
  // important part
  plugins: [createLocaleApi(localizer)],
  // ...
});

Alternatively, the current language can also be inferred via a function.

const localizer = setupLocalizer({
  language: getUserLocale,
  messages: {
    en: {
      'greeting': 'Hello',
    },
    de: {
      'greeting': 'Hallo',
    },
  },
});

The function getUserLocale retrieves either the cookie with name _culture or local storage value for key locale. In any case either the first found language or ultimately en is used as a fallback.

:::

License

Piral is released using the MIT license. For more information see the license file.