dust-helper-intl
Dust helpers for internationalization.
Installation
Browser
- Install with bower:
bower install dust-helper-intl
- Load the scripts into your page.
- Register the helpers:
DustHelperIntl;
Node/CommonJS
- You can install the helpers with npm:
npm install dust-helper-intl
- Load in the module and register it:
var Dust = ;globalIntl = globalIntl || ;;
NOTE: Since node (as of 0.10) doesn't provide the global Intl
object
(ECMA-402) you'll need to provide a polyfill. The intl
NPM package can
provide this or you can use another.
Usage
@intlDate
NOTE: We will use the following variables in the examples:
var dateStr = ; // 'Wed Mar 26 2014 15:18:48 GMT-0700 (PDT)'var timeStamp = ; // 1395872439650
Convert from date string
Template:
var tmpl = '<time>{@intlDate val="' + dateStr + '" /}</time>';
Output:
3/26/2014
Convert from timestamp
Template:
var tmpl = '<time>{@intlDate val=' + timeStamp + ' /}</time>';
or
var tmpl = '<time>{@intlDate val={timeStamp} /}</time>';var ctx = timeStamp : ;
Output:
3/26/2014
Note: Timestamps are always numeric values, passing them as String will fail (see JavaScript Date constructor).
Formatting the output
Template:
var tmpl = '<time>{@intlDate val="' + dateStr + '" hour="numeric" minute="numeric" timeZone="UTC"/}</time>';
Output:
3:26 PM
Configuration properties
Property | Allowed 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" |
@intlNumber
Basic (en-US)
Template:
var tmpl = '<b>{@intlNumber val="40000.004" /}</b>';
Output:
40,000.004
Basic (de-DE)
Template:
var tmpl = '<b>{@intlNumber val="40000.004" locales="de-DE"/}</b>';
Output:
40.000,004
Currency (USD)
Template:
var tmpl = '<b>{@intlNumber val="40000" style="currency" currency="USD" /}</b>';
Output:
$40,000
Currency (EUR)
Template:
var tmpl = '<b>{@intlNumber val="40000" style="currency" currency="EUR" /}</b>';
Output:
€40,000
Currency (JPY)
Template:
var tmpl = '<b>{@intlNumber val="40000" style="currency" currency="JPY" /}</b>';
Output:
¥40,000
Percentages (en-US)
Template:
var tmpl = '<b>{@intlNumber val="400" style="percent" /}</b>';
Output:
40,000 %
Percentages (de-DE)
Template:
var tmpl = '<b>{@intlNumber val="400" style="percent" locales="de-DE" /}</b>';
Output:
40.000 %
@intlMessage
NOTE: var ctx
is the context passed into the dust template.
Basic String using _msg
Template:
var tmpl = '<p>{@intlMessage _msg=MSG firstName=firstName lastName=lastName /}</p>' ctx = MSG: 'Hi, my name is {firstName} {lastName}.' firstName: 'Anthony' lastName: 'Pipkin' ;
Output:
Hi, my name is Anthony Pipkin.
Basic String using _key
Template:
var tmpl = '<p>{@intlMessage _key=KEY firstName=firstName lastName=lastName /}</p>' ctx = intl: messages: KEY: 'Hi, my name is {firstName} {lastName}.' firstName: 'Anthony' lastName: 'Pipkin' ;
Output:
Hi, my name is Anthony Pipkin.
Formatted String (en-US)
Template:
var tmpl = '<p>{@intlMessage _msg=POP_MSG city=city population=population census_date=census_date timeZone=timeZone/}</p>' ctx = POP_MSG: '{city} has a population of {population, number, integer} as of {census_date, date, medium}.' city: 'Atlanta' population: 5475213 census_date: '1/1/2010' timeZone: 'UTC' ;
Output:
Atlanta has a population of 5,475,213 as of Jan 1, 2010.
Formatted String (de-DE)
Template:
var tmpl = '<p>{@intlMessage _msg=POP_MSG locales="de-DE" city=city population=population census_date=census_date timeZone=timeZone/}</p>' ctx = POP_MSG: '{city} has a population of {population, number, integer} as of {census_date, date, medium}.' city: 'Atlanta' population: 5475213 census_date: '1/1/2010' timeZone: 'UTC' ;
Output:
Atlanta has a population of 5.475.213 as of 1. Jan. 2010.
String plurals
Template:
var tmpl = '<p>{@intlMessage _msg=HARVEST_MSG person=person count=count /}</p>' ctx = HARVEST_MSG: '{person} harvested {count, plural, one {# apple} other {# apples}}.' person: 'Allison' count: 10 ;
Output:
Allison harvested 10 apples.
Template:
var tmpl = '<p>{@intlMessage _msg=HARVEST_MSG person=person count=count /}</p>' ctx = HARVEST_MSG: '{person} harvested {count, plural, one {# apple} other {# apples}}.' person: 'Jeremy' count: 1 ;
Output:
Jeremy harvested 1 apple.
License
This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.