Skip to content

LLM Resources

@clippium/i18n - API documentation โ€‹

Functions โ€‹

i18n() โ€‹

ts
function i18n<Opts>(opts: Opts): Promise<{
  changeLang: (lang: Lang) => Promise<boolean>;
  getCurrentLang: () => Lang;
  getCurrentLocales: () => Promise<{}>;
  getLangs: () => Lang[];
  getLocales: (lang?: Lang) => Promise<{}>;
  t: (value: TParam, opts?: string | TOptionsBase & $Dictionary | TOptionsBase & $Dictionary & {}) => string;
}>

Initializes the i18n library with the provided options and returns an i18n object with helper methods.

Type Parameters โ€‹

Type Parameter
Opts extends I18nOpts

Parameters โ€‹

ParameterTypeDescription
optsOptsOptions for configuring i18n.

Returns โ€‹

Promise<{ changeLang: (lang: Lang) => Promise<boolean>; getCurrentLang: () => Lang; getCurrentLocales: () => Promise<{}>; getLangs: () => Lang[]; getLocales: (lang?: Lang) => Promise<{}>; t: (value: TParam, opts?: string | TOptionsBase & $Dictionary | TOptionsBase & $Dictionary & {}) => string; }>

  • An object with helper methods for managing localization.
NameTypeDescription
changeLang(lang: Lang) => Promise<boolean>Change language. Example const { changeLanguage } = yourI18nInstance // change lang to spanish const isChanged = changeLang('es')
getCurrentLang() => LangRetrieves the ID of the current locale. Example const { getCurrentLang } = await i18n({ // your config }) const currentLang = getCurrentLang(); console.log(currentLang) // Returns for example: 'es'
getCurrentLocales() => Promise<{}>Retrieves the object with the current language translations. Example const { getCurrentLocales } = await i18n({ // your config }) const locales = await getCurrentLocales(); console.log(locales)
getLangs() => Lang[]Retrieves an array of available locales. Example const { getLangs } = await i18n({ // your config }) const appLocales = getLangs() console.log(appLocales) // Returns for example: ['en', 'es', 'de']
getLocales(lang?: Lang) => Promise<{}>Retrieves the object with the current language translations. Example const { getLocales } = await i18n({ // your config }) const locales = await getLocales(); console.log(locales)
t(value: TParam, opts?: string | TOptionsBase & $Dictionary | TOptionsBase & $Dictionary & {}) => stringTranslates a given string key to the current locale. Example const { t } = await i18n({ // your config }) console.log( t('general:greet') )

Example โ€‹

ts
import { i18n } from '@clippium/i18n'

const I18N = await i18n({
  locales: {
    en: { general: { greet: '๐Ÿ‡ฌ๐Ÿ‡ง Hello pigeon' } },
    es: { general: { greet: '๐Ÿ‡ช๐Ÿ‡ธ Hola paloma' } }
  },
});

const currentLang = I18N.getCurrentLang();
const availableLangs = I18N.getLangs();
const translatedString = I18N._('general:greet');

console.log(
  currentLang,
  availableLangs,  // returns ['es', 'en']
  translatedString
)

// change lang to spanish
I18N.changeLang('es')

console.log(I18N.getCurrentLang()) // returns 'es'

Type Aliases โ€‹

I18nOpts โ€‹

ts
type I18nOpts: {
  defaultLocale: string;
  locales: {};
};

Options for configuring i18n.

Type declaration โ€‹

NameTypeDescription
defaultLocale?stringDefault language. If not set, gets the system language and takes the locales object's first language as the fallback language.
locales{}Set your translations. Example const locales = { en: { general: { greet: '๐Ÿ‡ฌ๐Ÿ‡ง Hello pigeon' } }, es: { general: { greet: '๐Ÿ‡ช๐Ÿ‡ธ Hola paloma' } } }