twitter/twitter-cldr-js

Name: twitter-cldr-js

Owner: Twitter, Inc.

Description: JavaScript implementation of the ICU (International Components for Unicode) that uses the Common Locale Data Repository to format dates, plurals, and more. Based on twitter-cldr-rb.

Created: 2012-07-25 16:38:15.0

Updated: 2017-12-09 02:48:18.0

Pushed: 2016-06-22 19:56:48.0

Homepage:

Size: 27467

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

twitter-cldr-js Build Status

TwitterCldr uses Unicode's Common Locale Data Repository (CLDR) to format certain types of text into their localized equivalents via the Rails asset pipeline. It is a port of twitter-cldr-rb, a Ruby gem that uses the same CLDR data. Originally, this project was not a gem, but a collection of JavaScript files. It has been turned into a gem to move the JavaScript compiling routines from twitter-cldr-rb and provide support for the asset pipeline.

Currently, twitter-cldr-js supports the following:

  1. Date and time formatting
  2. Relative date and time formatting (eg. 1 month ago)
  3. Number formatting (decimal, currency, and percentage)
  4. Long/short decimals
  5. Plural rules
  6. Bidirectional reordering
  7. Text Segmentation
Installation

Add twitter-cldr-js to your Gemfile:

'twitter_cldr_js', :require => 'twitter_cldr/js'

If you're not using bundler, run gem install twitter_cldr_js and then require twitter_cldr/js somewhere in your project. Note that twitter-cldr-js isn't really designed to run outside of Rails. If you need the JavaScript functionality it provides but aren't using Rails, consider copying the compiled JavaScript files (lib/assets/javascripts/twitter_cldr/*.js) into your project by hand instead of using this gem directly.

You can also get twitter-cldr-js in a form of an NPM package:

install twitter_cldr

Check out twitter/twitter-cldr-npm repository for details.

Usage with Rails

To use twitter-cldr-js, you need to make use of two files: the core file with the libraries, core.js and one of the various locale data files, es.js, en.js etc. You can include them in your JavaScript manifest (app/assets/javascripts/application.js) like this:

require twitter_cldr/es
require twitter_cldr/core

This will make the core library twitter-cldr-js available to the JavaScript in your app along with the data bundle for the Spanish locale. If your app supports multiple languages however, this single-locale approach won't be much use. Instead, require the right file with javascript_include_tag for example in a view or a layout:

javascript_include_tag "twitter_cldr/#{I18n.locale}.js" %>
Initialization

You need to load the core library along with a language bundle for optimal use. If you load the data bundle before the core library, the core library sets the data bundle as its data source.

require twitter_cldr/es
require twitter_cldr/core

You can verify that by trying this:

terCldr.Settings.locale(); // "es"

If you only load the core library, without the data set, the same command will result in an error.

only loaded `twitter_cldr/core`)
terCldr.Settings.locale(); // Error: "Data not set"

You can change the data bundle the library is using as its source by invoking the set_data method on the TwitterCldr object.

ata = ...; // The es locale data bundle
ata = ...; // The ar locale data bundle

terCldr.set_data(es_data);
terCldr.Settings.locale(); // "es"

terCldr.set_data(ar_data);
terCldr.Settings.locale(); // "ar"
Dates and Times
nclude the es data bundle for the Spanish DateTimeFormatter
fmt = new TwitterCldr.DateTimeFormatter();

format(new Date(), {"type": "full"});                     // "lunes, 12 de diciembre de 2011 21:44:57 UTC -0800"
format(new Date(), {"type": "long"});                     // "12 de diciembre de 201121:45:42 -08:00"
format(new Date(), {"type": "medium"});                   // "12/12/2011 21:46:09"
format(new Date(), {"type": "short"});                    // "12/12/11 21:47"

format(new Date(), {"format": "date", "type": "full"});   // "lunes, 12 de diciembre de 2011"
format(new Date(), {"format": "date", "type": "long"});   // "12 de diciembre de 2011"
format(new Date(), {"format": "date", "type": "medium"}); // "12/12/2011"
format(new Date(), {"format": "date", "type": "short"});  // "12/12/11"

format(new Date(), {"format": "time", "type": "full"});   // "21:44:57 UTC -0800"
format(new Date(), {"format": "time", "type": "long"});   // "21:45:42 -08:00"
format(new Date(), {"format": "time", "type": "medium"}); // "21:46:09"
format(new Date(), {"format": "time", "type": "short"});  // "21:47"

The default CLDR data set only includes 4 date formats, full, long, medium, and short. See below for a list of additional formats.

Additional Date Formats

Besides the default date formats, CLDR supports a number of additional ones. The list of available formats varys for each locale. To get a full list, use the additional_formats method:

"EEEEd", "Ed", "GGGGyMd", "H", "Hm", "Hms", "M", "MEd", "MMM", "MMMEEEEd", "MMMEd", ... ]
terCldr.DateTimeFormatter.additional_formats();

You can use any of the returned formats as the format option when formatting dates:

0/11/2012 15:38:33
format(new Date(), {});
0 de noviembre
format(new Date(), {"format": "additional", "type": "EEEEd"});

It's important to know that, even though a format may not be available across locales, TwitterCLDR will do it's best to approximate if no exact match can be found.

List of additional date format examples for English:

| Format | Output | |:——-|——————| | EHm | Wed 17:05 | | EHms | Wed 17:05:33 | | Ed | 28 Wed | | Ehm | Wed 5:05 p.m. | | Ehms | Wed 5:05:33 p.m. | | Gy | 2012 AD | | H | 17 | | Hm | 17:05 | | Hms | 17:05:33 | | M | 11 | | MEd | Wed 11/28 | | MMM | Nov | | MMMEd | Wed Nov 28 | | MMMd | Nov 28 | | Md | 11/28 | | d | 28 | | h | 5 p.m. | | hm | 5:05 p.m. | | hms | 5:05:33 p.m. | | ms | 05:33 | | y | 2012 | | yM | 11/2012 | | yMEd | Wed 11/28/2012 | | yMMM | Nov 2012 | | yMMMEd | Wed Nov 28 2012 | | yMMMd | Nov 28 2012 | | yMd | 11/28/2012 | | yQQQ | Q4 2012 | | yQQQQ | 4th quarter 2012 |

Relative Dates and Times

In addition to formatting full dates and times, TwitterCLDR supports relative time spans. It tries to guess the best time unit (eg. days, hours, minutes, etc) based on the length of time given. Indicate past or future by using negative or positive numbers respectively:

nclude the en data bundle for the English TimespanFormatter
fmt = new TwitterCldr.TimespanFormatter();
then = Math.round(new Date(2012, 1, 1, 12, 0, 0).getTime() / 1000);
now = Math.round(Date.now() / 1000);

format(then - now);                    // "6 months ago"
format(then - now, {unit: "week"});    // "24 weeks ago"
format(then - now, {unit: "year"});    // "0 years ago"
format(then + now, {unit: "week"});    // "In 24 weeks"
format(then + now, {unit: "year"});    // "In 0 years"

The TimespanFormatter can also handle time spans without a direction via the direction: "none" option. Directionless timespans can be combined with the type option:

format(180, {direction: "none", type: "short"});                 // "3 mins"
format(180, {direction: "none", type: "abbreviated"});           // "3m"
format(180, {direction: "none", type: "short", unit: "second"}); // "180 secs"

By default, timespans are exact representations of a given unit of elapsed time. TwitterCLDR also supports approximate timespans which round up to the nearest larger unit. For example, “44 seconds” remains “44 seconds” while “45 seconds” becomes “1 minute”. To approximate, pass the approximate: true option:

format(44, {approximate: true});  // Dentro de 44 segundos
format(45, {approximate: true});  // Dentro de 1 minuto
format(52, {approximate: true});  // Dentro de 1 minuto
Numbers

twitter-cldr-js number formatting supports decimals, currencies, and percentages.

Decimals
nclude the es data bundle for the Spanish NumberFormatter
fmt = new TwitterCldr.DecimalFormatter();
format(1337);                      // "1.337"
format(-1337);                     // "-1.337"
format(1337, {precision: 2});      // "1.337,00"
Short / Long Decimals

In addition to formatting regular decimals, TwitterCLDR supports short and long decimals. Short decimals abbreviate the notation for the appropriate power of ten, for example “1M” for 1,000,000 or “2K” for 2,000. Long decimals include the full notation, for example “1 million” or “2 thousand”:

fmt = new TwitterCldr.ShortDecimalFormatter();
format(2337);     // 2K
format(1337123);  // 1M

= new TwitterCldr.LongDecimalFormatter();
format(2337);     // 2 thousand
format(1337123);  // 1 million
Currencies
fmt = new TwitterCldr.CurrencyFormatter();
format(1337, {currency: "EUR"});                 // 1.337,00 ?
Percentages
fmt = new TwitterCldr.PercentFormatter();
format(1337);                      // 1.337%
format(1337, {precision: 2});      // 1.337,00%
More on Currencies

If you're looking for a list of supported currencies, use the Currencies function:

l supported currency codes
terCldr.Currencies.currency_codes()             # ["ADP", "AED", "AFA", "AFN", ... ]

ta for a specific currency code
terCldr.Currencies.for_code("CAD")            # {currency: "CAD", name: "Canadian dollar", cldr_symbol: "CA$", symbol: "$", code_points: [36]}
Plural Rules

Some languages, like English, have “countable” nouns. You probably know this concept better as “plural” and “singular”, i.e. the difference between “strawberry” and “strawberries”. Other languages, like Russian, have three plural forms: one (numbers ending in 1), few (numbers ending in 2, 3, or 4), and many (everything else). Still other languages like Japanese don't use countable nouns at all.

TwitterCLDR makes it easy to find the plural rules for any numeric value:

nclude the ru data bundle for access to Russian Plural rules
terCldr.PluralRules.rule_for(1);      // "one"
terCldr.PluralRules.rule_for(2);      // "few"
terCldr.PluralRules.rule_for(8);      // "many"

Get all the rules for your language:

terCldr.PluralRules.all();            // ["one", "few", "many", "other"]
Rule Based Number Formatting

The available rule-based number formats defined by the CLDR data set vary by language. Some languages support ordinal and cardinal numbers, occasionally with an additional masculine/feminine option, while others do not. You'll need to consult the list of available formats for your language.

Rule-based number formats are categorized by groups, and within groups by rulesets. You'll need to specify both to make use of all the available formats for your language.

To get a list of supported groups for the current locale, use the group_names method:

nclude the en data bundle for the English RBNF Formatter
formatter = new TwitterCldr.RBNF()
atter.group_names()

To get a list of supported rulesets for a group name, use the rule_set_names_for_group method:

atter.rule_set_names_for_group('SpelloutRules')
 '2d-year', 'spellout-numbering-year', 'spellout-numbering', ..., 'spellout-ordinal-verbose' ]

atter.rule_set_names_for_group('OrdinalRules')
 'digits-ordinal' ]

Once you've chosen a group and ruleset, you can pass them to the format method:

atter.format(123, 'OrdinalRules', 'digits-ordinal')
123rd'

In comparision, here is what the Spanish formatting looks like

nclude the es data bundle for the Spanish RBNF Formatter
formatter = new TwitterCldr.RBNF()
at.format(123, 'OrdinalRules', 'digits-ordinal-masculine') // '123º'
at.format(123, 'OrdinalRules', 'digits-ordinal-feminine')  // '123ª'

For languages that have support for SpelloutRules, like English (and other languages), you can also specify an ordinal spellout:

atter.format(1024, "SpelloutRules", "spellout-ordinal")
one thousand twenty-fourth'
Handling Bidirectional Text

When it comes to displaying text written in both right-to-left (RTL) and left-to-right (LTR) languages, most display systems run into problems. The trouble is that Arabic or Hebrew text and English text (for example) often get scrambled visually and are therefore difficult to read. It's not usually the basic ASCII characters like A-Z that get scrambled - it's most often punctuation marks and the like that are confusingly mixed up (they are considered “weak” types by Unicode).

To mitigate this problem, Unicode supports special invisible characters that force visual reordering so that mixed RTL and LTR (called “bidirectional”) text renders naturally on the screen. The Unicode Consortium has developed an algorithm (The Unicode Bidirectional Algorithm, or UBA) that intelligently inserts these control characters where appropriate. You can make use of the UBA implementation in TwitterCLDR by creating a new instance of TwitterCldr.Bidi via the from_string method, and manipulating it like so:

bidi_str = TwitterCldr.Bidi.from_string("hello ???? world", {"direction": "RTL"});
_str.reorder_visually();
_str.toString();

Disclaimer: Google Translate tells me the Arabic in the example above means “fancy”, but my confidence is not very high, especially since all the letters are unattached. Apologies to any native speakers :)

Postal Codes

The CLDR contains postal code validation regexes for a number of countries.

nited States
terCldr.PostalCodes.is_valid("us", "94103");     // true
terCldr.PostalCodes.is_valid("us", "9410");      // false

ngland (Great Britain)
terCldr.PostalCodes.is_valid("gb", "BS98 1TL");  // true

weden
terCldr.PostalCodes.is_valid("se", "280 12");    // true

anada
terCldr.PostalCodes.is_valid("ca", "V3H 1Z7");   // true

Get a list of supported territories by using the territories method:

terCldr.PostalCodes.territories();  // ["ad", "am", "ar", "as", "at", ... ]

Just want the regex? No problem:

terCldr.PostalCodes.regex_for_territory("us");  // /\d{5}([ \-]\d{4})?/
Phone Codes

Look up phone codes by territory:

nited States
terCldr.PhoneCodes.code_for_territory("us");  // "1"

erú
terCldr.PhoneCodes.code_for_territory("pe");  // "51"

gypt
terCldr.PhoneCodes.code_for_territory("eg");  // "20"

enmark
terCldr.PhoneCodes.code_for_territory("dk"); // "45"

Get a list of supported territories by using the territories method:

terCldr.PhoneCodes.territories();  // ["zw", "an", "tr", "by", "mh", ...]
Territories Containment

Determine if a territory/region contains another region or a country (as describe here):

terCldr.TerritoriesContainment.children('151') // ["BG", "BY", "CZ", "HU", "MD", "PL", ...]
terCldr.TerritoriesContainment.children('RU')  // []

terCldr.TerritoriesContainment.parents('013') // ["419", "003", "019"]
terCldr.TerritoriesContainment.parents('001') // []

terCldr.TerritoriesContainment.contains('151', 'RU') // true
terCldr.TerritoriesContainment.contains('419', 'BZ') // true
terCldr.TerritoriesContainment.contains('419', 'FR') // false
Unicode Regular Expressions

Unicode regular expressions are an implmentaion of regular expressions that support all Unicode characters in the BMP. They provide support for multi-character strings, Unicode character escapes, set operations (unions, intersections, and differences), and character sets.

Changes to Character Classes

Here's a complete list of the operations you can do inside a Unicode regex's character class.

| Regex | Description | |:——————-|:——————————————————————————————————————–| |[a] | The set containing 'a'. | |[a-z] | The set containing 'a' through 'z' and all letters in between, in Unicode order. | |[^a-z] | The set containing all characters except 'a' through 'z', that is, U+0000 through 'a'-1 and 'z'+1 through U+10FFFF. | |[[pat1][pat2]] | The union of sets specified by pat1 and pat2. | |[[pat1]&[pat2]] | The intersection of sets specified by pat1 and pat2. | |[[pat1]-[pat2]] | The symmetric difference of sets specified by pat1 and pat2. | |[:Lu:] or \p{Lu} | The set of characters having the specified Unicode property; in this case, Unicode uppercase letters. | |[:^Lu:] or \P{Lu} | The set of characters not having the given Unicode property. |

For a description of available Unicode properties, see Wikipedia (click on “[show]“).

Using Unicode Regexes

Create Unicode regular expressions via the compile method:

x = TwitterCldr.UnicodeRegex.compile("[:Lu:]+");
x2 = TwitterCldr.UnicodeRegex.compile("\\p{Lu}+", "g");
                        //escaping the '\'
x3 = TwitterCldr.UnicodeRegex.compile("[[a-z]-[d-g]]+", "g");
                        //supports the JavaScript RegExp modifiers

Once compiled, instances of UnicodeRegex behave just like normal javascript regexes and support the match method:

x.match("ABC");  // ["ABC"]
x2.match("ABCDfooABC");  // ["ABCD", "ABC"]
x3.match("dog"); // ["o"]

Protip: Try to avoid negation in character classes (eg. [^abc] and \P{Lu}) as it tends to negatively affect both performance when constructing regexes as well as matching.

Text Segmentation

TwitterCLDR currently supports text segmentation by sentence as described in the Unicode Technical Report #29. The segmentation algorithm makes use of Unicode regular expressions (described above). Segmentation by word, line, and grapheme boundaries could also be supported if someone wants them.

Text segmentation is performed by the BreakIterator class (name borrowed from ICU). You can use the each_sentence method segment by sentence.

ator = new TwitterCldr.BreakIterator("en");
ator.each_sentence("The. Quick. Brown. Fox.");
                    // "The.", " Quick.", " Brown.", " Fox."

To improve segmentation accuracy, a list of special segmentation exceptions have been created by the ULI (Unicode Interoperability Technical Committee). They help with special cases like the abbreviations “Mr.” and “Ms.” where breaks should not occur. ULI rules are enabled by default, but you can disable them via the use_uli_exceptions option:

ator = new TwitterCldr.BreakIterator ("en",
                    {"use_uli_exceptions" : false}
                );
ator.each_sentence("I like Ms. Murphy, she's nice.");
                    // ["I like Ms.", " Murphy, she's nice."]
Generating the JavaScript

The JavaScript files that make up twitter-cldr-js can be automatically generated for each language via a set of Rake tasks.

Requirements

twitter-cldr-js requires Rails 3.1 or later. To run the JavaScript test suite, you'll need Node and the jasmine-node NPM package.

Running Tests
  1. Install node (eg. brew install node, sudo apt-get install node, etc)
  2. Install jasmine-node: npm install jasmine-node -g
  3. Run bundle install
  4. Run bundle exec rake
Authors
Links
License

Copyright 2015 Twitter, Inc.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.