# API
# useI18nStore()
Returns i18nStore. You can use this function to have access to translations, current locale, setting the current locale.
Example:
import { useI18nStore } from '@pitcher/i18n'
const store = useI18nStore()
console.log(store)
/*
{ id: 'i18n',
state: {
availableLanguages: {
en: 'English'
}
locale: 'en'
messages: {
en: {}
}
},
setLanguage: Function
}
*/
# store.setLanguage()
Accepts a string as first param (e.g. en, de), sets the current locale in i18nStore. Second param is an options object, which might not need to use unless you customize the translations directory. This function is asynchronous.
// default function params
async setLanguage(lang, { app = 'app', dir = 'translations', load = true } = {}) {
...
}
Example:
import { useI18nStore } from '@pitcher/i18n'
// common usage
await i18n.setLanguage('de')
// load: false does not load any translation files but supports dates and number formatting
await i18n.setLanguage('tr', { load: false })
# useBrowserLanguage()
Returns 2 helper functions to get available languages which are getAvailableBrowserLanguage() and getBrowserLanguages().
getAvailableBrowserLanguage(availableLanguages: Array)- accepts an array of languages and returns available browser languagegetBrowserLanguages()- returns all available browser languages throughnavigator.languages
# formatDate()
Accepts a date as first param, returns a localized date string depending on the user locale.
NOTE
This function first checks user locale from paramsStore in @pitcher/core, if locale is not available in paramsStore, then it tries to get locale from i18nStore.
# params
| Param | Default | Type | Description |
|---|---|---|---|
| date | undefined | Date | date to format |
| showYear (optional) | true | Boolean | include year in return value |
Example:
import { formatDate } from '@pitcher/i18n'
const myDate = new Date()
const formattedDate = formatDate(myDate) // add false as second param for day and month only
# formatTime()
Accepts a date as first param, returns a localized time string depending on the user locale.
NOTE
This function first checks user locale from paramsStore in @pitcher/core, if locale is not available in paramsStore, then it tries to get locale from i18nStore.
# params
| Param | Default | Type | Description |
|---|---|---|---|
| date | undefined | Date | date to format |
| showSeconds | false | Boolean | include seconds in return value |
Example:
import { formatTime } from '@pitcher/i18n'
const myDate = new Date()
const formattedTime = formatTime(myDate) // add true as second param to include seconds
# formatDecimal()
Formats a number with minimum and maximum fraction digits, returns a localized string depending on the user locale.
NOTE
This function first checks user locale from paramsStore in @pitcher/core, if locale is not available in paramsStore, then it tries to get locale from i18nStore.
# params
| Param | Default | Type | Description |
|---|---|---|---|
| value | undefined | Number | number to format |
| maximumFractionDigits | 1 | Number | maximum fraction digits |
| minimumFractionDigits | 0 | Number | minimum fraction digits |
Example:
import { formatDecimal } from '@pitcher/i18n'
formatDecimal(123.456, 2, 0) // 123.46
formatDecimal(100, 2, 1) // 100.0
formatDecimal(100, 2, 0) // 100
# formatPercent()
Formats a number with minimum and maximum fraction digits, returns a localized string depending on the user locale.
NOTE
This function first checks user locale from paramsStore in @pitcher/core, if locale is not available in paramsStore, then it tries to get locale from i18nStore.
# params
| Param | Default | Type | Description |
|---|---|---|---|
| value | undefined | Number | number to format |
| maximumFractionDigits | 1 | Number | maximum fraction digits |
| minimumFractionDigits | 0 | Number | minimum fraction digits |
Example:
import { formatPercent } from '@pitcher/i18n'
formatPercent(123.456, 2, 0) // 123.46 %
formatPercent(100, 2, 1) // 100.0 %
formatPercent(100, 2, 0) // 100 %
# formatCurrency()
Formats a number into a localized currency, returns a localized string depending on the user locale.
NOTE
This function first checks user locale from paramsStore in @pitcher/core, if locale is not available in paramsStore, then it tries to get locale from i18nStore.
# params
| Param | Default | Type | Description |
|---|---|---|---|
| value | undefined | Number | number to format |
| currency | undefined | String | target currency e.g. EUR, CHF |
| currencyDisplay | symbol | symbol | name | code | how to display the currency in currency formatting |
Example:
import { formatCurrency } from '@pitcher/i18n'
formatCurrency(123.456, 'EUR') // € 123.46
formatCurrency(100000, 'CHF') // sFr. 10'0000.00
formatCurrency(123.456, 'TRY', 'symbol') // ₺123,46
formatCurrency(123.456, 'TRY', 'code') // TRY 123,46
formatCurrency(123.456, 'TRY', 'name') // 123,46 Türk lirası
← Scripts