SlideShare une entreprise Scribd logo
1  sur  103
Télécharger pour lire hors ligne
Internationalize your JavaScript
Application: Prepare for "the next
billion" internet users.
Kevin Hakanson
(2 November 2013)
Abstract
Are you prepared for "the next billion" internet users, most
of whom don't use English as their primary language?
We will explore the globalization (internationalization and
localization) of JavaScript based applications. It will look at
the ECMAScript Internationalization API and popular open
source projects like AngularJS, messageformat.js, jQuery
Globalize and twitter-cldr-js.
Topics will include cultures/locales, character encoding,
number formatting, date formatting, choice/plural formatting
and translations.
Kevin Hakanson
@hakanson
#ICC12

kevin.hakanson@gmail.com

github.com/hakanson

stackoverflow.com/users/22514/kevin-hakanson
Bio
Kevin Hakanson is an application architect for Thomson
Reuters where he is focused on highly scalable web
applications, especially the JavaScript and security
aspects. His background includes both .NET and Java, but
he is most nostalgic about Lotus Notes. He has been
developing professionally since 1994 and holds a Master’s
degree in Software Engineering. When not staring at a
computer screen, he is probably staring at another screen,
either watching TV or playing video games with his family.
Internationalization
Internationalization of software means
designing it such that it supports or can
be easily adapted to support the needs
of users speaking different languages
and having different cultural
expectations, and enables worldwide
communication between them.
Localization
Localization then is the actual
adaptation to a specific language and
culture.
Globalization
Globalization of software is commonly
understood to be the combination of
internationalization and localization.
http://upload.wikimedia.org/wikipedia/commons/e/e3/Globalisationchart.jpg
I18N & L10N
The terms are frequently abbreviated to the
numeronyms i18n (where 18 stands for the
number of letters between the first i and last n
in internationalization) and l10n respectively,
due to the length of the words.
Other “correct” spellings are internationalisation
and localisation.
http://en.wikipedia.org/wiki/Internationalization_and_localization
K3n H6n
Kevin Hakanson
ECMAScript Internationalization API
The ECMAScript Internationalization API
provides key language-sensitive functionality as
a complement to the ECMAScript Language
Specification, 5.1 edition or successor.

Standard ECMA-402
1st Edition / December 2012
http://www.ecma-international.org/ecma-402/1.0/
ECMA-402
Its functionality has been selected from that of
well-established internationalization APIs such
as those of the:
● Internationalization Components for
Unicode (ICU) library
● .NET framework
● Java platform
ICU - International Components
for Unicode http://site.icu-project.org/
ICU is a mature, widely used set of C/C++ and Java
libraries providing Unicode and Globalization support for
software applications. ICU is widely portable and gives
applications the same results on all platforms and between
C/C++ and Java software.
ICU is released under a nonrestrictive open source license
that is suitable for use with both commercial software and
with other open source or free software.
Used by both Google Chrome and Mozilla Firefox.
ECMA-402
Three key pieces of language-sensitive functionality:
○ String comparison (collation)
String.prototype.localeCompare
○ Number formatting
Number.prototype.toLocaleString
○ Date and time formatting
Date.prototype.toLocaleString
Date.prototype.toLocaleDateString
Date.prototype.toLocaleTimeString
ECMA-402 Language Tags
Identifies locales using language tags as
defined by IETF BCP 47 (RFCs 5646 and 4647
or their successors), which may include
extensions such as those registered through
RFC 6067. Their canonical form is specified in
RFC 5646 section 4.5 or its successor.
(Note: RFC 5646 uses ISO 639 codes for language)
Example Language Tags
en-US
English (United States)
es-MX
Español (México)
fr-CA
français (Canada)
>

var locales = ["en-US", "es-MX", "fr-CA"]

>

Intl.DateTimeFormat.supportedLocalesOf(locales)
["en-US", "es-MX", "fr-CA"]
ECMA-402 Browser Support
Google Chrome
Firefox Nightly
Bug 853301 - Enable ECMAScript Internationalization API for desktop Firefox
http://nightly.mozilla.org/

Internet Explorer 11
Part of Windows 8.1
Update to Windows 7 or Windows Server 2008
http://windows.microsoft.com/en-us/internet-explorer/ie-11-release-preview
http://ie.microsoft.com/testdrive/Info/Downloads/Default.html
ECMAScript Internationalization test402
http://test262.ecmascript.org/testcases_intl402.html#

Google Chrome (Version 30.0.1599.101)

Internet Explorer (Version 11.0.9600.16384)

Firefox Nightly 27.0a1 (2013-10-16)
ECMA-402 Blog Posts
Norbert Lindenberg (Dec 2012)
http://norbertlindenberg.com/2012/12/ecmascriptinternationalization-api/
David Storey (Aug 2013)
http://generatedcontent.org/post/59403168016/esintlapi
Dr. Alex Rauschmayer (Sep 2013)
http://www.2ality.com/2013/09/ecmascript-i18n-api.html
EMCA-402 Polyfill/Backport?
Compatibility implementation of the ECMAScript
Internationalization API (ECMA-402) for JavaScript
https://github.com/andyearnshaw/Intl.js

“Intl.js attempts to fill the void of availability for this API...
so that developers can take advantage of the native API
in environments that support it, or Intl.js for legacy or
unsupporting environments.”
“Intl.js is designed to be compatible with ECMAScript
3.1 environments in order to follow the specification as
closely as possible.”
jQuery Globalize
Used to be a jQuery plugin (jquery-global), now
a JavaScript library called Globalize.
http://wiki.jqueryui.com/w/page/39118647/Globalize

Enables complex culture-aware number and
date parsing and formatting, including the raw
culture information for hundreds of different
languages and countries, as well as an
extensible system for localization.
Globalize Cultures
Each culture is given a unique code that is a
combination of an ISO 639 two-letter lowercase
culture code for the language, and a two-letter
uppercase code for the country or region.
For example, "en-US" is the culture code for English in
the United States.

"Neutral" cultures based on accepted set of
rules by anyone speaking that language.
For example, "es" is the neutral culture for Spanish.
Globalize API
Globalize.addCultureInfo( cultureName,
extendCultureName, info )
Globalize.cultures
Globalize.culture( selector )
Globalize.findClosestCulture( selector )
Globalize.format( value, format, culture )
Globalize.localize( key, culture )
Globalize.parseInt( value, radix, culture )
Globalize.parseFloat( value, radix, culture )
Globalize.parseDate( value, formats, culture )
Globalize Culture Examples
>

Globalize.culture("en-US")
Object {name: "en-US", englishName: "English (United States)",
nativeName: "English", isRTL: false, language: "en"…}

>

Globalize.culture("es-MX")
Object {name: "es-MX", englishName: "Spanish (Mexico)",
nativeName: "Español (México)", isRTL: false, language: "es"…}

>

Globalize.culture("fr-CA")
Object {name: "fr-CA", englishName: "French (Canada)",
nativeName: "français (Canada)", isRTL: false, language: "fr"…}
HTML is great for declaring static documents,
but it falters when we try to use it for declaring
dynamic views in web-applications. AngularJS
lets you extend HTML vocabulary for your
application. The resulting environment is
extraordinarily expressive, readable, and quick
to develop.
http://www.angularjs.org/
AngularJS I18N/L10N Support
Supports i18n/l10n for datetime, number and
currency filters.
Localizable pluralization support provided by
the ngPluralize directive.
All localizable components depend on localespecific rule sets managed by the $locale
service.
http://docs.angularjs.org/guide/i18n
Extending AngularJS
i18n for your Angular apps, made easy
http://pascalprecht.github.io/angular-translate/#/guide

Internazionalization (i18n) with AngularJS
http://blog.brunoscopelliti.com/internazionalization-i18n-with-angularjs

Localizing Your AngularJS App
http://codingsmackdown.tv/blog/2012/12/14/localizing-your-angularjs-app/

Angular i18n library wrapping Jed (gettext for js)
https://github.com/ErikAndreas/lingua
messageformat.js
ICU MessageFormat for Javascript - i18n Plural
and Gender Capable Messages
● Implements SelectFormat and PluralFormat
● Plan to pull in locale-aware NumberFormat
parsing as a "plugin"
https://github.com/SlexAxton/messageformat.js
Example Formatted Message
{GENDER, select,
male {He has}
female {She has}
other {They have}
} {NUM_FRIENDS, plural,
=0 {no friends}
one {1 friend}
other {# friends}
}
Example Usage
>

var mf = new MessageFormat('en')

>

var messageTemplate = "{...}" // see previous slide

>

var message = mf.compile(messageTemplate)

>

message({ "GENDER" : "male" , "NUM_FRIENDS" : 0 })
"He has no friends."

>

message({ "GENDER" : "female" , "NUM_FRIENDS" : 1 })
"She has 1 friend."

>

message({ "GENDER" : "other" , "NUM_FRIENDS" : 2 })
"They have 2 friends."
messageformat.js
Alex Sexton: Client Side Internationalization
https://www.youtube.com/watch?v=uXS_-JRsB8M
CLDR - Unicode Common Locale
Data Repository http://cldr.unicode.org/
The Unicode CLDR provides key building
blocks for software to support the world's
languages, with the largest and most extensive
standard repository of locale data available.
This data is used by a wide spectrum of
companies for their software
internationalization and localization, adapting
software to the conventions of different
languages for such common software tasks.
CLDR Includes:
Locale-specific patterns for formatting and parsing: dates, times,
timezones, numbers and currency values
Translations of names: languages, scripts, countries and regions,
currencies, eras, months, weekdays, day periods, timezones, cities,
and time units
Language & script information: characters used; plural cases;
gender of lists; capitalization; rules for sorting & searching; writing
direction; transliteration rules; rules for spelling out numbers; rules for
segmenting text into graphemes, words, and sentences
Country information: language usage, currency information, calendar
preference and week conventions, postal and telephone codes
Other: ISO & BCP 47 code support (cross mappings, etc.), keyboard
layouts
twitter-cldr-js
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. Currently, supports the following:
●
●
●
●
●
●

Date and time formatting
Relative date and time formatting (eg. 1 month ago)
Number formatting (decimal, currency, and percentage)
Long/short decimals
Plural rules
Bidirectional reordering

https://github.com/twitter/twitter-cldr-js
Cultures / Locales
http://www.jsonline.com/entertainment/dining/15-beer-and-wine-facts-that-may-surprise-you-b99117052z1-227119841.html
EMCA-402
Implementation dependent
David Storey’s Appendix A includes locale
support tables for each browser that supports
the Internationalization API
http://generatedcontent.org/post/59403168016/esintlapi
Globalize (~353)
// globalize.js
Globalize.cultures[ "default" ] = {...};
days: {
// full day names
names: [ "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" ],

// globalize.cultures.es-MX.js
Globalize.addCultureInfo( "es-MX", "default", {...});
days: {
names: ["domingo", "lunes", "martes", "miércoles", "jueves",
"viernes", "sábado"],
AngularJS "en-US" $locale
Bundled into angular.js
angularModule('ngLocale', []).provider('$locale', $LocaleProvider);
function $LocaleProvider(){
this.$get = function() {
return { id: 'en-us', /* … */ };
};
}

In separate angular-locale_en-US.js
angular.module("ngLocale", [], ["$provide", function($provide) {
$provide.value("$locale", {
"id": "en-us", /* … */
});
AngularJS (~287)
angular-locale_en-us.js

angular-locale_es-mx.js

"DAY": {
"0": "Sunday",
"1": "Monday",
"2": "Tuesday",
"3": "Wednesday",
"4": "Thursday",
"5": "Friday",
"6": "Saturday"
},

"DAY": {
"0": "domingo",
"1": "lunes",
"2": "martes",
"3": "miu00e9rcoles",
"4": "jueves",
"5": "viernes",
"6": "su00e1bado"
},
AngularJS locale.js File Encoding
Bug Fix from 1.0.7 monochromatic-rainbow
(2013-05-22)
i18n: escape all chars above u007f in locale files
(695c54c1, #2417)
https://github.com/angular/angular.js/blob/master/CHANGELOG.md

Example: src/ngLocale/angular-locale_es-mx.js
-

"3": "miércoles",

+

"3": "miu00e9rcoles",
Globalize Culture Examples (redux)
>

Globalize.culture("en-US")
Object {name: "en-US", englishName: "English (United States)",
nativeName: "English", isRTL: false, language: "en"…}

>

Globalize.culture("es-MX")
Object {name: "es-MX", englishName: "Spanish (Mexico)",
nativeName: "Español (México)", isRTL: false, language: "es"…}

>

Globalize.culture("fr-CA")
Object {name: "fr-CA", englishName: "French (Canada)",
nativeName: "français (Canada)", isRTL: false, language: "fr"…}
Globalize File Encoding (UTF-8)
WebStorm integrated web server without
<meta charset='utf-8'/> in demo.html
http://localhost:63342/jsi18n/demo.html#/
>

Globalize.culture("es-MX")
Object {name: "es-MX", englishName: "Spanish (Mexico)",
nativeName: "Español (México)", isRTL: false, language: "es"
…}

>

Globalize.culture("fr-CA")
Object {name: "fr-CA", englishName: "French (Canada)",
nativeName: "français (Canada)", isRTL: false, language: "fr"…}
X-Powered-By: Express
var express = require('express');
var app = express();
app.configure(function(){
app.use(function(req, res, next) {
if (/.*.js/.test(req.path)) {
res.charset = "utf-8";
// Content-Type: application/javascript; charset=utf-8
}
next();
});
app.use(express.static('../jsi18n'));
});
app.listen(1337); // http://localhost:1337/demo.html#/
More Options
Convert source code to ASCII (7 bit)
Before: var σ = 'Köln';
> uglifyjs -b beautify=false,ascii-only=true file.js

After:

var u03c3="Kxf6ln";

Load source with charset attribute on script tag
var π = Math.PI, ε = 1e-6;
<script charset="utf-8" src="d3.js"></script>
http://www.2ality.com/2013/09/javascript-unicode.html
é (small e, acute accent)
HTML Entity
&eacute;
&#xE9;
&#233;

JavaScript
"u00E9"
"xE9"
UTF-8
é
C3 A9

URL
%e9

%c3%a9
ñ (small n, tilde)
HTML Entity
&ntilde;
&#xF1;
&#241;

JavaScript
"u00F1"
"xF1"
UTF-8
ñ
C3 B1

URL
%f1

%c3%b1
ç (small c, cedilla)
HTML Entity
&ccedil;
&#xE7;
&#231;

JavaScript
"u00E7"
"xE7"
UTF-8
ç
C3 A7

URL
%e7

%c3%a7
>

div = document.createElement("div")
<div>​</div>

>

div.innerHTML = "&ntilde;&eacute;&ccedil;"
"&ntilde;&eacute;&ccedil;"
>
div.innerText
"ñéç"
>
div.innerHTML
"ñéç"
>
div.innerHTML = "&ntilde;&eacute;&ccedil; &#xF1;&#xE9;&#xE7; &#241;
&#233;&#231;"
"&ntilde;&eacute;&ccedil; &#xF1;&#xE9;&#xE7; &#241;&#233;&#231;"
>
div.innerHTML
"ñéç ñéç ñéç"
>

div.innerText = "u00F1u00E9u00E7 xF1xE9xE7"
"ñéç ñéç"

>

encodeURIComponent("ñéç")
"%C3%B1%C3%A9%C3%A7"
escape("ñéç")
"%F1%E9%E7"

>
Mac Character Viewer
System Preferences - Keyboard
☑Show Keyboard & Character Viewers in menu bar
Windows Character Map
Start - Run - charmap.exe
JavaScript has a Unicode problem
> "mañana" == "mañana"
false
> "maxF1ana" == "manu0303ana"
false
The first string contains U+00F1 LATIN SMALL LETTER N
WITH TILDE, while the second string uses two separate
code points (U+006E LATIN SMALL LETTER N and
U+0303 COMBINING TILDE) to create the same glyph.
http://mathiasbynens.be/notes/javascript-unicode
DEMO
Dynamically Changing
Culture / Locale
ECMA-402
No settable default locale list:
● global communication channel (security risk)
● application may require multiple components
and contexts; higher-level concern of
application/library vs ECMAScript
Every API takes a locale list as parameter
Globalize
Include culture files
<script src="globalize/globalize.js"></script>
<script src="globalize/cultures/globalize.culture.en-US.js"></script>
<script src="globalize/cultures/globalize.culture.es-MX.js"></script>
<script src="globalize/cultures/globalize.culture.fr-CA.js"></script>

Call function to set default
Globalize.culture("en-US")
AngularJS $locale Binding
AngularJS binds value of $locale and doesn’t
allow updates to locale or including multiple
locale <script> files.
<script src="angular/angular.js"></script>
<script> // http://stackoverflow.com/questions/13007430/angularjs-and-locale
(function () {
var locale = localStorage.getItem('$locale');
if (locale) {
document.write('<script src="angular/i18n/angular-locale_' +
locale.toLowerCase() + '.js"></script>');
}
})();
</script>
AngularJS Number Filter
Number filter captures value of $locale.
NUMBER_FORMATS inside closure.
https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js

numberFilter.$inject = ['$locale'];
function numberFilter($locale) {
var formats = $locale.NUMBER_FORMATS;
return function(number, fractionSize) {
return formatNumber(number, formats.PATTERNS[0], formats.GROUP_SEP, formats.DECIMAL_SEP,
fractionSize);
};
}
Number / Currency
Formatting
“ONE MILLION DOLLARS”
Decimal Mark
A symbol used to separate the integer part from the
fractional part of a number written in decimal form.
Different cultures use different symbols for the decimal
mark. The choice of symbol for the decimal mark also
affects the choice of symbol for the thousands separator
used in digit grouping.
Full Stop "." – Blue
Comma "," – Green
Eastern Arabic numerals – Red
Data unavailable – Grey
http://en.wikipedia.org/wiki/Decimal_mark
Globalize
Four main types of number formatting:
●
●
●
●

n for number
d for decimal digits
p for percentage
c for currency

Each format token may also be followed by a
number determining how many decimal places
to display (with d determines the minimum
number of digits to display, zero padded)
>
>
>

>
>
>

>
>

Globalize.format(12345.6789, "n")
"12,345.68"
Globalize.format(12345.6789, "n4")
"12,345.6789"
Globalize.format(12345.6789, "n9")
"12,345.678900000"
Globalize.format(12345.6789, "d")
"12345"
Globalize.format(12345.6789, "d4")
"12345"
Globalize.format(12345.6789, "d9")
"000012345"
Globalize.format(12345.6789, "n", "es-MX")
"12,345.68"
Globalize.format(12345.6789, "n", "fr-CA")
"12 345,68"
EMCA-402
Three format styles for NumberFormat objects
and Number.prototype.toLocaleString:
decimal
currency
percent

Number of digits used to represent a number
can be constrained
Grouping separators can be disabled
ECMA-402 Components of Number
and Currency Formats
Property

Values

style

"decimal", "currency", "percent"

currency

string

currencyDisplay

"code", "symbol", "name"

minimumIntegerDigits

non-negative integer

minimumFractionDigits

non-negative integer

maximumFractionDigits

non-negative integer

minimumSignificantDigits

positive integer

maximumSignificantDigits positive integer
useGrouping

boolean
>
>
>

>

>

>
>

num = 12345.6789
12345.6789
num.toLocaleString("en-us")
"12,345.679"
num.toLocaleString("en-us", { useGrouping: true,
minimumSignificantDigits: 4})
"12,345.6789"
num.toLocaleString("en-US", { useGrouping: false,
maximumFractionDigits: 0})
"12346"
num.toLocaleString("en-US", { useGrouping: false,
maximumFractionDigits: 0, minimumIntegerDigits: 9})
"000012346"
num.toLocaleString("es-MX")
"12,345.679"
num.toLocaleString("fr-CA")
"12 345,679"
Chromium Issue 304722
Intl NumberFormat minimumFractionDigits options property
not honored
Reported by kevin.ha...@gmail.com, Oct 6
Steps to reproduce the problem:
(12345.6789).toLocaleString("en-us",{ useGrouping: false, minimumFractionDigits:
4})
"12345.679"

What is the expected behavior?
"12345.6789"
https://code.google.com/p/chromium/issues/detail?id=304722
twitter-cldr-js
Number formatting supports decimals,
currencies, and percentages.
In addition to formatting regular decimals,
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".
>

num = 12345.6789
12345.6789

fmt = new TwitterCldr.DecimalFormatter()
DecimalFormatter {all_tokens: Object, tokens: Array[0],
symbols: Object, default_symbols: Object, constructor: function…}
> fmt.format(num)
"12,345.6789"
> fmt.format(num, {precision: 2})
"12,345.68"
>

fmt = new TwitterCldr.CurrencyFormatter();
CurrencyFormatter {default_currency_symbol: "$",
default_precision: 2, all_tokens: Object, tokens: Array[0], symbols:
Object…}
> fmt.format(num, {currency: "USD"})
"$12,345.6789"
> fmt.format(num, {currency: "USD", precision: 2})
"$12,345.68"
>
DEMO
Date Formatting
International Dates, not Dating
ECMA-402 Components of Date and
Time Formats
Property

Values

weekday

"narrow", "short", "long"

era

"narrow", "short", "long"

year

"2-digit", "numeric"

month

"2-digit", "numeric", "narrow",
"short", "long"

day

"2-digit", "numeric"

hour

"2-digit", "numeric"

minute

"2-digit", "numeric"

second

"2-digit", "numeric"

timeZoneName

"short", "long"
Globalize Standard Date Formats
Format

Meaning

"en-US"

new Date (2013,0,1,13,1,1)

f

Long Date, Short dddd, MMMM dd, yyyy h:mm tt
Time

Tuesday, January 01, 2013 1:01 PM

F

Long Date, Long
Time

dddd, MMMM dd, yyyy h:mm:ss tt

Tuesday, January 01, 2013 1:01:01
PM

t

Short Time

h:mm tt

1:01 PM

T

Long Time

h:mm:ss tt

1:01:01 PM

d

Short Date

M/d/yyyy

1/1/2013

D

Long Date

dddd, MMMM dd, yyyy

Tuesday, January 01, 2013

Y

Month/Year

MMMM, yyyy

January 2013 *

M

Month/Day

MMMM dd

January 01

* https://github.com/jquery/globalize/issues/97
AngularJS Predefined Date Formats
Format

"en-US"

new Date (2013,0,1,13,1,1)

medium

MMM d, y h:mm:ss a

Jan 1, 2013 1:01:01 PM

short

M/d/yy h:mm a

1/1/13 1:01 PM

fullDate

EEEE, MMMM d,y

Tuesday, January 1, 2013

longDate

MMM d, y

January 1, 2013

mediumDate

MMM d, y

Jan 1, 2013

shortDate

M/d/yy

1/1/13

mediumTime

h:mm:ss a

1:01:01 PM

shortTime

h:mm a

1:01 PM
Date/Time Format Patterns
jQuery Globalize

AngularJS

// short date pattern

"fullDate": "EEEE, MMMM d, y",

d: "M/d/yyyy",

"longDate": "MMMM d, y",

// long date pattern

"medium": "MMM d, y h:mm:ss a",

D: "dddd, MMMM dd, yyyy",

"mediumDate": "MMM d, y",

// short time pattern

"mediumTime": "h:mm:ss a",

t: "h:mm tt",

"short": "M/d/yy h:mm a",

// long time pattern

"shortDate": "M/d/yy",

T: "h:mm:ss tt",

"shortTime": "h:mm a"

// long date, short time pattern
f: "dddd, MMMM dd, yyyy h:mm tt",
// long date, long time pattern
F: "dddd, MMMM dd, yyyy h:mm:ss tt",
// month/day pattern
M: "MMMM dd",
// month/year pattern
Y: "yyyy MMMM"
twitter-cldr-js
The default CLDR data set only includes 4 date
formats, full, long, medium, and short. See
below for a list of additional formats.
>

TwitterCldr.DateTimeFormatter.additional_formats()
["EHm", "EHms", "Ed", "Ehm", "Ehms", "Gy", "GyMMM",
"GyMMMEd", "GyMMMd", "H", "Hm", "Hms", "M", "MEd", "MMM",
"MMMEd", "MMMd", "Md", "d", "h", "hm", "hms", "ms", "y", "yM",
"yMEd", "yMMM", "yMMMEd", "yMMMd", "yMd", "yQQQ",
"yQQQQ"]
>

then = new Date (2013,0,1,13,1,1)
Tue Jan 01 2013 13:01:01 GMT-0600 (CST)

>

Globalize.format( then , 'f' )
"Tuesday, January 01, 2013 1:01 PM"

> then.toLocaleString("en-US", { weekday : "long", month : "long",
day : "2-digit", year : "numeric", hour : "numeric", minute : "2digit", hour12 : true } )
"Tuesday, January 01, 2013 1:01 PM"
fmt = new TwitterCldr.DateTimeFormatter()
DateTimeFormatter {tokens: Object, weekday_keys: Array[7],
methods: Object, format: function, get_tokens: function…}
> fmt.format(then, {"type": "full"})
"Tuesday, January 1, 2013 at 1:01:01 PM UTC-06:00"
>
>

then = new Date (2013,0,1,13,1,1)
Tue Jan 01 2013 13:01:01 GMT-0600 (CST)

>

Globalize.format( then , 't' )
"1:01 PM"

> then.toLocaleTimeString("en-US", { hour : "numeric", minute :
"2-digit", hour12 : true } )
"1:01 PM"
fmt = new TwitterCldr.DateTimeFormatter()
DateTimeFormatter {tokens: Object, weekday_keys: Array[7],
methods: Object, format: function, get_tokens: function…}
> fmt.format(then, {"format": "time", "type": "short"})
"1:01 PM"
>
Intl AngularJS Filter
Proof of concept AngularJS filter illustrating
ECMA-402 API. Takes two parameters:
arg0 - culture (optional)
arg1 - format (predefined string or Intl struct)

Sample usage:
{{ then | dateIntl:"en-US":"longDate" }}
{{ then | dateIntl:"es-MX":"longDate" }}
{{ then | dateIntl:"longDate" }}
{{ then | dateIntl:{ hour : "numeric", minute
: "2-digit", hour12 : true } }}
var IntlFilters = angular.module('Intl.filters', []);
IntlFilters.filter('dateIntl', ['$locale', function($locale) {
return function(date, arg0, arg1) {
var culture = ( arg1 ? arg0 : $locale.id );
var format = (arg1 ? arg1 : arg0 );
var s = date.toISOString();
if (typeof format == "string") {
switch (format) {
case "medium" :
s = date.toLocaleString(culture, { month : "short", day :
"numeric", year : "numeric", hour : "numeric", minute : "2-digit", second : "2digit", hour12 : true });
break;
// case "longdate", "shorttime", ...
}
} else {
s = date.toLocaleString(culture, format);
}
return s;
};
}]);
DEMO
AngularUI Bootstrap
Datepicker (ui.bootstrap.
datepicker)
● A clean, flexible, and fully
customizable date picker.
● Everything is formatted
using the date filter and thus
is also localized.

Timepicker (ui.bootstrap.
timepicker)
Choice / Plural Formatting
In the field of memetics, a metameme (or meta-meme) is defined as a meme about a meme.
http://en.wikipedia.org/wiki/Metameme
messageformat.js (redux)
{GENDER, select,
male {He has}
female {She has}
other {They have}
} {NUM_FRIENDS, plural,
=0 {no friends}
one {1 friend}
other {# friends}
}
AngularJS: ngSwitch
Conditionally swap DOM structure on your
template based on a scope expression
Place an expression ng-switch="..." attribute

Choses one of the nested elements and makes
it visible based on which element matches the
value obtained from the evaluated expression
ng-switch-when="..." attribute is used to inform
ngSwitch which element to display
http://docs.angularjs.org/api/ng.directive:ngSwitch
AngularJS: ngPluralize
There are two plural categories in Angular's
default en-US locale: "one" and "other".
An explicit number rule can only match one number.

Configure ngPluralize by providing two
attributes: count and when.
count can be either a string or an Angular expression
when specifies the mappings between plural categories
and the actual string to be displayed
http://docs.angularjs.org/api/ng.directive:ngPluralize
AngularJS Formatted Message
<div>
<span ng-switch="salutationkey">
<span ng-switch-when="MR">He</span>
<span ng-switch-when="MRS">She</span>
<span ng-switch-when="MS">She</span>
</span>
has
<span ng-pluralize count="friends"
when="{'0': 'no friends.',
'one': '1 friend.',
'other': '{{friends|number}} friends.'}">
</span>
</div>
twitter-cldr-js (en)
TwitterCldr.PluralRules = (function() {
function PluralRules() {}
PluralRules.rules = {"keys": ["one","other"], "rule": function(n) {
return (function() { if (n == 1) { return "one" } else { return "other" } })();
}};
PluralRules.all = function() {
return this.rules.keys;
};
PluralRules.rule_for = function(number) {
var error;
try {
return this.rules.rule(number);
} catch (_error) {
error = _error;
return "other";
}
};
return PluralRules;
})();
Translations
http://untitledmagazine.net/category/international-cats/
AngularJS module that makes your life much
easier when it comes to i18n and l10n including
lazy loading and pluralization.
Provides components like filters and directives,
asynchronous loading of i18n data, full
pluralization support through MessageFormat
and much more!
http://pascalprecht.github.io/angular-translate/#/guide
angular translate Usage
Set Preferred
$translateProvider.preferredLanguage('en-US');

Update
$translate.uses('en-US');

Use with HTML Template Binding
{{ 'FAV_COLOR' | translate }}:

Use in JavaScript
$filter('translate')('FAV_COLOR');
$translateProvider.translations('en-US', {
FAV_COLOR : "Favorite Color",
BLUE : "Blue",
});

$translateProvider.translations('es-MX', {
FAV_COLOR : "Color Favorito",
BLUE : "Azul",
});

$translateProvider.translations('fr-CA', {
FAV_COLOR : "Couleur préférée",
BLUE : "Bleu",
});
Globalize Usage
Set Culture
Globalize.culture( "en-US" );

Localize
Globalize.localize( "FAV_COLOR" )

Localize for Culture
Globalize.localize( "FAV_COLOR", "en-US" )
Globalize.localize( "FAV_COLOR", "fr-CA" )
Globalize.addCultureInfo( "en-US", {
messages: {
"FAV_COLOR" : "Favorite Color",
"BLUE" : "Blue"
}
});
Globalize.addCultureInfo( "es-MX", {
messages: {
"FAV_COLOR" : "Color Favorito",
"BLUE" : "Azul"
}
});
Globalize.addCultureInfo( "fr-CA", {
messages: {
"FAV_COLOR" : "Couleur préférée",
"BLUE" : "Bleu"
}
});
DEMO
¿preguntas?
(questions?)

Contenu connexe

Tendances

JSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the WebJSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the WebKazuho Oku
 
PHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonPHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonHaim Michael
 
Css Founder.com | Cssfounder Net
Css Founder.com | Cssfounder NetCss Founder.com | Cssfounder Net
Css Founder.com | Cssfounder NetCss Founder
 
Osp ii presentation
Osp ii presentationOsp ii presentation
Osp ii presentationpresse_jkp
 
Comparison of Programming Platforms
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming PlatformsAnup Hariharan Nair
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalystdwm042
 
PHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and FrameworksPHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and FrameworksRoyston Olivera
 
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?Wong Hoi Sing Edison
 
Chowdhury webtech
Chowdhury webtechChowdhury webtech
Chowdhury webtechkaran saini
 
Chowdhury webtech
Chowdhury webtechChowdhury webtech
Chowdhury webtechArpit Meena
 
Livecode widget course
Livecode widget courseLivecode widget course
Livecode widget coursecrazyaxe
 
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothBhavsingh Maloth
 

Tendances (20)

JSX Optimizer
JSX OptimizerJSX Optimizer
JSX Optimizer
 
JSX
JSXJSX
JSX
 
JSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the WebJSX - developing a statically-typed programming language for the Web
JSX - developing a statically-typed programming language for the Web
 
PHP, Java EE & .NET Comparison
PHP, Java EE & .NET ComparisonPHP, Java EE & .NET Comparison
PHP, Java EE & .NET Comparison
 
Css Founder.com | Cssfounder Net
Css Founder.com | Cssfounder NetCss Founder.com | Cssfounder Net
Css Founder.com | Cssfounder Net
 
Osp ii presentation
Osp ii presentationOsp ii presentation
Osp ii presentation
 
Intro to J Ruby
Intro to J RubyIntro to J Ruby
Intro to J Ruby
 
Comparison of Programming Platforms
Comparison of Programming PlatformsComparison of Programming Platforms
Comparison of Programming Platforms
 
Practical catalyst
Practical catalystPractical catalyst
Practical catalyst
 
PHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and FrameworksPHP - History, Introduction, Summary, Extensions and Frameworks
PHP - History, Introduction, Summary, Extensions and Frameworks
 
Tech talk webtech
Tech talk webtechTech talk webtech
Tech talk webtech
 
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
[HKDUG] #20161210 - BarCamp Hong Kong 2016 - What's News in PHP?
 
Chowdhury webtech
Chowdhury webtechChowdhury webtech
Chowdhury webtech
 
Chowdhury webtech
Chowdhury webtechChowdhury webtech
Chowdhury webtech
 
Chowdhury webtech
Chowdhury webtechChowdhury webtech
Chowdhury webtech
 
Chowdhury webtech
Chowdhury webtechChowdhury webtech
Chowdhury webtech
 
C# tutorial
C# tutorialC# tutorial
C# tutorial
 
Livecode widget course
Livecode widget courseLivecode widget course
Livecode widget course
 
Web programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh MalothWeb programming UNIT II by Bhavsingh Maloth
Web programming UNIT II by Bhavsingh Maloth
 
PHP programmimg
PHP programmimgPHP programmimg
PHP programmimg
 

Similaire à Internationalize your JavaScript Application: Prepare for "the next billion" internet users.

Portable Code Compiler
Portable Code CompilerPortable Code Compiler
Portable Code Compilerijtsrd
 
Cross Platform Mobile Technologies
Cross Platform Mobile TechnologiesCross Platform Mobile Technologies
Cross Platform Mobile TechnologiesTalentica Software
 
Analysis Of The Original Version Of Java
Analysis Of The Original Version Of JavaAnalysis Of The Original Version Of Java
Analysis Of The Original Version Of JavaAmanda Brady
 
tranning synopsis(java programming).pdf
tranning synopsis(java programming).pdftranning synopsis(java programming).pdf
tranning synopsis(java programming).pdfsumitgiri32
 
Built Cross-Platform Application with .NET Core Development.pdf
Built Cross-Platform Application with .NET Core Development.pdfBuilt Cross-Platform Application with .NET Core Development.pdf
Built Cross-Platform Application with .NET Core Development.pdfI-Verve Inc
 
Top 11 Front-End Web Development Tools To Consider in 2020
 Top 11 Front-End Web Development Tools To Consider in 2020 Top 11 Front-End Web Development Tools To Consider in 2020
Top 11 Front-End Web Development Tools To Consider in 2020Katy Slemon
 
What makes Flutter the best cross platform sdk
What makes Flutter the best cross platform sdkWhat makes Flutter the best cross platform sdk
What makes Flutter the best cross platform sdkExpeed Software
 
Unesco Presentation
Unesco PresentationUnesco Presentation
Unesco PresentationUmesh
 
Front End Development | Introduction
Front End Development | IntroductionFront End Development | Introduction
Front End Development | IntroductionJohnTaieb
 
Cross Platform Development Using Flutter
Cross Platform Development Using FlutterCross Platform Development Using Flutter
Cross Platform Development Using Fluttervivatechijri
 
Top 10 Front End Development Technologies to Focus in 2018
Top 10 Front End Development Technologies to Focus in 2018Top 10 Front End Development Technologies to Focus in 2018
Top 10 Front End Development Technologies to Focus in 2018Helios Solutions
 
Best things to know about .net framework
Best things to know about .net frameworkBest things to know about .net framework
Best things to know about .net frameworkShelly Megan
 
TypeScript - Javascript done right
TypeScript - Javascript done rightTypeScript - Javascript done right
TypeScript - Javascript done rightWekoslav Stefanovski
 
.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions.Net introduction by Quontra Solutions
.Net introduction by Quontra SolutionsQUONTRASOLUTIONS
 

Similaire à Internationalize your JavaScript Application: Prepare for "the next billion" internet users. (20)

Balaji Resume
Balaji ResumeBalaji Resume
Balaji Resume
 
Introduction to .net
Introduction to .netIntroduction to .net
Introduction to .net
 
Portable Code Compiler
Portable Code CompilerPortable Code Compiler
Portable Code Compiler
 
What Is A Technology Stack?
What Is A Technology Stack?What Is A Technology Stack?
What Is A Technology Stack?
 
Cross Platform Mobile Technologies
Cross Platform Mobile TechnologiesCross Platform Mobile Technologies
Cross Platform Mobile Technologies
 
Analysis Of The Original Version Of Java
Analysis Of The Original Version Of JavaAnalysis Of The Original Version Of Java
Analysis Of The Original Version Of Java
 
ColdBox i18N
ColdBox i18N ColdBox i18N
ColdBox i18N
 
tranning synopsis(java programming).pdf
tranning synopsis(java programming).pdftranning synopsis(java programming).pdf
tranning synopsis(java programming).pdf
 
Built Cross-Platform Application with .NET Core Development.pdf
Built Cross-Platform Application with .NET Core Development.pdfBuilt Cross-Platform Application with .NET Core Development.pdf
Built Cross-Platform Application with .NET Core Development.pdf
 
Top 11 Front-End Web Development Tools To Consider in 2020
 Top 11 Front-End Web Development Tools To Consider in 2020 Top 11 Front-End Web Development Tools To Consider in 2020
Top 11 Front-End Web Development Tools To Consider in 2020
 
Chapter1
Chapter1Chapter1
Chapter1
 
What makes Flutter the best cross platform sdk
What makes Flutter the best cross platform sdkWhat makes Flutter the best cross platform sdk
What makes Flutter the best cross platform sdk
 
Unesco Presentation
Unesco PresentationUnesco Presentation
Unesco Presentation
 
Front End Development | Introduction
Front End Development | IntroductionFront End Development | Introduction
Front End Development | Introduction
 
Cross Platform Development Using Flutter
Cross Platform Development Using FlutterCross Platform Development Using Flutter
Cross Platform Development Using Flutter
 
Top 10 Front End Development Technologies to Focus in 2018
Top 10 Front End Development Technologies to Focus in 2018Top 10 Front End Development Technologies to Focus in 2018
Top 10 Front End Development Technologies to Focus in 2018
 
Full Stack Development
Full Stack DevelopmentFull Stack Development
Full Stack Development
 
Best things to know about .net framework
Best things to know about .net frameworkBest things to know about .net framework
Best things to know about .net framework
 
TypeScript - Javascript done right
TypeScript - Javascript done rightTypeScript - Javascript done right
TypeScript - Javascript done right
 
.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions.Net introduction by Quontra Solutions
.Net introduction by Quontra Solutions
 

Plus de Kevin Hakanson

Sharpen your "Architectural Documentation" Saw
Sharpen your "Architectural Documentation" SawSharpen your "Architectural Documentation" Saw
Sharpen your "Architectural Documentation" SawKevin Hakanson
 
Who's in your Cloud? Cloud State Monitoring
Who's in your Cloud? Cloud State MonitoringWho's in your Cloud? Cloud State Monitoring
Who's in your Cloud? Cloud State MonitoringKevin Hakanson
 
Adopting Multi-Cloud Services with Confidence
Adopting Multi-Cloud Services with ConfidenceAdopting Multi-Cloud Services with Confidence
Adopting Multi-Cloud Services with ConfidenceKevin Hakanson
 
Introduction to Speech Interfaces for Web Applications
Introduction to Speech Interfaces for Web ApplicationsIntroduction to Speech Interfaces for Web Applications
Introduction to Speech Interfaces for Web ApplicationsKevin Hakanson
 
Learning to Mod Minecraft: A Father/Daughter Retrospective
Learning to Mod Minecraft: A Father/Daughter RetrospectiveLearning to Mod Minecraft: A Father/Daughter Retrospective
Learning to Mod Minecraft: A Father/Daughter RetrospectiveKevin Hakanson
 
ng-owasp: OWASP Top 10 for AngularJS Applications
ng-owasp: OWASP Top 10 for AngularJS Applicationsng-owasp: OWASP Top 10 for AngularJS Applications
ng-owasp: OWASP Top 10 for AngularJS ApplicationsKevin Hakanson
 
Securing TodoMVC Using the Web Cryptography API
Securing TodoMVC Using the Web Cryptography APISecuring TodoMVC Using the Web Cryptography API
Securing TodoMVC Using the Web Cryptography APIKevin Hakanson
 
Make your own Print & Play card game using SVG and JavaScript
Make your own Print & Play card game using SVG and JavaScriptMake your own Print & Play card game using SVG and JavaScript
Make your own Print & Play card game using SVG and JavaScriptKevin Hakanson
 
Implementing Messaging Patterns in JavaScript using the OpenAjax Hub
Implementing Messaging Patterns in JavaScript using the OpenAjax HubImplementing Messaging Patterns in JavaScript using the OpenAjax Hub
Implementing Messaging Patterns in JavaScript using the OpenAjax HubKevin Hakanson
 
Developer's Guide to JavaScript and Web Cryptography
Developer's Guide to JavaScript and Web CryptographyDeveloper's Guide to JavaScript and Web Cryptography
Developer's Guide to JavaScript and Web CryptographyKevin Hakanson
 

Plus de Kevin Hakanson (10)

Sharpen your "Architectural Documentation" Saw
Sharpen your "Architectural Documentation" SawSharpen your "Architectural Documentation" Saw
Sharpen your "Architectural Documentation" Saw
 
Who's in your Cloud? Cloud State Monitoring
Who's in your Cloud? Cloud State MonitoringWho's in your Cloud? Cloud State Monitoring
Who's in your Cloud? Cloud State Monitoring
 
Adopting Multi-Cloud Services with Confidence
Adopting Multi-Cloud Services with ConfidenceAdopting Multi-Cloud Services with Confidence
Adopting Multi-Cloud Services with Confidence
 
Introduction to Speech Interfaces for Web Applications
Introduction to Speech Interfaces for Web ApplicationsIntroduction to Speech Interfaces for Web Applications
Introduction to Speech Interfaces for Web Applications
 
Learning to Mod Minecraft: A Father/Daughter Retrospective
Learning to Mod Minecraft: A Father/Daughter RetrospectiveLearning to Mod Minecraft: A Father/Daughter Retrospective
Learning to Mod Minecraft: A Father/Daughter Retrospective
 
ng-owasp: OWASP Top 10 for AngularJS Applications
ng-owasp: OWASP Top 10 for AngularJS Applicationsng-owasp: OWASP Top 10 for AngularJS Applications
ng-owasp: OWASP Top 10 for AngularJS Applications
 
Securing TodoMVC Using the Web Cryptography API
Securing TodoMVC Using the Web Cryptography APISecuring TodoMVC Using the Web Cryptography API
Securing TodoMVC Using the Web Cryptography API
 
Make your own Print & Play card game using SVG and JavaScript
Make your own Print & Play card game using SVG and JavaScriptMake your own Print & Play card game using SVG and JavaScript
Make your own Print & Play card game using SVG and JavaScript
 
Implementing Messaging Patterns in JavaScript using the OpenAjax Hub
Implementing Messaging Patterns in JavaScript using the OpenAjax HubImplementing Messaging Patterns in JavaScript using the OpenAjax Hub
Implementing Messaging Patterns in JavaScript using the OpenAjax Hub
 
Developer's Guide to JavaScript and Web Cryptography
Developer's Guide to JavaScript and Web CryptographyDeveloper's Guide to JavaScript and Web Cryptography
Developer's Guide to JavaScript and Web Cryptography
 

Dernier

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 

Dernier (20)

TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 

Internationalize your JavaScript Application: Prepare for "the next billion" internet users.

  • 1. Internationalize your JavaScript Application: Prepare for "the next billion" internet users. Kevin Hakanson (2 November 2013)
  • 2. Abstract Are you prepared for "the next billion" internet users, most of whom don't use English as their primary language? We will explore the globalization (internationalization and localization) of JavaScript based applications. It will look at the ECMAScript Internationalization API and popular open source projects like AngularJS, messageformat.js, jQuery Globalize and twitter-cldr-js. Topics will include cultures/locales, character encoding, number formatting, date formatting, choice/plural formatting and translations.
  • 4. Bio Kevin Hakanson is an application architect for Thomson Reuters where he is focused on highly scalable web applications, especially the JavaScript and security aspects. His background includes both .NET and Java, but he is most nostalgic about Lotus Notes. He has been developing professionally since 1994 and holds a Master’s degree in Software Engineering. When not staring at a computer screen, he is probably staring at another screen, either watching TV or playing video games with his family.
  • 5.
  • 6.
  • 7.
  • 8. Internationalization Internationalization of software means designing it such that it supports or can be easily adapted to support the needs of users speaking different languages and having different cultural expectations, and enables worldwide communication between them.
  • 9. Localization Localization then is the actual adaptation to a specific language and culture.
  • 10. Globalization Globalization of software is commonly understood to be the combination of internationalization and localization.
  • 12. I18N & L10N The terms are frequently abbreviated to the numeronyms i18n (where 18 stands for the number of letters between the first i and last n in internationalization) and l10n respectively, due to the length of the words. Other “correct” spellings are internationalisation and localisation. http://en.wikipedia.org/wiki/Internationalization_and_localization
  • 14. ECMAScript Internationalization API The ECMAScript Internationalization API provides key language-sensitive functionality as a complement to the ECMAScript Language Specification, 5.1 edition or successor. Standard ECMA-402 1st Edition / December 2012 http://www.ecma-international.org/ecma-402/1.0/
  • 15. ECMA-402 Its functionality has been selected from that of well-established internationalization APIs such as those of the: ● Internationalization Components for Unicode (ICU) library ● .NET framework ● Java platform
  • 16. ICU - International Components for Unicode http://site.icu-project.org/ ICU is a mature, widely used set of C/C++ and Java libraries providing Unicode and Globalization support for software applications. ICU is widely portable and gives applications the same results on all platforms and between C/C++ and Java software. ICU is released under a nonrestrictive open source license that is suitable for use with both commercial software and with other open source or free software. Used by both Google Chrome and Mozilla Firefox.
  • 17. ECMA-402 Three key pieces of language-sensitive functionality: ○ String comparison (collation) String.prototype.localeCompare ○ Number formatting Number.prototype.toLocaleString ○ Date and time formatting Date.prototype.toLocaleString Date.prototype.toLocaleDateString Date.prototype.toLocaleTimeString
  • 18. ECMA-402 Language Tags Identifies locales using language tags as defined by IETF BCP 47 (RFCs 5646 and 4647 or their successors), which may include extensions such as those registered through RFC 6067. Their canonical form is specified in RFC 5646 section 4.5 or its successor. (Note: RFC 5646 uses ISO 639 codes for language)
  • 19.
  • 20. Example Language Tags en-US English (United States) es-MX Español (México) fr-CA français (Canada) > var locales = ["en-US", "es-MX", "fr-CA"] > Intl.DateTimeFormat.supportedLocalesOf(locales) ["en-US", "es-MX", "fr-CA"]
  • 21. ECMA-402 Browser Support Google Chrome Firefox Nightly Bug 853301 - Enable ECMAScript Internationalization API for desktop Firefox http://nightly.mozilla.org/ Internet Explorer 11 Part of Windows 8.1 Update to Windows 7 or Windows Server 2008 http://windows.microsoft.com/en-us/internet-explorer/ie-11-release-preview http://ie.microsoft.com/testdrive/Info/Downloads/Default.html
  • 22. ECMAScript Internationalization test402 http://test262.ecmascript.org/testcases_intl402.html# Google Chrome (Version 30.0.1599.101) Internet Explorer (Version 11.0.9600.16384) Firefox Nightly 27.0a1 (2013-10-16)
  • 23. ECMA-402 Blog Posts Norbert Lindenberg (Dec 2012) http://norbertlindenberg.com/2012/12/ecmascriptinternationalization-api/ David Storey (Aug 2013) http://generatedcontent.org/post/59403168016/esintlapi Dr. Alex Rauschmayer (Sep 2013) http://www.2ality.com/2013/09/ecmascript-i18n-api.html
  • 24. EMCA-402 Polyfill/Backport? Compatibility implementation of the ECMAScript Internationalization API (ECMA-402) for JavaScript https://github.com/andyearnshaw/Intl.js “Intl.js attempts to fill the void of availability for this API... so that developers can take advantage of the native API in environments that support it, or Intl.js for legacy or unsupporting environments.” “Intl.js is designed to be compatible with ECMAScript 3.1 environments in order to follow the specification as closely as possible.”
  • 25. jQuery Globalize Used to be a jQuery plugin (jquery-global), now a JavaScript library called Globalize. http://wiki.jqueryui.com/w/page/39118647/Globalize Enables complex culture-aware number and date parsing and formatting, including the raw culture information for hundreds of different languages and countries, as well as an extensible system for localization.
  • 26. Globalize Cultures Each culture is given a unique code that is a combination of an ISO 639 two-letter lowercase culture code for the language, and a two-letter uppercase code for the country or region. For example, "en-US" is the culture code for English in the United States. "Neutral" cultures based on accepted set of rules by anyone speaking that language. For example, "es" is the neutral culture for Spanish.
  • 27. Globalize API Globalize.addCultureInfo( cultureName, extendCultureName, info ) Globalize.cultures Globalize.culture( selector ) Globalize.findClosestCulture( selector ) Globalize.format( value, format, culture ) Globalize.localize( key, culture ) Globalize.parseInt( value, radix, culture ) Globalize.parseFloat( value, radix, culture ) Globalize.parseDate( value, formats, culture )
  • 28. Globalize Culture Examples > Globalize.culture("en-US") Object {name: "en-US", englishName: "English (United States)", nativeName: "English", isRTL: false, language: "en"…} > Globalize.culture("es-MX") Object {name: "es-MX", englishName: "Spanish (Mexico)", nativeName: "Español (México)", isRTL: false, language: "es"…} > Globalize.culture("fr-CA") Object {name: "fr-CA", englishName: "French (Canada)", nativeName: "français (Canada)", isRTL: false, language: "fr"…}
  • 29. HTML is great for declaring static documents, but it falters when we try to use it for declaring dynamic views in web-applications. AngularJS lets you extend HTML vocabulary for your application. The resulting environment is extraordinarily expressive, readable, and quick to develop. http://www.angularjs.org/
  • 30. AngularJS I18N/L10N Support Supports i18n/l10n for datetime, number and currency filters. Localizable pluralization support provided by the ngPluralize directive. All localizable components depend on localespecific rule sets managed by the $locale service. http://docs.angularjs.org/guide/i18n
  • 31. Extending AngularJS i18n for your Angular apps, made easy http://pascalprecht.github.io/angular-translate/#/guide Internazionalization (i18n) with AngularJS http://blog.brunoscopelliti.com/internazionalization-i18n-with-angularjs Localizing Your AngularJS App http://codingsmackdown.tv/blog/2012/12/14/localizing-your-angularjs-app/ Angular i18n library wrapping Jed (gettext for js) https://github.com/ErikAndreas/lingua
  • 32. messageformat.js ICU MessageFormat for Javascript - i18n Plural and Gender Capable Messages ● Implements SelectFormat and PluralFormat ● Plan to pull in locale-aware NumberFormat parsing as a "plugin" https://github.com/SlexAxton/messageformat.js
  • 33. Example Formatted Message {GENDER, select, male {He has} female {She has} other {They have} } {NUM_FRIENDS, plural, =0 {no friends} one {1 friend} other {# friends} }
  • 34. Example Usage > var mf = new MessageFormat('en') > var messageTemplate = "{...}" // see previous slide > var message = mf.compile(messageTemplate) > message({ "GENDER" : "male" , "NUM_FRIENDS" : 0 }) "He has no friends." > message({ "GENDER" : "female" , "NUM_FRIENDS" : 1 }) "She has 1 friend." > message({ "GENDER" : "other" , "NUM_FRIENDS" : 2 }) "They have 2 friends."
  • 35. messageformat.js Alex Sexton: Client Side Internationalization https://www.youtube.com/watch?v=uXS_-JRsB8M
  • 36. CLDR - Unicode Common Locale Data Repository http://cldr.unicode.org/ The Unicode CLDR provides key building blocks for software to support the world's languages, with the largest and most extensive standard repository of locale data available. This data is used by a wide spectrum of companies for their software internationalization and localization, adapting software to the conventions of different languages for such common software tasks.
  • 37. CLDR Includes: Locale-specific patterns for formatting and parsing: dates, times, timezones, numbers and currency values Translations of names: languages, scripts, countries and regions, currencies, eras, months, weekdays, day periods, timezones, cities, and time units Language & script information: characters used; plural cases; gender of lists; capitalization; rules for sorting & searching; writing direction; transliteration rules; rules for spelling out numbers; rules for segmenting text into graphemes, words, and sentences Country information: language usage, currency information, calendar preference and week conventions, postal and telephone codes Other: ISO & BCP 47 code support (cross mappings, etc.), keyboard layouts
  • 38. twitter-cldr-js 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. Currently, supports the following: ● ● ● ● ● ● Date and time formatting Relative date and time formatting (eg. 1 month ago) Number formatting (decimal, currency, and percentage) Long/short decimals Plural rules Bidirectional reordering https://github.com/twitter/twitter-cldr-js
  • 41. EMCA-402 Implementation dependent David Storey’s Appendix A includes locale support tables for each browser that supports the Internationalization API http://generatedcontent.org/post/59403168016/esintlapi
  • 42. Globalize (~353) // globalize.js Globalize.cultures[ "default" ] = {...}; days: { // full day names names: [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ], // globalize.cultures.es-MX.js Globalize.addCultureInfo( "es-MX", "default", {...}); days: { names: ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"],
  • 43. AngularJS "en-US" $locale Bundled into angular.js angularModule('ngLocale', []).provider('$locale', $LocaleProvider); function $LocaleProvider(){ this.$get = function() { return { id: 'en-us', /* … */ }; }; } In separate angular-locale_en-US.js angular.module("ngLocale", [], ["$provide", function($provide) { $provide.value("$locale", { "id": "en-us", /* … */ });
  • 44. AngularJS (~287) angular-locale_en-us.js angular-locale_es-mx.js "DAY": { "0": "Sunday", "1": "Monday", "2": "Tuesday", "3": "Wednesday", "4": "Thursday", "5": "Friday", "6": "Saturday" }, "DAY": { "0": "domingo", "1": "lunes", "2": "martes", "3": "miu00e9rcoles", "4": "jueves", "5": "viernes", "6": "su00e1bado" },
  • 45. AngularJS locale.js File Encoding Bug Fix from 1.0.7 monochromatic-rainbow (2013-05-22) i18n: escape all chars above u007f in locale files (695c54c1, #2417) https://github.com/angular/angular.js/blob/master/CHANGELOG.md Example: src/ngLocale/angular-locale_es-mx.js - "3": "miércoles", + "3": "miu00e9rcoles",
  • 46. Globalize Culture Examples (redux) > Globalize.culture("en-US") Object {name: "en-US", englishName: "English (United States)", nativeName: "English", isRTL: false, language: "en"…} > Globalize.culture("es-MX") Object {name: "es-MX", englishName: "Spanish (Mexico)", nativeName: "Español (México)", isRTL: false, language: "es"…} > Globalize.culture("fr-CA") Object {name: "fr-CA", englishName: "French (Canada)", nativeName: "français (Canada)", isRTL: false, language: "fr"…}
  • 47. Globalize File Encoding (UTF-8) WebStorm integrated web server without <meta charset='utf-8'/> in demo.html http://localhost:63342/jsi18n/demo.html#/ > Globalize.culture("es-MX") Object {name: "es-MX", englishName: "Spanish (Mexico)", nativeName: "Español (México)", isRTL: false, language: "es" …} > Globalize.culture("fr-CA") Object {name: "fr-CA", englishName: "French (Canada)", nativeName: "français (Canada)", isRTL: false, language: "fr"…}
  • 48. X-Powered-By: Express var express = require('express'); var app = express(); app.configure(function(){ app.use(function(req, res, next) { if (/.*.js/.test(req.path)) { res.charset = "utf-8"; // Content-Type: application/javascript; charset=utf-8 } next(); }); app.use(express.static('../jsi18n')); }); app.listen(1337); // http://localhost:1337/demo.html#/
  • 49. More Options Convert source code to ASCII (7 bit) Before: var σ = 'Köln'; > uglifyjs -b beautify=false,ascii-only=true file.js After: var u03c3="Kxf6ln"; Load source with charset attribute on script tag var π = Math.PI, ε = 1e-6; <script charset="utf-8" src="d3.js"></script> http://www.2ality.com/2013/09/javascript-unicode.html
  • 50. é (small e, acute accent) HTML Entity &eacute; &#xE9; &#233; JavaScript "u00E9" "xE9" UTF-8 é C3 A9 URL %e9 %c3%a9
  • 51. ñ (small n, tilde) HTML Entity &ntilde; &#xF1; &#241; JavaScript "u00F1" "xF1" UTF-8 ñ C3 B1 URL %f1 %c3%b1
  • 52. ç (small c, cedilla) HTML Entity &ccedil; &#xE7; &#231; JavaScript "u00E7" "xE7" UTF-8 ç C3 A7 URL %e7 %c3%a7
  • 53. > div = document.createElement("div") <div>​</div> > div.innerHTML = "&ntilde;&eacute;&ccedil;" "&ntilde;&eacute;&ccedil;" > div.innerText "ñéç" > div.innerHTML "ñéç" > div.innerHTML = "&ntilde;&eacute;&ccedil; &#xF1;&#xE9;&#xE7; &#241; &#233;&#231;" "&ntilde;&eacute;&ccedil; &#xF1;&#xE9;&#xE7; &#241;&#233;&#231;" > div.innerHTML "ñéç ñéç ñéç" > div.innerText = "u00F1u00E9u00E7 xF1xE9xE7" "ñéç ñéç" > encodeURIComponent("ñéç") "%C3%B1%C3%A9%C3%A7" escape("ñéç") "%F1%E9%E7" >
  • 54. Mac Character Viewer System Preferences - Keyboard ☑Show Keyboard & Character Viewers in menu bar
  • 55. Windows Character Map Start - Run - charmap.exe
  • 56. JavaScript has a Unicode problem > "mañana" == "mañana" false > "maxF1ana" == "manu0303ana" false The first string contains U+00F1 LATIN SMALL LETTER N WITH TILDE, while the second string uses two separate code points (U+006E LATIN SMALL LETTER N and U+0303 COMBINING TILDE) to create the same glyph. http://mathiasbynens.be/notes/javascript-unicode
  • 57. DEMO
  • 59. ECMA-402 No settable default locale list: ● global communication channel (security risk) ● application may require multiple components and contexts; higher-level concern of application/library vs ECMAScript Every API takes a locale list as parameter
  • 60. Globalize Include culture files <script src="globalize/globalize.js"></script> <script src="globalize/cultures/globalize.culture.en-US.js"></script> <script src="globalize/cultures/globalize.culture.es-MX.js"></script> <script src="globalize/cultures/globalize.culture.fr-CA.js"></script> Call function to set default Globalize.culture("en-US")
  • 61. AngularJS $locale Binding AngularJS binds value of $locale and doesn’t allow updates to locale or including multiple locale <script> files. <script src="angular/angular.js"></script> <script> // http://stackoverflow.com/questions/13007430/angularjs-and-locale (function () { var locale = localStorage.getItem('$locale'); if (locale) { document.write('<script src="angular/i18n/angular-locale_' + locale.toLowerCase() + '.js"></script>'); } })(); </script>
  • 62. AngularJS Number Filter Number filter captures value of $locale. NUMBER_FORMATS inside closure. https://github.com/angular/angular.js/blob/master/src/ng/filter/filters.js numberFilter.$inject = ['$locale']; function numberFilter($locale) { var formats = $locale.NUMBER_FORMATS; return function(number, fractionSize) { return formatNumber(number, formats.PATTERNS[0], formats.GROUP_SEP, formats.DECIMAL_SEP, fractionSize); }; }
  • 65. Decimal Mark A symbol used to separate the integer part from the fractional part of a number written in decimal form. Different cultures use different symbols for the decimal mark. The choice of symbol for the decimal mark also affects the choice of symbol for the thousands separator used in digit grouping. Full Stop "." – Blue Comma "," – Green Eastern Arabic numerals – Red Data unavailable – Grey http://en.wikipedia.org/wiki/Decimal_mark
  • 66. Globalize Four main types of number formatting: ● ● ● ● n for number d for decimal digits p for percentage c for currency Each format token may also be followed by a number determining how many decimal places to display (with d determines the minimum number of digits to display, zero padded)
  • 67. > > > > > > > > Globalize.format(12345.6789, "n") "12,345.68" Globalize.format(12345.6789, "n4") "12,345.6789" Globalize.format(12345.6789, "n9") "12,345.678900000" Globalize.format(12345.6789, "d") "12345" Globalize.format(12345.6789, "d4") "12345" Globalize.format(12345.6789, "d9") "000012345" Globalize.format(12345.6789, "n", "es-MX") "12,345.68" Globalize.format(12345.6789, "n", "fr-CA") "12 345,68"
  • 68. EMCA-402 Three format styles for NumberFormat objects and Number.prototype.toLocaleString: decimal currency percent Number of digits used to represent a number can be constrained Grouping separators can be disabled
  • 69. ECMA-402 Components of Number and Currency Formats Property Values style "decimal", "currency", "percent" currency string currencyDisplay "code", "symbol", "name" minimumIntegerDigits non-negative integer minimumFractionDigits non-negative integer maximumFractionDigits non-negative integer minimumSignificantDigits positive integer maximumSignificantDigits positive integer useGrouping boolean
  • 70. > > > > > > > num = 12345.6789 12345.6789 num.toLocaleString("en-us") "12,345.679" num.toLocaleString("en-us", { useGrouping: true, minimumSignificantDigits: 4}) "12,345.6789" num.toLocaleString("en-US", { useGrouping: false, maximumFractionDigits: 0}) "12346" num.toLocaleString("en-US", { useGrouping: false, maximumFractionDigits: 0, minimumIntegerDigits: 9}) "000012346" num.toLocaleString("es-MX") "12,345.679" num.toLocaleString("fr-CA") "12 345,679"
  • 71. Chromium Issue 304722 Intl NumberFormat minimumFractionDigits options property not honored Reported by kevin.ha...@gmail.com, Oct 6 Steps to reproduce the problem: (12345.6789).toLocaleString("en-us",{ useGrouping: false, minimumFractionDigits: 4}) "12345.679" What is the expected behavior? "12345.6789" https://code.google.com/p/chromium/issues/detail?id=304722
  • 72. twitter-cldr-js Number formatting supports decimals, currencies, and percentages. In addition to formatting regular decimals, 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".
  • 73. > num = 12345.6789 12345.6789 fmt = new TwitterCldr.DecimalFormatter() DecimalFormatter {all_tokens: Object, tokens: Array[0], symbols: Object, default_symbols: Object, constructor: function…} > fmt.format(num) "12,345.6789" > fmt.format(num, {precision: 2}) "12,345.68" > fmt = new TwitterCldr.CurrencyFormatter(); CurrencyFormatter {default_currency_symbol: "$", default_precision: 2, all_tokens: Object, tokens: Array[0], symbols: Object…} > fmt.format(num, {currency: "USD"}) "$12,345.6789" > fmt.format(num, {currency: "USD", precision: 2}) "$12,345.68" >
  • 74. DEMO
  • 77. ECMA-402 Components of Date and Time Formats Property Values weekday "narrow", "short", "long" era "narrow", "short", "long" year "2-digit", "numeric" month "2-digit", "numeric", "narrow", "short", "long" day "2-digit", "numeric" hour "2-digit", "numeric" minute "2-digit", "numeric" second "2-digit", "numeric" timeZoneName "short", "long"
  • 78. Globalize Standard Date Formats Format Meaning "en-US" new Date (2013,0,1,13,1,1) f Long Date, Short dddd, MMMM dd, yyyy h:mm tt Time Tuesday, January 01, 2013 1:01 PM F Long Date, Long Time dddd, MMMM dd, yyyy h:mm:ss tt Tuesday, January 01, 2013 1:01:01 PM t Short Time h:mm tt 1:01 PM T Long Time h:mm:ss tt 1:01:01 PM d Short Date M/d/yyyy 1/1/2013 D Long Date dddd, MMMM dd, yyyy Tuesday, January 01, 2013 Y Month/Year MMMM, yyyy January 2013 * M Month/Day MMMM dd January 01 * https://github.com/jquery/globalize/issues/97
  • 79. AngularJS Predefined Date Formats Format "en-US" new Date (2013,0,1,13,1,1) medium MMM d, y h:mm:ss a Jan 1, 2013 1:01:01 PM short M/d/yy h:mm a 1/1/13 1:01 PM fullDate EEEE, MMMM d,y Tuesday, January 1, 2013 longDate MMM d, y January 1, 2013 mediumDate MMM d, y Jan 1, 2013 shortDate M/d/yy 1/1/13 mediumTime h:mm:ss a 1:01:01 PM shortTime h:mm a 1:01 PM
  • 80. Date/Time Format Patterns jQuery Globalize AngularJS // short date pattern "fullDate": "EEEE, MMMM d, y", d: "M/d/yyyy", "longDate": "MMMM d, y", // long date pattern "medium": "MMM d, y h:mm:ss a", D: "dddd, MMMM dd, yyyy", "mediumDate": "MMM d, y", // short time pattern "mediumTime": "h:mm:ss a", t: "h:mm tt", "short": "M/d/yy h:mm a", // long time pattern "shortDate": "M/d/yy", T: "h:mm:ss tt", "shortTime": "h:mm a" // long date, short time pattern f: "dddd, MMMM dd, yyyy h:mm tt", // long date, long time pattern F: "dddd, MMMM dd, yyyy h:mm:ss tt", // month/day pattern M: "MMMM dd", // month/year pattern Y: "yyyy MMMM"
  • 81. twitter-cldr-js The default CLDR data set only includes 4 date formats, full, long, medium, and short. See below for a list of additional formats. > TwitterCldr.DateTimeFormatter.additional_formats() ["EHm", "EHms", "Ed", "Ehm", "Ehms", "Gy", "GyMMM", "GyMMMEd", "GyMMMd", "H", "Hm", "Hms", "M", "MEd", "MMM", "MMMEd", "MMMd", "Md", "d", "h", "hm", "hms", "ms", "y", "yM", "yMEd", "yMMM", "yMMMEd", "yMMMd", "yMd", "yQQQ", "yQQQQ"]
  • 82. > then = new Date (2013,0,1,13,1,1) Tue Jan 01 2013 13:01:01 GMT-0600 (CST) > Globalize.format( then , 'f' ) "Tuesday, January 01, 2013 1:01 PM" > then.toLocaleString("en-US", { weekday : "long", month : "long", day : "2-digit", year : "numeric", hour : "numeric", minute : "2digit", hour12 : true } ) "Tuesday, January 01, 2013 1:01 PM" fmt = new TwitterCldr.DateTimeFormatter() DateTimeFormatter {tokens: Object, weekday_keys: Array[7], methods: Object, format: function, get_tokens: function…} > fmt.format(then, {"type": "full"}) "Tuesday, January 1, 2013 at 1:01:01 PM UTC-06:00" >
  • 83. > then = new Date (2013,0,1,13,1,1) Tue Jan 01 2013 13:01:01 GMT-0600 (CST) > Globalize.format( then , 't' ) "1:01 PM" > then.toLocaleTimeString("en-US", { hour : "numeric", minute : "2-digit", hour12 : true } ) "1:01 PM" fmt = new TwitterCldr.DateTimeFormatter() DateTimeFormatter {tokens: Object, weekday_keys: Array[7], methods: Object, format: function, get_tokens: function…} > fmt.format(then, {"format": "time", "type": "short"}) "1:01 PM" >
  • 84. Intl AngularJS Filter Proof of concept AngularJS filter illustrating ECMA-402 API. Takes two parameters: arg0 - culture (optional) arg1 - format (predefined string or Intl struct) Sample usage: {{ then | dateIntl:"en-US":"longDate" }} {{ then | dateIntl:"es-MX":"longDate" }} {{ then | dateIntl:"longDate" }} {{ then | dateIntl:{ hour : "numeric", minute : "2-digit", hour12 : true } }}
  • 85. var IntlFilters = angular.module('Intl.filters', []); IntlFilters.filter('dateIntl', ['$locale', function($locale) { return function(date, arg0, arg1) { var culture = ( arg1 ? arg0 : $locale.id ); var format = (arg1 ? arg1 : arg0 ); var s = date.toISOString(); if (typeof format == "string") { switch (format) { case "medium" : s = date.toLocaleString(culture, { month : "short", day : "numeric", year : "numeric", hour : "numeric", minute : "2-digit", second : "2digit", hour12 : true }); break; // case "longdate", "shorttime", ... } } else { s = date.toLocaleString(culture, format); } return s; }; }]);
  • 86. DEMO
  • 87. AngularUI Bootstrap Datepicker (ui.bootstrap. datepicker) ● A clean, flexible, and fully customizable date picker. ● Everything is formatted using the date filter and thus is also localized. Timepicker (ui.bootstrap. timepicker)
  • 88. Choice / Plural Formatting
  • 89. In the field of memetics, a metameme (or meta-meme) is defined as a meme about a meme. http://en.wikipedia.org/wiki/Metameme
  • 90. messageformat.js (redux) {GENDER, select, male {He has} female {She has} other {They have} } {NUM_FRIENDS, plural, =0 {no friends} one {1 friend} other {# friends} }
  • 91. AngularJS: ngSwitch Conditionally swap DOM structure on your template based on a scope expression Place an expression ng-switch="..." attribute Choses one of the nested elements and makes it visible based on which element matches the value obtained from the evaluated expression ng-switch-when="..." attribute is used to inform ngSwitch which element to display http://docs.angularjs.org/api/ng.directive:ngSwitch
  • 92. AngularJS: ngPluralize There are two plural categories in Angular's default en-US locale: "one" and "other". An explicit number rule can only match one number. Configure ngPluralize by providing two attributes: count and when. count can be either a string or an Angular expression when specifies the mappings between plural categories and the actual string to be displayed http://docs.angularjs.org/api/ng.directive:ngPluralize
  • 93. AngularJS Formatted Message <div> <span ng-switch="salutationkey"> <span ng-switch-when="MR">He</span> <span ng-switch-when="MRS">She</span> <span ng-switch-when="MS">She</span> </span> has <span ng-pluralize count="friends" when="{'0': 'no friends.', 'one': '1 friend.', 'other': '{{friends|number}} friends.'}"> </span> </div>
  • 94. twitter-cldr-js (en) TwitterCldr.PluralRules = (function() { function PluralRules() {} PluralRules.rules = {"keys": ["one","other"], "rule": function(n) { return (function() { if (n == 1) { return "one" } else { return "other" } })(); }}; PluralRules.all = function() { return this.rules.keys; }; PluralRules.rule_for = function(number) { var error; try { return this.rules.rule(number); } catch (_error) { error = _error; return "other"; } }; return PluralRules; })();
  • 97. AngularJS module that makes your life much easier when it comes to i18n and l10n including lazy loading and pluralization. Provides components like filters and directives, asynchronous loading of i18n data, full pluralization support through MessageFormat and much more! http://pascalprecht.github.io/angular-translate/#/guide
  • 98. angular translate Usage Set Preferred $translateProvider.preferredLanguage('en-US'); Update $translate.uses('en-US'); Use with HTML Template Binding {{ 'FAV_COLOR' | translate }}: Use in JavaScript $filter('translate')('FAV_COLOR');
  • 99. $translateProvider.translations('en-US', { FAV_COLOR : "Favorite Color", BLUE : "Blue", }); $translateProvider.translations('es-MX', { FAV_COLOR : "Color Favorito", BLUE : "Azul", }); $translateProvider.translations('fr-CA', { FAV_COLOR : "Couleur préférée", BLUE : "Bleu", });
  • 100. Globalize Usage Set Culture Globalize.culture( "en-US" ); Localize Globalize.localize( "FAV_COLOR" ) Localize for Culture Globalize.localize( "FAV_COLOR", "en-US" ) Globalize.localize( "FAV_COLOR", "fr-CA" )
  • 101. Globalize.addCultureInfo( "en-US", { messages: { "FAV_COLOR" : "Favorite Color", "BLUE" : "Blue" } }); Globalize.addCultureInfo( "es-MX", { messages: { "FAV_COLOR" : "Color Favorito", "BLUE" : "Azul" } }); Globalize.addCultureInfo( "fr-CA", { messages: { "FAV_COLOR" : "Couleur préférée", "BLUE" : "Bleu" } });
  • 102. DEMO