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

opuscapita-i18n

v1.0.12

Published

OpusCapita simple i18n mechanism

Downloads

78

Readme

OpusCapita i18n

Synopsis

Provides data <-> value converters for:

  • Dates
  • Numbers
  • Empty values (Strip to null converter)

Provides simple i18n mechanism for JS applications/modules.

Installation

Using npm

$ npm i --save opuscapita-i18n

Npm scripts

Linting

$ npm run lint

Runing tests

$ npm run test // with coverage
$ npm run testonly // only test resylts

Cleaning work directory (removing built docs, etc.)

$ npm run clean

Usage & API

Converters

Converter is a class that converts a value from its object representation to string one and reverse. All converters (Date, Number, StripToNull) implement the same interface that provides two methods valueToString and stringToValue.

Date Converter

import DateConverter from 'opuscapita-i18n/lib/converters/DateConverter';

let dc = new DateConverter(''MM/dd/yyyy'', 'en');
dc.valueToString(new Date(2001, 0, 15)) === '01/15/2001' // true
dc.stringToValue('01/15/2001').toISOString() === new Date(2001, 0, 15).toISOString() // true

NumberConverter

Format definition is similar to Java's DecimalFormat class, but exponent is not supported

import NumberConverter from 'opuscapita-i18n/lib/converters/NumberConverter';

let nc = new NumberConverter('#,##0.00', ',', '.'); // format, groupSep, decSep, decSepUseAlways = false
nc.valueToString(10000000) === '10,000,000.00' // true
nc.stringToValue('10,000.00') === 10000 // true

Strip to null converter

import StripToNullConverter from 'opuscapita-i18n/lib/converters/StripToNullConverter';

let stnc = new StripToNullConverter();
stnc.valueToString(null) === '' // true
converter.stringToValue('') === null // true

I18nManager

Provides mechanism for internationalization according to the locale (with fallback), passed in the constructor. Also provides facade function for operating with converters, according to format patterns.

import I18nManager from 'opuscapita-i18n/utils/I18nManager';

const formatInfos = {
  'en': {
    datePattern: 'dd/MM/yyyy',
    dateTimePattern: 'dd/MM/yyyy HH:mm:ss',
    integerPattern: '#,##0',
    numberPattern: '#,##0.00#######',
    numberDecimalSeparator: '.',
    numberGroupingSeparator: ',',
    numberGroupingSeparatorUse: true,
  }
};

let i18n = = new I18nManager('en', [{
  locales: ['en'],
  messages: {
    test: 'test',
    format: 'min={min}, max={max}',
    subcomponent: {
      hint: 'nested hint'
    }
  },
}], formatInfos);

// getting simple translation
i18n.getMessage('test') === 'test' // true

// getting translation with attributes
i18n.getMessage('format', { min: 10, max: 100 }) === 'min=10, max=100' // true

// adding translations on runtime
i18n = new I18nManager('de-DE', [], {});
i18n.register('test_component', [
  {
    locales: ['en'],
    messages: {
      component: {
        testMessage1: 'en test message 1',
        testMessage2: 'en test message 2',
      },
    },
  },
  {
    locales: ['de'],
    messages: {
      component: {
        testMessage2: 'de test message 2',
      },
    },
  },
]);

// getting fallback message in default locale (en) in case of error (example de-DE -> de -> en)
i18n.getMessage('component.testMessage1') === 'en test message 1' // true

// getting fallback message in root locale in case of error (example de-DE -> de)
i18n.getMessage('component.testMessage2') === 'de test message 2' // true

// getting fallback message key in case no values were found
i18n.getMessage('component.testMessage3') === 'component.testMessage3' // true

// Converter wrappers
i18n.formatDate(new Date(2001, 0, 10)) === '10/01/2001' //true
i18n.parseDate('10/01/2001').toISOString() === new Date(2001, 0, 10).toISOString() // true
i18n.formatDateTime(new Date(2001, 0, 10)) === '10/01/2001 00:00:00' // true

i18n.formatNumber(10000) === '10,000' // true
i18n.parseNumber('10,000') === 10000 // true

i18n.formatDecimalNumber(10000) === '10,000.00' // true
i18n.parseDecimalNumber('10,000.00') === 10000 // true

// getting some format value
i18n.dateFormat === 'YY' // true

Contributors

License

OpusCapita i18n is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.