plugin for locale functionality of js-joda
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist bump version, update CHANGELOG Nov 3, 2018
examples update to new plugin functionality, do not export plug anymore but au… Oct 29, 2018
packages bump version, update CHANGELOG Nov 3, 2018
src update to new plugin functionality, do not export plug anymore but au… Oct 29, 2018
test update to new plugin functionality, do not export plug anymore but au… Oct 29, 2018
utils
.babelrc update dependencies, change package name to scoped package, fix tests… Oct 28, 2018
.editorconfig add webpack-cli Mar 5, 2018
.eslintignore add packages to .eslintignore Mar 12, 2018
.eslintrc.js switch to eslint-config-js-joda Aug 1, 2017
.gitignore update ignore Oct 16, 2017
.npmignore ignore packages/submodules for npm package Mar 11, 2018
.travis.yml Test select firefox in .travis.yml Jan 2, 2018
CHANGELOG.md bump version, update CHANGELOG Nov 3, 2018
LICENSE initial js-joda-locale plugin setup (mostly copied from js-joda-extra) Mar 23, 2017
README.md
build_package.default.json update build_package.default.json for postinstall steps Mar 5, 2018
build_package.prebuilt.json rename packages ... again... package names must be all lowercase :/ May 14, 2018
esdoc.json initial js-joda-locale plugin setup (mostly copied from js-joda-extra) Mar 23, 2017
karma.conf.js fix karma webpack config Oct 28, 2018
package.json
webpack.config.js set maxEntrypointSize, maxAssetSize to 25MiB ... silence webpack warn… Mar 5, 2018

README.md

additional date-time classes that complement those in js-joda

npm version Build Status Sauce Test Status Coverage Status Tested node version Greenkeeper badge

Sauce Test Status

Motivation

Implementation of locale specific funtionality for js-joda, providing function not implemented in js-joda core

Especially this implements patterns elements to print and parse locale specific dates

Usage

also see examples in examples folder

Use prebuilt locale packages

Since the process described below requires a lot of setup and internal knowledge, we provide prebuilt sets of locales as separate npm packages. So for ease of use you may want to install the corresponding @js-joda/locale_<locale> package. The current list of available prebuilt locales is:

this list could be extended relatively easily if needed, as long as data is available in cldr-data

with these packages, no further steps are needed to build the cldr-data.

Without prebuilt locale packages

It is also possible to not use the prebuilt packages. This results in a more complex setup, but also more control over the complete process.

Dependencies

The implementation requires cldr data provided by the cldr-data package and uses cldrjs to load the data. This is necessary to display and parse locale specific data, e.g DayOfWeek or Month Names.

The cldr data is a peer dependency of this package, meaning it must be provided/npm installed by users of @js-joda/locale

Since the complete cldr-data package can be quite large, the examples and documentation below show ways to dynamically load or reduce the amount of data needed.

The implementation of @js-joda/locale also requires js-joda-timezone package e.g. to parse and output timezone names and offsets

Node

Install joda using npm

    npm install js-joda
    npm install js-joda-timezone
    npm install cldr-data
    npm install cldrjs
    npm install @js-joda/locale

To enable js-joda-locale you will only need to require it, requiring it automatically registers the locale extensions in the base js-joda Note: the Locale class is exported by @js-joda/locale so in order to use it, you will need to extract it from there.

require('@js-joda/locale_<locale>')

since js-joda-locale requires js-joda-timezone it will also need to be provided, as shown in the following examples

es5

const {
    DateTimeFormatter,
    ZonedDateTime,
    ZoneId,
} = require('js-joda');
require('js-joda-timezone');

const {
    Locale,
} = require('@js-joda/locale_en-us');

var zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneId.of('Europe/Berlin'));
console.log('en_US formatted string:', zdt.format(DateTimeFormatter.ofPattern('eeee MMMM dd yyyy GGGG, hh:mm:ss a zzzz, \'Week \' ww, \'Quarter \' QQQ').withLocale(Locale.US)));

this will output en_US formatted string: Friday January 01 2016 Anno Domini, 12:00:00 AM Central European Time, Week 01, Quarter Q1

also see examples/usage_node.js or examples/usage_node_build.js

es6

import { DateTimeFormatter, ZonedDateTime, ZoneId } from 'js-joda';
import 'js-joda-timezone';
import { Locale } from '.@js-joda/locale_en-us';

const zdt = ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, ZoneId.of('Europe/Berlin'));
console.log('en_US formatted string:', zdt.format(DateTimeFormatter.ofPattern('eeee MMMM dd yyyy GGGG, hh:mm:ss a zzzz, \'Week \' ww, \'Quarter \' QQQ').withLocale(Locale.US)));

also see the example

using prebuilt locale files from core @js-joda/locale

If you prefer to download @js-joda/locale as a single dependency (albeit a rather large one in terms of download size), all prebuilt locale packages are also included. You still need to load the separate locale packages, this can be done e.g.

require('@js-joda/locale/dist/prebuilt/en-us');

Browser

  • using requirejs to load
  • might also be possible with the bower version of cldr-data

see the example

Packaging with webpack, minimizing package size

Since the cldr-data files can still be quite large, it is possible to only load the files needed for your application

Also possible would be to use webpack to reduce the overall size of the cldr-data (similar approaches should work with different packaging tools than webpack).

So the following tips are just one way to get the general idea on how to reduce the size of needed cldr-data, we use this for our karma testing setup in karma.conf.js and to build the prebuilt locale packages

In package.json file define which parts of cldr-data to download and install

(for more information see the cldr-data-npm docs)

...
"cldr-data-coverage": "core",
"cldr-data-urls-filter": "(cldr-core|cldr-numbers-modern|cldr-dates-modern)"
...

(data-coverage core only downloads data for the most popular languages / locales, while the urls-filter defines which parts of cldr-data are required for js-joda-locale to work)

In e.g. webpack.config.js, define which parts/locales of the cldr-data files should end up in the final package

You can for example use the null-loader to disable loading cldr-data except for the absolutely required parts/locales

use: [{ loader: 'null-loader' }],
resource: {
    // don't load everything in cldr-data
    test: path.resolve(__dirname, 'node_modules/cldr-data'),
    // except the actual data we need (supplemental and de, en, fr locales from main)
    exclude: [
        path.resolve(__dirname, 'node_modules/cldr-data/main/de'),
        path.resolve(__dirname, 'node_modules/cldr-data/main/en'),
        path.resolve(__dirname, 'node_modules/cldr-data/main/fr'),
        path.resolve(__dirname, 'node_modules/cldr-data/supplemental'),
    ],
}

or (as we do for our prebuilt packages) use the CldrDataIgnorePlugin, provided in utils/CldrDataIgnorePlugin.js

    "plugins": [
        new CldrDataIgnorePlugin(modulesDir, locales)),
    ]

where modulesDir is the absolute path to node_modules and locales is an array of locales to use as they can be defined for the prebuilt packages. This will only load the absolutely required files for js-joda-locale, it is what we use internally for the prebuilt packages and to build packages for our karma tests as well.

Depending on your usecase it might also be necessary to define a "faked" cldr-data module that loads the cldr-data files, this is necessary at least if the code needs to run in the browser since the cldr-data load uses modules not available in browser (e.g. fs)

    // add cldr-data load workaround
    resolve = {
        alias: {
            'cldr-data$': path.resolve(__dirname, 'test/utils/karma_cldrData.js'),
        }
    };

These should be the minimum required parts for js-joda-locale

see the karma.conf.js

Implementation details

provides methods for the following pattern letters of the DateTimeFormatterBuilder and DateTimeFormatter classes of js-joda

Localized Text

  • a for am/pm of day
  • G for era
  • q/Q for localized quarter of year

Zone Text

  • z for time zone name
  • Z for localized ZoneOffsets
  • O for localized ZoneOffsets

Week Information

  • w for week-of-year
  • W for week-of-month
  • Y for week-based-year
  • e for localized day-of-week
  • c for localized day-of-week

some of these are only partially localized, e.g. Q only if three or more Q are used, one or two Q also work with plain js-joda without using js-joda-locale

License

  • js-joda-locale is released under the BSD 3-clause license
  • The author of joda time and the lead architect of the JSR-310 is Stephen Colebourne.