Skip to main content

@rtorcato/js-common / i18n

i18n

Functions

detectLanguage()

detectLanguage(defaultLang?): string

Defined in: i18n/index.ts:5

Detects the user's preferred language from the browser or Node.js environment.

Parameters

defaultLang?

string = 'en'

Returns

string

The language code (e.g., 'en', 'fr').


formatNumber()

formatNumber(value, locale?, options?): string

Defined in: i18n/index.ts:22

Formats a number as a localized string.

Parameters

value

number

The number to format.

locale?

string

The locale code (e.g., 'en-US').

options?

NumberFormatOptions

Intl.NumberFormat options.

Returns

string


formatDateI18n()

formatDateI18n(date, locale?, options?): string

Defined in: i18n/index.ts:37

Formats a date as a localized string.

Parameters

date

Date

The date to format.

locale?

string

The locale code (e.g., 'en-US').

options?

DateTimeFormatOptions

Intl.DateTimeFormat options.

Returns

string


t()

t(key, dict, lang?): string

Defined in: i18n/index.ts:52

Simple translation function using a dictionary object.

Parameters

key

string

The translation key.

dict

Record<string, Record<string, string>>

The dictionary object (e.g., { en: { hello: 'Hello' }, fr: { hello: 'Bonjour' } }).

lang?

string

The language code (optional, defaults to detected language).

Returns

string


pluralize()

pluralize(word, count): string

Defined in: i18n/index.ts:67

Pluralizes a word based on a count (basic English only).

Parameters

word

string

The word to pluralize.

count

number

The count.

Returns

string