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

lisan-plugin-l10n

v0.1.1

Published

Lisan Localization Plugin

Downloads

9

Readme

Lisan Plugin l10n

Lisan localization plugin allows you to format your data based on the locale.

Installation

You can install lisan from the sources below, as you see fit.

from npm

npm install lisan-plugin-l10n

from CDN

<script src="https://unpkg.com/lisan-plugin-l10n/dist/index.umd.js" type="text/javascript"></script>

After adding the script tag above, all public variables will be accessible via window.lisanPluginL10n variables.

Usage

const { lisan } = require('lisan');
const { Localization } = require('lisan-plugin-l10n');
const { tr } = require('lisan-locales');

lisan.use(Localization);

lisan.setLocale(tr);

lisan.toOrdinal(3); // Returns: 3'üncü

Hint

Lisan provides Lisan Locales package which contains production-ready Localization configurations.

You can find the full list of Localization configs here.

Methods

For the full list of methods, see Lisan Localization API.

Locale Configuration

Type Signature

interface LocaleConfig {
  name: string;

  conditions?: Conditions;

  number?: NumberFormatOptions;
  currency?: CurrencyFormatOptions;

  ordinal?: (num: number) => string;

  date?: {
    masks: DateMasks;
    amPm: string[];

    // weeks
    // Sunday is the first day
    weekdays: string[];
    weekdaysShort: string[];
    weekdaysMin: string[];

    // months
    months: string[];
    monthsShort: string[];
  };
}

name

Type: string (Required) Default: ""

name is a string with a BCP 47 language tag.

conditions

Type: Conditions (Optional) Default: {}

conditions is an object contains Condition Tag and Condition Functions.

conditions object will be passed down to lisan.addConditions method.

Type Signature

type ConditionFunction = (num: string | number) => boolean;
type Conditions = Record<string, ConditionFunction>;

Condition keys are especially useful to achieve Pluralization.

number

Type: NumberFormatOptions (Optional) Default: {}

number takes number formatting options. When defined, the number formatter will be available in translations and lisan.toNumber method will be added to lisan instance.

Number formatting options have the following type definition.

Type Signature

interface NumberFormatOptions {
  grouping: {
    delimiters: string[];
    blocks: number[];
  };
  fraction: {
    delimiter: string;
    digits: number;
  };
  zeroFormat: string;
  nullFormat: string;
}

number.grouping

This option is being used to format numbers to have groups. Eg. thousand separators, lakh, wan

number.grouping.blocks

blocks is an array of numbers to define the length of each group.

  • Grouping is done from right to left that means the first number in the array determines the last group length.
  • The Grouping will continue by the last number in the array.
number.grouping.delimiters

delimiters is an array of numbers to define the delimiter of each group.

  • Delimiters match with groups by the array indices.
  • If the length of the blocks array is bigger than the length of delimiters, the last delimiter will be used for the rest of the groups.
  • If the length of the blocks array is less than the length of delimiters, registerLocale method throws an Exception.
Grouping Examples
const output = lisan.toNumber(12345678901234567890);

Info

Grouping configuration options were inspired by cleave.js configuration options.

number.floating

number.floating.precision

This option is used to define the length of the decimal points.

  • If the length of decimal points is longer than precision value, the decimal points will be rounded.
  • If precision is 0, the floating-point will be hidden.
  • If precision is -1, the floating-point will be printed as it is.
number.floating.delimiter

This option is used to define delimiter for the decimal point.

Decimal Point Examples
const output = lisan.toNumber(1234.1234567);

number.zeroFormat

This option is used to output a custom text when the given number equals 0.

number.nullFormat

This option is used to output a custom text when the given number equals to null.

currency

Type: CurrencyFormatOptions (Optional) Default: {}

currency takes currency formatting options. When defined, the currency formatter will be available in translations and lisan.toCurrency method will be added to lisan instance.

Currency formatting options have the following type definition.

Type Signature

interface CurrencyFormatOptions {
  grouping?: {
    delimiters: string[];
    blocks: number[];
  };
  fraction?: {
    delimiter: string;
    digits: number;
  };
  zeroFormat?: string;
  nullFormat?: string;
  template: (formattedNumber: string) => string;
}

Hint

currency inherits configuration from number configuration. So you can override any of the configurations.

{
  "currency": {
    "floating": {
      "delimiter": ".",
      "precision": 2
    },
    "template": function(formattedNumber) {
      return "$" + formattedNumber;
    }
  }
}

currency.template

template is a function used to format price values.

ordinal

Type: CurrencyFormatOptions (Optional) Default: x => x.toString()

ordinal takes ordinal function. The ordinal formatter is always available in translations and lisan.toOrdinal method is always added lisan instance.

Type Signature

{
  ordinal: (number: number) => string;
}

date

Type: DateOptions (Optional) Default: {}

date is being used to set the configuration for various date formatters and it has the following type definition.

interface DateOptions {
  masks: {
    dateTime: string;
    dateShort: string;
    dateMedium: string;
    dateLong: string;
    dateFull: string;
    timeShort: string;
    timeMedium: string;
    timeLong: string;
  };
  amPm: string[];

  // weeks
  weekdays: string[];
  weekdaysShort: string[];
  weekdaysMin: string[];

  // months
  months: string[];
  monthsShort: string[];
}

date.masks

date.masks is being used to generate formatters with the same mask name:

  • dateTime
  • dateShort
  • dateMedium
  • dateLong
  • dateFull
  • timeShort
  • timeMedium
  • timeLong

These formatters then can be used in dictionaries as below:

lisan.add({
  entries: {
    simpleExample: ({ date }, { dateTime }) =>
      `This is formatted ${dateTime(date)}`,
  },
});

Also a method is created for corresponding formatter.

date.amPm

amPM is an array. Only takes two string values in lowercase format, first being the am indicator and the second indicating pm.

date.weekdays

weekdays is an array and contains the names of the weekdays. The first item of the array has to be Sunday.

date.weekdaysShort

Same as date.weekdays, but shorter day names (usually 3 letters, eg. Sat)

date.weekdaysMin

Same as date.weekdays, but shorter day names (usually 2 letters, eg. St).

date.months

months is an array and contains the names of the months. The first item of the array has to be January.

date.monthsShort

Same as date.months, but shorter month names (usually 3 letters, eg. Jan).

Date Formatting Tokens

License

This package is MIT licensed.