
Research
Security News
The Growing Risk of Malicious Browser Extensions
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
js-joda-locale
Advanced tools
This version of the js-joda-locale
package is depreacted!
No updates will be published to the npm package js-joda-locale
, please use @js-joda/locale
instead
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
also see examples in examples folder
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.
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.
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 install
ed 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
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 need to provide it to js-joda as a plugin via the use
function
jsJoda.use('js-joda-locale')
since js-joda-locale
requires js-joda-timezone
it will also need to be provided, as shown
in the following examples
NOTE: if you are using a destructuring assignment in ES6, it should only be performed after
the plugin use
var joda = require('js-joda').use(require('js-joda-timezone')).use(require('../dist/js-joda-locale'));
var zdt = joda.ZonedDateTime.of(2016, 1, 1, 0, 0, 0, 0, joda.ZoneId.of('Europe/Berlin'));
console.log('en_US formatted string:', zdt.format(joda.DateTimeFormatter.ofPattern('eeee MMMM dd yyyy GGGG, hh:mm:ss a zzzz, \'Week \' ww, \'Quarter \' QQQ').withLocale(joda.Locale.US)));
also see examples/usage_node.js or examples/usage_node_build.js
import { use as jsJodaUse } from 'js-joda';
import jsJodaTimeZone from 'js-joda-timezone';
import jsJodaLocale from 'js-joda-locale';
const { DateTimeFormatter, Locale, ZonedDateTime, ZoneId } = jsJodaUse(jsJodaTimeZone).use(jsJodaLocale);
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
see the example
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
provides methods for the following pattern letters of the DateTimeFormatterBuilder and DateTimeFormatter classes of js-joda
Localized Text
a
for am/pm of dayG
for eraq
/Q
for localized quarter of yearZone Text
z
for time zone nameZ
for localized ZoneOffsetsO
for localized ZoneOffsetsWeek Information
w
for week-of-yearW
for week-of-monthY
for week-based-yeare
for localized day-of-weekc
for localized day-of-weeksome 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
FAQs
plugin for locale functionality for js-joda
We found that js-joda-locale demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover how browser extensions in trusted stores are used to hijack sessions, redirect traffic, and manipulate user behavior.
Research
Security News
An in-depth analysis of credential stealers, crypto drainers, cryptojackers, and clipboard hijackers abusing open source package registries to compromise Web3 development environments.
Security News
pnpm 10.12.1 introduces a global virtual store for faster installs and new options for managing dependencies with version catalogs.