Short Answer
It would seem that there is no way of doing this & in fact probably never will be for the built in Angular2 date
pipe.
Detailed Explanation
The Angular2 team has chosen to implement locale support by means of the Internationalization API as mentioned at the top of the date pipe source file:
WARNINGS:
- this pipe uses the Internationalization API. Therefore it is only reliable in Chrome and Opera browsers.
In theory this means that we should never need to worry about locale formatting because it will all be available via a specification controlled API provided directly by the browser.
In practice however we face the problem of having to worry about whether or not the browser supports all future locales you are ever likely to need in your application. The very first locale I tried (fr-CH
) did not work which lead me to post this question. It seems that Chrome currently only supports fr
(no dialects) as stated here (although this link is for Chrome apps I think it is safe to assume that this shares code with their Intl implementation)
Custom Pipe Alternative
Since there doesn't seem to be any allowance in the Internationalisation API for adding custom locales my best alternative is to implement a custom pipe using the moment library; based on the example take from this reddit post, courtesy of deadcorps3 (concept code only):
// Locale settings
let locale = getUserSelectedLanguage(); // retrieve from somewhere else
@Pipe({
name: 'i18nDate'
})
export class I18nDatePipe implements PipeTransform {
transform(value: string, format: string): string {
let momentDate = moment(new Date(value));
if (momentDate.isValid())
return momentDate.locale(locale).format(format);
else
return value;
}
}
Some Additional Information
- There is a polyfill for the Intl object here but I haven't looked into this & don't know if you can use it to override the default browser implementation. I am personally more interested in implementing something with the moment library - which I am already using in my projects - rather than start adding duplicate functionality.
- I also found this stackoverflow answer useful during my quest.