Gathering detailed insights and metrics for cldrjs
Gathering detailed insights and metrics for cldrjs
Gathering detailed insights and metrics for cldrjs
Gathering detailed insights and metrics for cldrjs
npm install cldrjs
99.7
Supply Chain
99.2
Quality
76
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
156 Stars
194 Commits
30 Forks
14 Watching
3 Branches
8 Contributors
Updated on 22 Nov 2024
JavaScript (97.45%)
Shell (2.55%)
Cumulative downloads
Total Downloads
Last day
-2.7%
41,188
Compared to previous day
Last week
-1.8%
220,980
Compared to previous week
Last month
11.4%
937,873
Compared to previous month
Last year
11.8%
9,505,868
Compared to previous year
CLDR (unicode.org) provides locale content for i18n software. The data is provided in two formats: LDML (XML format) and JSON. Our goal is to provide a simple layer to facilitate i18n software to access and use the official CLDR JSON data.
File | Minified + gzipped size | Summary |
---|---|---|
cldr.js | 2.1KB | Core library |
cldr/event.js | +1.4KB | Provides methods to allow listening to events, eg. get |
cldr/supplemental.js | +0.5KB | Provides supplemental helper methods |
cldr/unresolved.js | +0.7KB | Provides inheritance support for unresolved data |
Quick jump:
Organization | Project |
---|---|
https://github.com/globalize/globalize | |
https://github.com/angular/angular |
It's designed to work both in the browser and in Node.js. It supports AMD and CommonJs;
See our changelogs.
1// Load the appropriate portion of CLDR JSON data 2Cldr.load( 3 likelySubtagsData, 4 enData, 5 ptBrData 6);
See How to get CLDR JSON data? below for more information on how to get that data.
1var en = new Cldr( "en" ); 2en.attributes; 3// > { 4// "bundle": "en", 5// "minLanguageId": "en", 6// "maxLanguageId": "en-Latn-US", 7// "language": "en", 8// "script": "Latn", 9// "territory": "US", 10// "region": "US" 11// } 12 13var zh = new Cldr( "zh-u-nu-finance-cu-cny" ); 14zh.attributes; 15// > { 16// "bundle": "zh-Hant", 17// "minLanguageId": "zh", 18// "maxLanguageId": "zh-Hans-CN", 19// "language": "zh", 20// "script": "Hans", 21// "territory": "CN", 22// "region": "CN", 23// "u-nu": "finance", 24// "u-cu": "cny" 25// } 26
language
, script
, territory
(also aliased as region
), maxLanguageId
(computed by adding likely subtags) and minLanguageId
(computed by removing likely subtags) according to the specification.bundle
holds the bundle lookup match based on the available loaded CLDR data, obtained by following Bundle Lookup Matcher.Comparison between different locales.
locale | minLanguageId | maxLanguageId | language | script | region |
---|---|---|---|---|---|
en | "en" | "en-Latn-US" | "en" | "Latn" | "US" |
en-US | "en" | "en-Latn-US" | "en" | "Latn" | "US" |
de | "de" | "de-Latn-DE" | "de" | "Latn" | "DE" |
zh | "zh" | "zh-Hans-CN" | "zh" | "Hans" | "CN" |
zh-TW | "zh-TW" | "zh-Hant-TW" | "zh" | "Hant" | "TW" |
ar | "ar" | "ar-Arab-EG" | "ar" | "Arab" | "EG" |
pt | "pt" | "pt-Latn-BR" | "pt" | "Latn" | "BR" |
pt-BR | "pt" | "pt-Latn-BR" | "pt" | "Latn" | "BR" |
pt-PT | "pt-PT" | "pt-Latn-PT" | "pt" | "Latn" | "PT" |
es | "es" | "es-Latn-ES" | "es" | "Latn" | "ES" |
es-AR | "es-AR" | "es-Latn-AR" | "es" | "Latn" | "AR" |
1// Equivalent to: 2// .get( "main/{bundle}/numbers/symbols-numberSystem-latn/decimal" ); 3en.main( "numbers/symbols-numberSystem-latn/decimal" ); 4// > "." 5 6// Equivalent to: 7// .get( "main/{bundle}/numbers/symbols-numberSystem-latn/decimal" ); 8ptBr.main( "numbers/symbols-numberSystem-latn/decimal" ); 9// > ","
Have any locale attributes replaced with their corresponding values by embracing it with {}
. In the example below, {language}
is replaced with "en"
and {territory}
with "US"
.
1// Notice the more complete way to get this data is: 2// cldr.get( "supplemental/gender/personList/{language}" ) || 3// cldr.get( "supplemental/gender/personList/001" ); 4var enGender = en.get( "supplemental/gender/personList/{language}" ); 5// > "neutral" 6 7var USCurrencies = en.get( "supplemental/currencyData/region/{territory}" ); 8// > [ 9// { USD: { _from: "1792-01-01" } }, 10// { USN: { _tender: "false" } }, 11// { USS: { _tender: "false" } } 12// ] 13 14// Notice the more complete way to get this data is: 15// cldr.get( "supplemental/measurementData/measurementSystem/{territory}" ) || 16// cldr.get( "supplemental/measurementData/measurementSystem/001" ); 17var enMeasurementSystem = en.get( "supplemental/measurementData/measurementSystem/{territory}" ); 18// > "US"
Get undefined
for non-existent data.
1en.get( "/crazy/invalid/path" ); 2// ➡ undefined 3 4// Avoid this 5enData && enData.crazy && enData.crazy.invalid && enData.crazy.invalid.path;
If you are using unresolved JSON data, you can resolve them dynamically during runtime by loading the cldr/unresolved.js
extension module. Currently, we support bundle inheritance.
1Cldr.load( 2 unresolvedEnData 3 unresolvedEnGbData, 4 unresolvedEnInData, 5 parentLocalesData, // supplemental 6 likelySubtagsData // supplemental 7); 8 9var enIn = new Cldr( "en-IN" ); 10 11// 1st time retrieved by resolving: en-IN ➡ en-GB (parent locale lookup). 12// Further times retrieved straight from the resolved cache. 13enIn.main( "dates/calendars/gregorian/dateTimeFormats/availableFormats/yMd" ); 14// > "dd/MM/y" 15 16// 1st time retrieved by resolving: en-IN ➡ en-GB (parent locale lookup) ➡ en (truncate lookup) 17// Further times retrieved straight from the resolved cache. 18enIn.main( "numbers/symbols-numberSystem-latn/decimal" ); 19// > "."
We offer some convenient helpers.
1var usFirstDay = en.supplemental.weekData.firstDay(); 2// ➡ sun 3// Equivalent to: 4// en.get( "supplemental/weekData/firstDay/{territory}" ) || 5// en.get( "supplemental/weekData/firstDay/001" ); 6 7var brFirstDay = ptBr.supplemental.weekData.firstDay(); 8// ➡ mon 9// Equivalent to: 10// ptBr.get( "supplemental/weekData/firstDay/{territory}" ) || 11// ptBr.get( "supplemental/weekData/firstDay/001" );
We officially support:
Sniff tests show cldr.js also works on the following browsers:
If you find any bugs, please just let us know. We'll be glad to fix them for the officially supported browsers, or at least update the documentation for the unsupported ones.
cldr.js has no external dependencies. You can include it in the script tag of your page and you're ready to go. Download it by clicking here.
1<script src="cldr.js"></script>
1// Load the appropriate portion of CLDR JSON data. 2// See "How to get CLDR JSON data?" below for more information on how to get that data. 3Cldr.load( cldrJsonData ); 4 5// Instantiate it by passing a locale. 6var ptBr = new Cldr( "pt-BR" ); 7 8// Get CLDR item data given its path. 9// Equivalent to: 10// .get( "main/{bundle}/numbers/symbols-numberSystem-latn/decimal" ); 11ptBr.main( "numbers/symbols-numberSystem-latn/decimal" ); 12// > ","
We are UMD wrapped. So, it supports AMD, CommonJS, or global variables (in case neither AMD nor CommonJS have been detected).
Example of usage on AMD:
1bower install cldrjs
1require.config({ 2 paths: { 3 "cldr": "bower_components/cldrjs/dist/cldr" 4 } 5}); 6 7require( [ "cldr", "cldr/supplemental", "cldr/unresolved" ], function( Cldr ) { 8 ... 9});
Example of usage with Node.js:
1npm install cldrjs
1var Cldr = require( "cldrjs" );
By downloading the JSON packages individually...
Unicode CLDR is available as JSON at https://github.com/unicode-cldr/ (after this json-packaging proposal took place). Please, read https://github.com/unicode-cldr/cldr-json for more information about package organization.
By using a package manager...
cldr-data
can be used for convenience. It always downloads from the correct source.
Use bower bower install cldr-data
(detailed instructions) or npm npm install cldr-data
. For more information, see:
By generating the JSON mappings yourself...
You can generate the JSON representation of the languages not available in the ZIP file by using the official conversion tool (tools.zip
). This ZIP contains a README with instructions on how to build the data.
You can choose to generate unresolved data to save space or bandwidth (-r false
option of the conversion tool) and instead have it resolve at runtime.
The short answer is by using Cldr.load()
and passing the JSON data as the first argument. Below, follow several examples on how this could be accomplished.
For the examples below, first fetch CLDR JSON data:
1wget http://www.unicode.org/Public/cldr/latest/json.zip 2unzip json.zip -d cldr
Example of embedding CLDR JSON data:
1<script> 2// Embedded (hard-coded) CLDR JSON data. 3Cldr.load({ 4 supplemental: { 5 likelySubtags: { 6 ... 7 } 8 } 9}); 10</script>
Example of loading it dynamically:
1<script src="jquery.js"></script> 2<script> 3$.get( "cldr/supplemental/likelySubtags.json", Cldr.load ); 4</script>
Example using AMD (also see our functional tests):
1define([ 2 "cldr", 3 "json!cldr/supplemental/likelySubtags.json" 4], function( Cldr, likelySubtags ) { 5 6 Cldr.load( likelySubtags ); 7 8});
Example using Node.js:
1var Cldr = require( "cldrjs" ); 2Cldr.load( require( "./cldr/supplemental/likelySubtags.json" ) );
It's NOT recommended that libraries embed data into their code logic for several reasons: avoid forcing a certain data version on users, avoid maintaining locale changes, avoid duplicating data among different i18n libraries.
We recommend loading CLDR data must be performed by end user code.
It depends on the used modules.
File | Required CLDR JSON data |
---|---|
cldr.js | cldr/supplemental/likelySubtags.json |
cldr/unresolved.js | cldr/supplemental/parentLocales.json |
cldr/supplemental.js | cldr/supplemental/{timeData, weekData}.json |
You must also load any portion of the CLDR data you plan to use in your library or your end-application.
Cldr.load( json, ... )
Load resolved or unresolved [1] JSON data.
1: Unresolved processing is only available after loading cldr/unresolved.js
extension module.
new Cldr( locale )
Create a new instance of Cldr.
.attributes
Attributes is an Object created during instance initialization (construction) and are used internally by .get()
to replace dynamic parts of an item path.
.get( path )
Get the item data given its path, or undefined
if missing.
.main( path )
It's an alias for .get([ "main/{bundle}", ... ])
.
Cldr.on( event, listener )
Add a listener function to the specified event globally (for all instances).
Cldr.once( event, listener )
Add a listener function to the specified event globally (for all instances). It will be automatically removed after it's first execution.
Cldr.off( event, listener )
Remove a listener function from the specified event globally (for all instances).
.on( event, listener )
Add a listener function to the specified event for this instance.
.once( event, listener )
Add a listener function to the specified event for this instance. It will be automatically removed after it's first execution.
.off( event, listener )
Remove a listener function from the specified event for this instance.
get
➡ ( path, value )
Triggered before a .get()
(or any alias) return. The triggered listener receives the normalized path and the value found.
.supplemental( path )
It's an alias for .get([ "supplemental", ... ])
.
.supplemental.timeData.allowed()
Helper function. Return the supplemental timeData allowed of locale's territory.
.supplemental.timeData.preferred()
Helper function. Return the supplemental timeData preferred of locale's territory.
.supplemental.weekData.firstDay()
Helper function. Return the supplemental weekData firstDay of locale's territory.
.supplemental.weekData.minDays()
Helper function. Return the supplemental weekData minDays of locale's territory as a Number.
.get( path )
Overload (extend) .get()
to get the item data or lookup by following locale inheritance, set a local resolved cache if it's found (for subsequent faster access), or return undefined
.
E_MISSING_BUNDLE
Thrown when none of the loaded CLDR data can be used as a bundle for the corresponding locale. See more information on Bundle Lookup Matcher.
Error object:
Attribute | Value |
---|---|
code | E_MISSING_BUNDLE |
locale | Locale whose bundle could not be found |
E_MISSING_PARAMETER
Thrown when a required parameter is missing on any static or instance methods.
Error object:
Attribute | Value |
---|---|
code | E_MISSING_PARAMETER |
name | Name of the missing parameter |
E_INVALID_PAR_TYPE
Thrown when a parameter has an invalid type on any static or instance methods.
Error object:
Attribute | Value |
---|---|
code | E_INVALID_PAR_TYPE |
name | Name of the invalid parameter |
value | Invalid value |
expected | Expected type |
To run the tests and build, you can run npm scripts. You will need to install all dependencies before:
1npm install
Run tests
1npm run test:unit 2npm run test:functional
Build distribution file.
1npm run build
On MacOS, use gnu-sed (brew install gnu-sed
)
1./chore/release <version> # where version is for example 0.5.2
MIT © Rafael Xavier de Souza
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More