Giter Club home page Giter Club logo

intl-datetimeformat-pattern's Introduction

intl-datetimeformat-pattern

Library to map CLDR Pattern/Skeleton to a valid Intl.DateTimeFormat options object.

Overview

ECMA-402, the Javascript Internationalization Specification does not include a way to format a CLDR Pattern or Skeleton, which is a common features in other programming languages. The reason for this omition is that using a pattern requires the programmer to specify a localized pattern for each locale. That model does not work well for the Web because users will tripover easily by using the same pattern for all locales, which defeats the purposes of Intl. Instead, it expects a bag of options that represent the desired configuration for the output, e.g.:

const options = {
    weekday: 'short',
    month: 'short',
    day: 'numeric',
    year: '2-digit'
};
const dt = new Intl.DateTimeFormat('en', options);
dt.format(Date.now()); // yields 'Fri, Oct 27, 17'

As a result, Javascript engines can decide what is the best possible output based on the provided locale, and the bag of options provided as the second argument for Intl.DateTimeFormat constructor. This complicates things if what you have is a pattern or skeleton from CLDR, and assuming that you, as a developer, know what you are doing, you should be able to rely on patterns to format your date values.

This Library

This library provides a single function that when invoked with a string value representing the pattern or skeleton, returns the most likely options object that can be used as the option bag for Intl.DateTimeFormat constructor.

The translation process is based on CLDR's Date Field Symbol Table:

Examples

import patternToOptions from "intl-datetimeformat-pattern";
const options = patternToOptions("EEE, MMM d, ''yy");
const dt = new Intl.DateTimeFormat('en', options);
dt.format(Date.now()); // yields 'Fri, Oct 27, 17'

Cavets

The mapping process is loose, and for most cases, if the pattern or skeleton is described in CLDR/ICU, it is very likely that it produces the equivalent output. Other arbitrary patterns might result in different results, althought the result will probably preserve a similar set of components and settings that those described in the pattern.

Javascript engines will analyze the options derivated from the pattern, and will attempt to choose the best fit. Usually, that internal best-fit representation matches the original pattern, but this is implementation specific. Microsoft Internet Explorer and Edge do not use CLDR/ICU at the moment, which means they are more likely to differ from the rest of the browsers that are using ICU under the hood.

Contribute

Check out the Contributing document for the details. Thanks!

License

This software is free to use under the MIT license. See the LICENSE file for license text and copyright information.

intl-datetimeformat-pattern's People

Contributors

caridy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.