Wednesday, August 15, 2012

Find: TwitterCLDR - Improving Internationalization Support in Ruby

TwitterCLDR: Improving Internationalization Support in Ruby

We recently open sourced TwitterCLDR under the Apache Public License 2.0. TwitterCLDR is an “ICU level” internationalization library for Ruby that supports dates, times, numbers, currencies, world languages, sorting, text normalization, time spans, plurals, and unicode code point data. By sharing our code with the community we hope to collaborate together and improve internationalization support for websites all over the world. If your company is considering supporting multiple languages, then you can try TwitterCLDR to help your internationalization efforts.

Motivation

Here’s a test. Say this date out loud: 2/1/2012
If you said, “February first, 2012”, you’re probably an American. If you said, “January second, 2012”, you’re probably of European or possibly Asian descent. If you said, “January 12, 1902”, you’re probably a computer. The point is that as humans, we almost never think about formatting dates, plurals, lists, and the like. If you’re creating a platform available around the world, however, these kinds of minutiae make a big difference to users.
The Unicode Consortium publishes and maintains a bunch of data regarding formatting dates, numbers, lists, and more, called the Common Locale Data Repository (CLDR). IBM maintains International Components for Unicode (ICU), a library that uses the Unicode Consortium’s data to make it easier for programmers to use. However, this library is targeted at Java and C/C++ developers and not Ruby programmers, which is one of the programming languages used at Twitter. For example, Ruby and TwitterCLDR helps power our Translation Center. TwitterCLDR provides a way to use the same CLDR data that Java uses, but in a Ruby environment. Hence, formatting dates, times, numbers, currencies and plurals should now be much easier for the typical Rubyist. Let’s go over some real world examples.


Example Code

Dates, Numbers, and Currencies
Let’s format a date in Spanish (es):
$> DateTime.now.localize(:es).to_full_s $> "lunes, 12 de diciembre de 2011 21:44:57 UTC -0800"
Too long? Make it shorter:
$> DateTime.now.localize(:es).to_short_s$> "12/12/11 21:44"
Built in support for relative times lets you do this:
$> (DateTime.now - 1).localize(:en).ago.to_s$> "1 day ago"$> (DateTime.now + 1).localize(:en).until.to_s$> "In 1 day"
Number formatting is easy:
$> 1337.localize(:en).to_s$> "1,337"$> 1337.localize(:fr).to_s$> "1 337"
We’ve got you cove...



No comments:

Post a Comment