Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
@ngneat/transloco-locale
Advanced tools
The localization (l10n) library plugin for Angular and Transloco
This plugin provides localization(l10n) support for Transloco.
Localization refers to the adaptation of a product, application or document content to meet the language, cultural and other requirements of a specific target market (a locale).
npm i @ngneat/transloco-locale
Inject TranslocoLocaleModule
along with TranslocoModule
into AppModule
:
import { TranslocoLocaleModule } from '@ngneat/transloco-locale';
@NgModule({
imports: [TranslocoModule, TranslocoLocaleModule.init()],
bootstrap: [AppComponent]
})
export class AppModule {}
The library provides localization pipes base on the native Javascript's API.
Transform a date into the locale's date format.
The date expression could be: a Date
object, a number
(milliseconds since UTC epoch), or an ISO string.
<!--9/10/2019-->
<span> {{ date | translocoDate }} </span>
<!-- Sep 10, 2019, 10:46:12 PM-->
<span>
{{ date | translocoDate: { dateStyle: 'medium', timeStyle: 'medium' }}
</span>
<!-- 7:40:32 PM Coordinated-->
<span>
{{ date | translocoDate: { timeZone: 'UTC', timeStyle: 'full' } }}
</span>
<!-- Jan 1, 1970-->
<span>
{{ 1 | translocoDate: { dateStyle: 'medium' } }}
</span>
<!-- Feb 8, 2019-->
<span>
{{ '2019-02-08' | translocoDate: { dateStyle: 'medium' } }}
</span>
Transform a given number into the locale's currency format.
The library comes out of the box with locale currency mapping, so once the locale is change the currency will automatically display the right currency.
The currency mapping could be customise if needed through the config, and could be provided by LOCALE_CURRENCY_MAPPING
token.
<!--$1,000,000.00-->
<span> {{ 1000000 | translocoCurrency }} </span>
<!--1,000,000.00 US dollars-->
<span>
{{ 1000000 | translocoCurrency: 'name' }}
</span>
<!--$1,000,000-->
<span>
{{ 1000000 | translocoCurrency: 'symbol' : { minimumFractionDigits: 0 } }}
</span>
Transform a given number into current locale's decimal number format.
<!--1,234,567,890-->
<span>
{{ 1234567890 | translocoDecimal }}
</span>
<!--1234567890-->
<span>
{{ 1234567890 | translocoDecimal: {useGrouping: false} }}
</span>
Transform a given number into current locale's percent number format.
<!--100%-->
<span> 1 | translocoPercent </span>
<!--100%-->
<span> "1" | translocoPercent </span>
The library provides three different ways to set the locale.
Using locale format for the translation files will automatically declare the locale on langChanges$
event:
├─ i18n/
├─ en-US.json
├─ en-GB.json
├─ es-ES.json
Users who don't have more than one locale per language
could provide a language to locale mapping object using the config's langToLocaleMapping
:
@NgModule({
imports: [
TranslocoLocaleModule.init({
langToLocaleMapping: {
en: 'en-US',
es: 'es-ES'
}
})
],
bootstrap: [AppComponent]
})
export class AppModule {}
The third option in manually setting the locale, this could be done by calling setLocale
method from localeService
:
export class AppComponent {
constructor(private service: TranslocoLocaleService) {}
ngOnInit() {
this.service.setLocale('en-US');
}
}
Let's go over each one of the config
options:
localeConfig?
: Declare the default configuration of the locale's formatting. A general configuration could be set using the global
property, for a configuration by locale use localeBased
property (default value determine by the native Javascript's API).defaultLocale?
: The default locale formatted in BCP 47 (default value: en-US
),langToLocaleMapping?
: A key value object
that maps Transloco language to it's Locale (default value: {}
).localeToCurrencyMapping?
: A key value object
that maps the Locale to it's currency (formatted in ISO 4217) (the library provide a default value with all of the existing mapping).There are two types of formatting options, one for date
and one for number
.
The formatted options could be declared in three levels
import { TranslocoLocaleModule } from '@ngneat/transloco-locale';
const globalFormatConfig = {
date: {
dateStyle: 'long',
timeStyle: 'long'
}
};
const esESFormatConfig = {
date: {
timeStyle: 'medium'
},
currency: {
minimumFractionDigits: 0
}
};
@NgModule({
imports: [
TranslocoLocaleModule.init({
localeConfig: {
global: globalFormatConfig,
localeBased: {
'es-ES': esESFormatConfig
}
}
})
]
})
export class AppModule {}
LOCALE_CONFIG
token:@Component({
selector: 'my-comp',
templateUrl: './my-comp.component.html',
providers: [
{
provide: LOCALE_CONFIG,
useValue: localeConfig
}
]
})
export class MyComponent {}
<span>
{{ date | translocoDate: { dateStyle: 'medium', timeStyle: 'medium' }}
</span>
<span>
{{ number | translocoDecimal: {useGrouping: false} }}
</span>
Note the format option of the global, locale's format and the one's being passed in the template, will be merged. While the template is the stronger one and then the locale and the global.
useGrouping
- Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. Possible values are true and false; the (default is true).minimumIntegerDigits
- The minimum number of integer digits to use. Possible values are from 1 to 21 (default is 1).minimumFractionDigits
- The minimum number of fraction digits to use. Possible values are from 0 to 20 (default is 0).maximumFractionDigits
- The maximum number of fraction digits to use. Possible values are from 0 to 20 (default is 3).minimumSignificantDigits
- The minimum number of significant digits to use. Possible values are from 1 to 21 (default is 1).maximumSignificantDigits
- The maximum number of significant digits to use. Possible values are from 1 to 21 (default is 21).dateStyle
- The date formatting style.timeStyle
- The time formatting style.timeZone
- The time zone to use. The only value implementations must recognize is "UTC"; the default is the runtime's default time zone. Implementations may also recognize the time zone names of the IANA time zone database, such as "Asia/Shanghai", "Asia/Kolkata", "America/New_York".localeChanges$
- Observable of the active locale.getLocale
- Gets the active locale.setLocale
- Sets the active locale.FAQs
The localization (l10n) library plugin for Transloco
The npm package @ngneat/transloco-locale receives a total of 7,682 weekly downloads. As such, @ngneat/transloco-locale popularity was classified as popular.
We found that @ngneat/transloco-locale demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.