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

@markuplint/i18n

v4.5.0

Published

Internationalization for markuplint

Downloads

84,018

Readme

@markuplint/i18n

npm version

Install

markuplint package includes this package.

$ npm install @markuplint/i18n

$ yarn add @markuplint/i18n

API

import { translator } from '@markuplint/i18n';

const t = translator({
  locale: 'ja',
  ...require('@markuplint/i18n/locales/ja.json'),
});

The translator function creates the t function. It is an overloading function that accepts kind of arguments below:

Translate sentence

type T = (template?: string, ...values: string[]) => string;
const message = t(
  // Template #1
  '{0} is {1:c}',
  // The {0} value of template #1
  t(
    // Template #2
    '{0} of {1}',
    // The {0} value of template #2
    t(
      // Template #3
      'the {0}',
      // The {0} value of template #3
      'value',
    ),
    // The {1} value of template #2
    t(
      // Template #4
      'the "{0*}" {1}',
      // The {0} value of template #4
      'id',
      // The {1} value of template #4
      'attribute',
    ),
  ),
  // The {1} value of template #1
  'duplicated',
);

console.log(message);
// => 属性「id」の値が重複しています

Placeholder

There is a placeholder that the number is surrounded by {} on template strings. It is replaced argument as a phrase. It translates the phrase if it matches the keyword defined in the dictionary.

Tagged templates syntax

:warning: It is experimental.

import { taggedTemplateTranslator } from '@markuplint/i18n';

const _ = taggedTemplateTranslator({
  locale: 'ja',
  ...require('path/to/dictionary/ja.json'),
});

const message = _`${
  //
  _`${
    //
    _`the ${'value'}`
  } of ${
    //
    _`the "${'id'}" ${'attribute'}`
  }`
} is ${
  //
  'c:duplicated'
}`;

console.log(message);
// => 属性「id」の値が重複しています

Translate a phrase

type T = (phrase: string) => string;
const phrase = t('element');

console.log(phrase);
// => 要素

Translate listed phrases

type T = (phrases: string[], useLastSeparator?: boolean) => string;
const list = t(['element', 'attribute', 'value']);

console.log(list);
// => 「要素」「属性」「値」

/* If locale is "en" */
console.log(list);
// => "element", "attribute", "value"
const list = t(['element', 'attribute', 'value'], true);

console.log(list);
// => 「要素」「属性」「値」

/* If locale is "en" */
console.log(list);
// => "element", "attribute" and "value"

It converts the character-separated list specified in each locale.

| Locale | Separator | Before Char | After Char | Last Separator | | ------ | -------------------- | -------------------------- | --------------------------- | ------------------------------ | | en | , (comma + space) | " (double quote) | " (double quote) | and (space + chars + space ) | | ja | none (empty string) | (left corner bracket) | (right corner bracket) | none (empty string) |

Avoid translation

The autocomplete is defined as オートコンプリート in the JA dictionary. However, It avoids translation if the number placeholder includes * (asterisk). It is an effective means if you want a code or a specific name.

const phrase = t('the "{0}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
// => 属性「オートコンプリート」

const phrase = t('the "{0*}" {1}', 'autocomplete', 'attribute');
console.log(phrase);
// => 属性「autocomplete」

Another means is that it surrounds % (percentage) to a phrase. It is effective when you use listing.

const phrase = t('the "{0}" {1}', '%autocomplete%', 'attribute');
console.log(phrase);
// => 属性「autocomplete」

const list = t(['element', '%attribute%', 'value']);
console.log(list);
// => 「要素」「attribute」「値」