From 4bb981d94c645ad3f88a49d77d332ff1f553925d Mon Sep 17 00:00:00 2001 From: Jeffery Grajkowski Date: Thu, 18 Jul 2013 10:59:31 -0700 Subject: [PATCH] Intl support The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting. It is not yet support in all modern browsers and is not in lib.d.ts. --- README.md | 1 + intl/intl-tests.ts | 90 +++++++++++++++++++++++++++++++++++++++ intl/intl.d.ts | 102 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 intl/intl-tests.ts create mode 100644 intl/intl.d.ts diff --git a/README.md b/README.md index 5176fe729c..65e58c2216 100755 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ List of Definitions * [i18next](http://i18next.com/) (by [Maarten Docter](https://github.com/mdocter)) * [iCheck](http://damirfoy.com/iCheck/) (by [Dániel Tar](https://github.com/qcz)) * [Impress.js](https://github.com/bartaz/impress.js) (by [Boris Yankov](https://github.com/borisyankov)) +* [Intl](http://www.ecma-international.org/ecma-402/1.0/) (by [Jeffery Grajkowski](http://github.com/pushplay)) * [iScroll](http://cubiq.org/iscroll-4) (by [Boris Yankov](https://github.com/borisyankov)) * [jake](https://github.com/mde/jake) (by [Kon](http://phyzkit.net/)) * [Jasmine](http://pivotal.github.com/jasmine/) (by [Boris Yankov](https://github.com/borisyankov)) diff --git a/intl/intl-tests.ts b/intl/intl-tests.ts new file mode 100644 index 0000000000..d2a9967393 --- /dev/null +++ b/intl/intl-tests.ts @@ -0,0 +1,90 @@ +/// + +// Collator + +alert(Intl.Collator.supportedLocalesOf(["ban", "id-u-co-pinyin", "de-ID"], { localeMatcher: "lookup" }).join(", ")); +// → "id-u-co-pinyin, de-ID" + +// in German, ä sorts with a +alert(new Intl.Collator("de").compare("ä", "z").toString()); +// → a negative value + +// in Swedish, ä sorts after z +alert(new Intl.Collator("sv").compare("ä", "z").toString()); +// → a positive value + +// in German, ä has a as the base letter +alert(new Intl.Collator("de", { sensitivity: "base" }).compare("ä", "a").toString()); +// → 0 + +// in Swedish, ä and a are separate base letters +alert(new Intl.Collator("sv", { sensitivity: "base" }).compare("ä", "a").toString()); +// → a positive value + +var a = ["Offenbach", "Österreich", "Odenwald"]; +var collator = new Intl.Collator("de-u-co-phonebk"); +a.sort(collator.compare); +alert(a.join(", ")); +// → "Odenwald, Österreich, Offenbach" + + +// DateTimeFormat + +alert(Intl.DateTimeFormat.supportedLocalesOf(["ban", "id-u-co-pinyin", "de-ID"], { localeMatcher: "lookup" }).join(", ")); +// → "id-u-co-pinyin, de-ID" + +// toLocaleString without arguments depends on the implementation, +// the default locale, and the default time zone +alert(new Intl.DateTimeFormat().format(new Date(Date.UTC(2012, 11, 20, 3, 0, 0)))); +// → "12/19/2012" if run in en-US locale with time zone America/Los_Angeles + +alert(new Intl.DateTimeFormat("sr-RS", { weekday: "long", year: "numeric", month: "long", day: "numeric" }).format(new Date())); +// → "недеља, 7. април 2013." + +alert([new Date(2012, 08), new Date(2012, 11), new Date(2012, 03)].map(new Intl.DateTimeFormat("pt-BR", { year: "numeric", month: "long" }).format).join("; ")); +// → "setembro de 2012; dezembro de 2012; abril de 2012" + + +// NumberFormat + +alert(Intl.NumberFormat.supportedLocalesOf(["ban", "id-u-co-pinyin", "de-ID"], { localeMatcher: "lookup" }).join(", ")); +// → "id-u-co-pinyin, de-ID" + +alert(new Intl.NumberFormat("ru-RU", { style: "currency", currency: "RUB" }).format(654321.987)); +// → "654 321,99 руб." + +alert([123456.789, 987654.321, 456789.123].map(new Intl.NumberFormat("es-ES").format).join("; ")); +// → "123.456,789; 987.654,321; 456.789,123" + +// German uses comma as decimal separator and period for thousands +alert(new Intl.NumberFormat("de-DE").format(123456.789)); +// → 123.456,789 + +// Arabic in most Arabic speaking countries uses real Arabic digits +alert(new Intl.NumberFormat("ar-EG").format(123456.789)); +// → ١٢٣٤٥٦٫٧٨٩ + +// India uses thousands/lakh/crore separators +alert(new Intl.NumberFormat("en-IN").format(123456.789)); +// → 1,23,456.789 + +// the nu extension key requests a numbering system, e.g. Chinese decimal +alert(new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(123456.789)); +// → 一二三,四五六.七八九 + +// when requesting a language that may not be supported, such as +// Balinese, include a fallback language, in this case Indonesian +alert(new Intl.NumberFormat(["ban", "id"]).format(123456.789)); +// → 123.456,789 + +// request a currency format +alert(new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(123456.789)); +// → 123.456,79 € + +// the Japanese yen doesn't use a minor unit +alert(new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(123456.789)) +// → ¥123,457 + +// limit to three significant digits +alert(new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(123456.789)); +// → 1,23,000 \ No newline at end of file diff --git a/intl/intl.d.ts b/intl/intl.d.ts new file mode 100644 index 0000000000..bc3359483e --- /dev/null +++ b/intl/intl.d.ts @@ -0,0 +1,102 @@ +// Type definitions for Intl +// Project: http://www.ecma-international.org/ecma-402/1.0/ +// Definitions by: Jeffery Grajkowski +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare module Intl { + + export interface SupportedLocalesOfOptions { + localeMatcher?: string; + } + + export interface CollatorOptions { + localeMatcher?: string; + usage?: string; + sensitivity?: string; + ignorePunctuation?: boolean; + numeric?: boolean; + caseFirst?: string; + } + + export class Collator { + constructor(locales?: string, options?: CollatorOptions); + constructor(locales?: string[], options?: CollatorOptions); + static supportedLocalesOf(locales: string, options?: SupportedLocalesOfOptions): string[]; + static supportedLocalesOf(locales: string[], options?: SupportedLocalesOfOptions): string[]; + compare(string1: string, string2: string): number; + resolvedOptions(): { + locale: string; + usage: string; + sensitivity: string; + ignorePunctuation: boolean; + collation: string; + numeric: boolean; + caseFirst: string; + }; + } + + export interface DateTimeFormatOptions { + localeMatcher?: string; + timeZone?: string; + hour12?: boolean; + formatMatcher?: boolean; + } + + export class DateTimeFormat { + constructor(locales?: string, options?: DateTimeFormatOptions); + constructor(locales?: string[], options?: DateTimeFormatOptions); + static supportedLocalesOf(locales: string, options?: SupportedLocalesOfOptions): string[]; + static supportedLocalesOf(locales: string[], options?: SupportedLocalesOfOptions): string[]; + format(date: Date): string; + resolvedOptions(): { + locale: string; + calendar: string; + numberingSystem: string; + timeZone: string; + hour12?: boolean; + weekday?: string; + era?: string; + year?: string; + month?: string; + day?: string; + hour?: string; + minute?: string; + second?: string; + timeZoneName?: string; + }; + } + + export interface NumberFormatOptions { + localeMatcher?: string; + style?: string; + currency?: string; + currencyDisplay?: string; + useGrouping?: boolean; + minimumIntegerDigits?: number; + minimumFractionDigits?: number; + maximumFractionDigits?: number; + minimumSignificantDigits?: number; + maximumSignificantDigits?: number; + } + + export class NumberFormat { + constructor(locales?: string, options?: NumberFormatOptions); + constructor(locales?: string[], options?: NumberFormatOptions); + static supportedLocalesOf(locales: string, options?: SupportedLocalesOfOptions): string[]; + static supportedLocalesOf(locales: string[], options?: SupportedLocalesOfOptions): string[]; + format(n: number): string; + resolvedOptions(): { + locale: string; + numberingSystem: string; + style: string; + useGrouping: boolean; + currency?: string; + currencyDisplay?: string; + minimumIntegerDigits?: number; + minimumFractionDigits?: number; + maximumFractionDigits?: number; + minimumSignificantDigits?: number; + maximumSignificantDigits?: number; + } + } +} \ No newline at end of file