Chapter 10
Where the locale.js file should contain a module definition with a dependency on
the ngLocale module:
angular.module('locale', ['ngLocale'])
Making use of available locale settings
Each and every localization file distributed with AngularJS contains definition of a
module named ngLocale. The ngLocale module exposes only one service: $locale.
The only documented, public API of the $locale service consists of the $locale.id
variable, which can be used to retrieve a currently used locale.
In reality the $locale service exposes much more constants for a given locale.
Among the exposed constants we can find ones holding date and time formats
($locale. DATETIMEFORMATS), as well as the number formats ($locale.NUMBER
FORMATS). The mentioned constants are JavaScript objects containing settings one
might need to format dates, time, number and currencies. For example, we can find
all the month names by inspecting $locale.DATETIME_FORMATS.MONTH.
Locale-specific settings and AngularJS filters
Having access to all the localization settings might come handy when writing custom
directives and filters, but AngularJS already makes good use of those settings in the
build in filters.
Date filter
The date filter converts dates according to a specified format. A target format can
be specified either in a precise, locale-independent manner (For example, mm/dd/
yy hh:mm a) or in a locale-dependent one. In the second case, instead of precisely
enumerating each character of a desired format, we can use predefined, named ones.
AngularJS understands the following names for predefined formats: medium, short,
fullDate, longDate, mediumDate, shortDate, mediumTime, shortTime.
As an example, let's consider the {{now | date:'fullDate'}} expression (where
now is initialized to new Date()), that will produce different results depending on
a locale:
- Tuesday, April 9, 2013 in en-us(English locale in United States)
- mardi 9 avril 2013 in fr-fr (French locale in France)