Intl
Created by Ion Prodan
This is a collection of pipes designed for Angular applications that leverage the Intl API.
While it’s not intended to fully replace Angular’s standard pipes for currency
, date
, and number
, it serves as a supplemental toolkit to enhance existing functionalities.
The default locale is determined by the LOCALE_ID
token. Altering this will change the locale for all included pipes.
Alternatively, you can specify the locale directly as the final parameter to any pipe, thereby overriding the default setting.
These pipes aim to utilize the Intl API. In cases where the Intl API is unavailable, the pipes will automatically resort to alternative methods, varying by the specific pipe in use.
For instance, DisplayNamesPipe
processes the code value passed into it, whereas ListFormatPipe
outputs the array values as a string.
Should an issue arise, an error message will be displayed in the console to provide further insights.
DisplayNamesPipe
Displays the localized display name of the given key for the given type.
For example, the display name of the language code “en” in English is “English”, and in German it’s “Englisch”.
Usage
Import DisplayNamesPipe
and add it to your component:
If LOCALE_ID
is set to en-US
, the above example will display English
, while if it’s set to de-DE
, it will display Englisch
.
Additionally, you can send the style and locale as parameters:
The above example will display Englisch
in German locale, no matter what LOCALE_ID
is set to.
Configuration
To explore additional configuration settings, you can customize the DisplayNamesPipe
by modifying the default options through the provideDisplayNamesOptions
provider:
The default options are:
For more information, check the DisplayNames API.
Error Handling
If an error occurs, the pipe will return the value that was sent to it, and a console error will be displayed with more details.
{{ 'any-nonvalid-value' | displayNames: 'language' }}
will return any-nonvalid-value
.
ListFormatPipe
Displays the localized string representation of a list of elements.
For example, the list ['en', 'fr', 'de']
will be displayed as en, fr, and de
in English, and en, fr et de
in French.
Usage
Import ListFormatPipe
and add it to your component:
If LOCALE_ID
is set to en-US
, the above example will display en, fr, and de
, while if it’s set to fr-FR
, it will display en, fr et de
.
Additionally, you can send the style and locale as parameters:
The above example will display en, fr et de
in French locale, no matter what LOCALE_ID
is set to.
Configuration
To explore additional configuration settings, you can customize the ListFormatPipe
by modifying the default options through the provideListFormatOptions
provider:
The default options are:
For more information, check the ListFormat API.
Error Handling
If an error occurs, the pipe will return the value of the array as string, and a console error will be displayed with more details.
{{ ['en', 'fr', 'de'] | listFormat }}
will return en, fr, de
.
PluralRulesPipe
Displays the localized string representation of a plural value.
For example, the value 1
will be displayed as one
in English, and uno
in Spanish.
Usage
Import PluralRulesPipe
and add it to your component:
If LOCALE_ID
is set to en-US
, the above example will display one
, while if it’s set to es-ES
, it will display uno
.
Additionally, you can send locale as last parameter:
The above example will display one
in English locale, no matter what LOCALE_ID
is set to.
Configuration
To explore additional configuration settings, you can customize the PluralRulesPipe
by modifying the default options through the providePluralRulesOptions
provider:
The default options are:
For more information, check the PluralRules API.
Error Handling
If an error occurs, the pipe will return the value that was sent to it, and a console error will be displayed with more details.
{{ 1 | pluralRules }}
will return 1
.
RelativeTimeFormatPipe
Displays the localized string representation of a relative time value.
For example, the value 1
will be displayed as in 1 day
in English, and dentro de 1 día
in Spanish.
Usage
Import RelativeTimeFormatPipe
and add it to your component:
If LOCALE_ID
is set to en-US
, the above example will display in 1 day
, while if it’s set to es-ES
, it will display dentro de 1 día
.
Additionally, you can send style and locale as parameters:
The above example will display dentro de 1 día
in Spanish locale, no matter what LOCALE_ID
is set to.
Configuration
To explore additional configuration settings, you can customize the RelativeTimeFormatPipe
by modifying the default options through the provideRelativeTimeFormatOptions
provider:
The default options are:
For more information, check the RelativeTimeFormat API.
Error Handling
If an error occurs, the pipe will return the value that was sent to it, and a console error will be displayed with more details.
{{ 1 | relativeTimeFormat: 'day' }}
will return 1
.
SupportedValuesOf
This is a utility transforms a key into an array containing the supported calendar, collation, currency, numbering systems, or unit values supported by the implementation.
Usage
Import SupportedValuesOf
and add it to your component:
The above example will display ["BRL", "CNY", "EUR", "GBP", "INR", "JPY", "KRW", "MXN", "RUB", "USD"]
if LOCALE_ID
is set to en-US
.
The key can be any of the following:
calendar
- Supported calendar values, such asbuddhist
,chinese
,coptic
, and so on.collation
- Supported collation values, such asbig5han
,compat
,ebase
,emoji
, and so on.currency
- Supported currency values, such asBRL
,CNY
,EUR
, and so on.numberingSystem
- Supported numbering system values, such asarab
,arabext
,bali
, and so on.timeZone
- Supported time zone values, such asAmerica/Los_Angeles
,Asia/Kolkata
,Asia/Tokyo
, and so on.unit
- Supported unit values, such asacre
,bit
,byte
,celsius
, and so on.
Not sure how often you’ll need this, but it’s there if you do.
For more information, check the SupportedValuesOf API.