@itomo/locale-nestjs
v0.1.8
Published
NestJS module for @itomo/locale-core: dependency-injected service, validation pipes, and decorators.
Maintainers
Readme
@itomo/locale-nestjs
NestJS integration for @itomo/locale-core with a DI-friendly service,
validation pipes, and decorators.
Install
pnpm add @itomo/locale-core @itomo/locale-nestjs
pnpm add @nestjs/common @nestjs/core reflect-metadata rxjsOptional for class-validator decorators:
pnpm add class-validatorQuick start
import { Module } from "@nestjs/common";
import { LocaleModule } from "@itomo/locale-nestjs";
@Module({
imports: [LocaleModule.forRoot({ preloadLocales: ["en", "vi"] })],
})
export class AppModule {}import { Controller, Get } from "@nestjs/common";
import { LocaleService, IsCountryAlpha2 } from "@itomo/locale-nestjs";
@Controller("countries")
export class CountriesController {
constructor(private readonly locale: LocaleService) {}
@Get(":code")
get(@IsCountryAlpha2("code") code: string) {
return this.locale.getCountryByCca2(code);
}
}Module API
LocaleModule.forRoot(options)LocaleModule.forRootAsync(options)LOCALE_MODULE_OPTIONSinjection token
LocaleModuleOptions:
global?: boolean(defaulttrue)defaultLocale?: string(defaulten-US)preloadLocales?: readonly string[]
LocaleService API
Countries
- Lookups:
getCountryByCca2(code),getCountryByCca3(code),getCountryByCioc(code),getCountryByNumeric(code),getCountryByAnyCode(code) - Lists and filters:
getAllCountries(),getIndependentCountries(),getUNMemberCountries(),getCountriesByRegion(region),getCountriesByContinent(continent),getCountriesByLanguage(iso6393),getCountriesByCurrency(alpha3),getCountriesByTimezone(tzId),getCountriesByCallingCode(code),getCountriesByRegionalBloc(bloc),getBorderingCountries(cca3) - Names and search:
searchCountries(query, locale?),getCountryName(cca2, locale),getCountryOfficialName(cca2, locale),getCountryNativeName(cca2, iso6393?) - Subdivisions:
getSubdivisions(cca2),getSubdivisionByCode(cca2, code),getSubdivisionsByType(cca2, type) - Localized helpers:
getAllCountriesLocalized(locale),searchCountriesLocalized(query, locale)
Currencies
- Lookups:
getCurrencyByAlpha3(code),getCurrencyByNumeric(code),getCurrencyByAnyCode(code) - Lists and filters:
getAllCurrencies(),getActiveCurrencies(),getDeprecatedCurrencies(),getCurrenciesByType(type),getCurrenciesByCountry(cca2) - Names, search, format:
searchCurrencies(query),formatAmount(amount, alpha3, locale),getCurrencySymbol(alpha3, locale?),getCurrencyName(alpha3, locale) - Localized helpers:
getAllCurrenciesLocalized(locale),searchCurrenciesLocalized(query, locale)
Languages
- Lookups:
getLanguageByIso6391(code),getLanguageByIso6392b(code),getLanguageByIso6392t(code),getLanguageByIso6393(code),getLanguageByAnyCode(code) - Lists and filters:
getAllLanguages(),getLivingLanguages(),getRtlLanguages(),getLanguagesByFamily(family),getLanguagesByCountry(cca2),getLanguagesByScript(iso15924) - Names and search:
searchLanguages(query, locale?),getLanguageName(iso6393, locale) - Localized helpers:
getAllLanguagesLocalized(locale),searchLanguagesLocalized(query, locale)
Scripts
getAllScripts()getScriptByCode(code)
Timezones
- Lookups and filters:
getAllTimezones(),getTimezoneById(id),getTimezonesByCountry(cca2),getTimezonesByUtcOffset(offset),getTimezonesByRegion(region),getCanonicalTimezones() - Search and utilities:
searchTimezones(query),getCurrentUtcOffset(tzId),isDaylightSavingActive(tzId),reinterpretInTimezone(date, fromTz, toTz) - Localized helpers:
getTimezoneName(tzId, locale),getAllTimezonesLocalized(locale),searchTimezonesLocalized(query, locale)
i18n and locale helpers
- Translation lifecycle:
loadTranslation(locale),getSupportedTranslationLocales(),getSupportedLanguages(),isLocaleSupported(locale) - Locale tags:
buildLocaleTag(language, script?, region?),parseLocaleTag(tag) - Locale resolution:
getCountryFromLocale(locale),getLanguageFromLocale(locale) - Phone and direction:
getPhoneCode(cca2),parsePhonePrefix(phone),getTextDirection(iso6391OrLocale),isRtlLanguage(iso6391OrLocale)
Validation helpers
Pipes
CountryAlpha2ValidationPipeCurrencyAlpha3ValidationPipe
Parameter decorators
IsCountryAlpha2(property = "code")IsCurrencyAlpha3(property = "code")
class-validator decorators
IsCountryCode(kind = "alpha2")IsCurrencyCode()IsTimezone()
License
MIT © iTomo Vietnam
