Modules@robinpath/i18n
i18n

@robinpath/i18n

0.1.1Node.jsPublic

Internationalization: translations, number/currency/date formatting, relative time, pluralization, RTL detection

i18n

Internationalization: translations, number/currency/date formatting, relative time, pluralization, RTL detection

Package: @robinpath/i18n | Category: Other | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the i18n module when you need to:

  • Get current locale -- Use i18n.getLocale to perform this operation
  • Load translations for a locale -- Use i18n.loadTranslations to perform this operation
  • Translate a key -- Use i18n.t to perform this operation
  • Format number for locale -- Use i18n.formatNumber to perform this operation
  • Format currency -- Use i18n.formatCurrency to perform this operation

Quick Reference

FunctionDescriptionReturns
setLocaleSet default localeLocale
getLocaleGet current localeCurrent locale
loadTranslationsLoad translations for a locale{locale, keys}
tTranslate a keyTranslated string
formatNumberFormat number for localeFormatted number
formatCurrencyFormat currencyFormatted currency
formatDateFormat date for localeFormatted date
formatRelativeTimeFormat relative timeRelative time string
formatListFormat list (A, B, and C)Formatted list
pluralizeSimple pluralizationCorrect form
directionGet text direction for localeltr or rtl
listLocalesList loaded translation localesLocale codes
hasTranslationCheck if key existstrue if exists
languageNameGet language display nameLanguage name
regionNameGet region display nameRegion name

Functions

setLocale

Set default locale

Module: i18n | Returns: string -- Locale

i18n.setLocale "de-DE"
ParameterTypeRequiredDescription
localestringYesLocale code

getLocale

Get current locale

Module: i18n | Returns: string -- Current locale

i18n.getLocale
ParameterTypeRequiredDescription
(none)NoCall with no arguments

loadTranslations

Load translations for a locale

Module: i18n | Returns: object -- {locale, keys}

i18n.loadTranslations "es" {"hello": "Hola", "bye": "Adiós"}
ParameterTypeRequiredDescription
localestringYesLocale code
stringsobjectYesKey-value translations

t

Translate a key

Module: i18n | Returns: string -- Translated string

i18n.t "hello" "es"
ParameterTypeRequiredDescription
keystringYesTranslation key
localestringNoOverride locale
interpolationobjectNoVariables for {{var}}

formatNumber

Format number for locale

Module: i18n | Returns: string -- Formatted number

i18n.formatNumber 1234567.89 "de-DE"
ParameterTypeRequiredDescription
numbernumberYesNumber
localestringNoLocale
optionsobjectNoIntl.NumberFormat options

formatCurrency

Format currency

Module: i18n | Returns: string -- Formatted currency

i18n.formatCurrency 99.99 "EUR" "de-DE"
ParameterTypeRequiredDescription
amountnumberYesAmount
currencystringYesCurrency code
localestringNoLocale

formatDate

Format date for locale

Module: i18n | Returns: string -- Formatted date

i18n.formatDate "2024-01-15" "ja-JP"
ParameterTypeRequiredDescription
datestringYesISO date string
localestringNoLocale
optionsobjectNo{dateStyle, timeStyle}

formatRelativeTime

Format relative time

Module: i18n | Returns: string -- Relative time string

i18n.formatRelativeTime -3 "day"
ParameterTypeRequiredDescription
valuenumberYesValue (negative=past)
unitstringYessecond
localestringNoLocale

formatList

Format list (A, B, and C)

Module: i18n | Returns: string -- Formatted list

i18n.formatList ["Alice", "Bob", "Charlie"]
ParameterTypeRequiredDescription
itemsarrayYesItems
localestringNoLocale
optionsobjectNo{type: conjunction

pluralize

Simple pluralization

Module: i18n | Returns: string -- Correct form

i18n.pluralize 5 "item" "items"
ParameterTypeRequiredDescription
countnumberYesCount
singularstringYesSingular form
pluralstringNoPlural form

direction

Get text direction for locale

Module: i18n | Returns: string -- ltr or rtl

i18n.direction "ar"
ParameterTypeRequiredDescription
localestringYesLocale

listLocales

List loaded translation locales

Module: i18n | Returns: array -- Locale codes

i18n.listLocales
ParameterTypeRequiredDescription
(none)NoCall with no arguments

hasTranslation

Check if key exists

Module: i18n | Returns: boolean -- true if exists

i18n.hasTranslation "hello" "es"
ParameterTypeRequiredDescription
keystringYesKey
localestringNoLocale

languageName

Get language display name

Module: i18n | Returns: string -- Language name

i18n.languageName "de" "en"
ParameterTypeRequiredDescription
localestringYesLanguage locale
displayLocalestringNoDisplay in this locale

regionName

Get region display name

Module: i18n | Returns: string -- Region name

i18n.regionName "JP" "en"
ParameterTypeRequiredDescription
regionCodestringYesRegion code (US, GB, etc)
localestringNoDisplay locale

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Get locale and validate result"
do
  set $result as i18n.getLocale
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate Locale

Retrieve all items and loop through them.

@desc "Get locale and iterate results"
do
  set $result as i18n.getLocale
  each $item in $result
    print $item
  end
enddo

2. Multi-step i18n workflow

Chain multiple i18n operations together.

@desc "Get locale, load translations, and more"
do
  set $r_getLocale as i18n.getLocale
  set $r_loadTranslations as i18n.loadTranslations "es" {"hello": "Hola", "bye": "Adiós"}
  set $r_t as i18n.t "hello" "es"
  print "All operations complete"
enddo

3. Safe getLocale with validation

Check results before proceeding.

@desc "Get locale and validate result"
do
  set $result as i18n.getLocale
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/i18n

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.4 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities