How to use i18next - 10 common examples

To help you get started, we’ve selected a few i18next examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
)

// with only callback
i18next.init((err: any, t: i18next.TranslationFunction) => {
  if (err) {
    console.log('something went wrong loading', err)
    return
  }
  t('key') // -> same as i18next.t
})

const v: string = i18next.t('my.key')
const a: boolean = i18next.exists('my.key')

// fix language to german
const de = i18next.getFixedT('de')
const z: string = de('myKey')

// or fix the namespace to anotherNamespace
const anotherNamespace = i18next.getFixedT(null, 'anotherNamespace')
const x: string = anotherNamespace('anotherNamespaceKey') // no need to prefix ns i18n.t('anotherNamespace:anotherNamespaceKey');

i18next.changeLanguage('en', (err: any, t: i18next.TranslationFunction) => {
  if (err) {
    console.log('something went wrong loading', err)
    return
  }
  t('key') // -> same as i18next.t
})

i18next.loadNamespaces('myNamespace', (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
(err: any, t: i18next.TranslationFunction) => {
    if (err) {
      console.log('something went wrong loading', err)
      return
    }
    t('key') // -> same as i18next.t
  },
)

i18next.on('initialized', options => {})
i18next.on('loaded', loaded => {})
i18next.on('failedLoading', (lng: string, ns: string, msg: string) => {})
i18next.on('missingKey', (lngs: string[], namespace: string, key: string, res: string) => {})
i18next.on('added', (lng: string, ns: string) => {})
i18next.on('removed', (lng: string, ns: string) => {})
i18next.on('languageChanged', (lng: string) => {})
i18next.on('customEvent', () => {})

i18next.getResource('en', 'test', 'key')
i18next.getResource('en', 'test', 'key', { keySeparator: '-' })

i18next.addResource('en', 'test', 'key', 'value')
i18next.addResource('en', 'test', 'key', 'value', {
  keySeparator: '-',
  silent: false,
})

i18next.addResources('en', 'test', { key: 'value' })

i18next.addResourceBundle(
  'en',
  'translations',
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
},
  (err: any, t: i18next.TranslationFunction) => {
    if (err) {
      console.log('something went wrong loading', err)
      return
    }
    t('key') // -> same as i18next.t
  },
)

i18next.on('initialized', options => {})
i18next.on('loaded', loaded => {})
i18next.on('failedLoading', (lng: string, ns: string, msg: string) => {})
i18next.on('missingKey', (lngs: string[], namespace: string, key: string, res: string) => {})
i18next.on('added', (lng: string, ns: string) => {})
i18next.on('removed', (lng: string, ns: string) => {})
i18next.on('languageChanged', (lng: string) => {})
i18next.on('customEvent', () => {})

i18next.getResource('en', 'test', 'key')
i18next.getResource('en', 'test', 'key', { keySeparator: '-' })

i18next.addResource('en', 'test', 'key', 'value')
i18next.addResource('en', 'test', 'key', 'value', {
  keySeparator: '-',
  silent: false,
})

i18next.addResources('en', 'test', { key: 'value' })

i18next.addResourceBundle(
  'en',
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
if (err) {
    console.log('something went wrong loading', err)
    return
  }
  t('key') // -> same as i18next.t
})

const v: string = i18next.t('my.key')
const a: boolean = i18next.exists('my.key')

// fix language to german
const de = i18next.getFixedT('de')
const z: string = de('myKey')

// or fix the namespace to anotherNamespace
const anotherNamespace = i18next.getFixedT(null, 'anotherNamespace')
const x: string = anotherNamespace('anotherNamespaceKey') // no need to prefix ns i18n.t('anotherNamespace:anotherNamespaceKey');

i18next.changeLanguage('en', (err: any, t: i18next.TranslationFunction) => {
  if (err) {
    console.log('something went wrong loading', err)
    return
  }
  t('key') // -> same as i18next.t
})

i18next.loadNamespaces('myNamespace', (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})
i18next.loadNamespaces(
  ['myNamespace1', 'myNamespace2'],
  (err: any, t: i18next.TranslationFunction) => {
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
// or fix the namespace to anotherNamespace
const anotherNamespace = i18next.getFixedT(null, 'anotherNamespace')
const x: string = anotherNamespace('anotherNamespaceKey') // no need to prefix ns i18n.t('anotherNamespace:anotherNamespaceKey');

i18next.changeLanguage('en', (err: any, t: i18next.TranslationFunction) => {
  if (err) {
    console.log('something went wrong loading', err)
    return
  }
  t('key') // -> same as i18next.t
})

i18next.loadNamespaces('myNamespace', (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})
i18next.loadNamespaces(
  ['myNamespace1', 'myNamespace2'],
  (err: any, t: i18next.TranslationFunction) => {
    /* resources have been loaded */
  },
)

i18next.loadLanguages('de', (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})
i18next.loadLanguages(['de', 'fr'], (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})

// reload all
i18next.reloadResources()
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
updateContent()
  },
)

i18next.init(
  {
    ns: ['common', 'moduleA', 'moduleB'],
    defaultNS: 'moduleA',
  },
  (err: any, t: i18next.TranslationFunction) => {
    i18next.t('myKey') // key in moduleA namespace (defined default)
    i18next.t('common:myKey') // key in common namespace
  },
)

i18next.loadNamespaces('anotherNamespace', (err: any, t: i18next.TranslationFunction) => {
  /* ... */
})

// fallback to one language
i18next.init(
  {
    lng: 'en-GB',
  },
  () => {
    i18next.t('i18n') // -> "Internationalisation"
    i18next.t('i18n_short') // -> "i18n" (from en.json)

    // force loading en
    i18next.t('i18n', { lng: 'en' }) // -> "Internationalization"
  },
)
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
const de = i18next.getFixedT('de')
const z: string = de('myKey')

// or fix the namespace to anotherNamespace
const anotherNamespace = i18next.getFixedT(null, 'anotherNamespace')
const x: string = anotherNamespace('anotherNamespaceKey') // no need to prefix ns i18n.t('anotherNamespace:anotherNamespaceKey');

i18next.changeLanguage('en', (err: any, t: i18next.TranslationFunction) => {
  if (err) {
    console.log('something went wrong loading', err)
    return
  }
  t('key') // -> same as i18next.t
})

i18next.loadNamespaces('myNamespace', (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})
i18next.loadNamespaces(
  ['myNamespace1', 'myNamespace2'],
  (err: any, t: i18next.TranslationFunction) => {
    /* resources have been loaded */
  },
)

i18next.loadLanguages('de', (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})
i18next.loadLanguages(['de', 'fr'], (err: any, t: i18next.TranslationFunction) => {
  /* resources have been loaded */
})
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
// reload languages
i18next.reloadResources(['de', 'fr'])

// reload namespaces for all languages
i18next.reloadResources(null, ['ns1', 'ns2'])

// reload namespaces in languages
i18next.reloadResources(['de', 'fr'], ['ns1', 'ns2'])

// for current language
i18next.dir()

// for another language
i18next.dir('en-US') // -> "ltr";
i18next.dir('ar') // -> "rtl";

// key = 'hello {{what}}'
i18next.t('key', { what: i18next.format('world', 'uppercase') }) // -> hello WORLD

const newInstance = i18next.createInstance(
  {
    fallbackLng: 'en',
    ns: ['file1', 'file2'],
    defaultNS: 'file1',
    debug: true,
  },
  (err: any, t: i18next.TranslationFunction) => {
    if (err) {
      console.log('something went wrong loading', err)
      return
    }
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
i18next.reloadResources()

// reload languages
i18next.reloadResources(['de', 'fr'])

// reload namespaces for all languages
i18next.reloadResources(null, ['ns1', 'ns2'])

// reload namespaces in languages
i18next.reloadResources(['de', 'fr'], ['ns1', 'ns2'])

// for current language
i18next.dir()

// for another language
i18next.dir('en-US') // -> "ltr";
i18next.dir('ar') // -> "rtl";

// key = 'hello {{what}}'
i18next.t('key', { what: i18next.format('world', 'uppercase') }) // -> hello WORLD

const newInstance = i18next.createInstance(
  {
    fallbackLng: 'en',
    ns: ['file1', 'file2'],
    defaultNS: 'file1',
    debug: true,
  },
  (err: any, t: i18next.TranslationFunction) => {
    if (err) {
      console.log('something went wrong loading', err)
      return
github i18next / i18next / test / typescript / i18next-tests.ts View on Github external
const t2: i18next.TranslationFunction<{ value: string }> = (
  key: string|string[],
  options?: i18next.TranslationOptions,
) => ({ value: 'asd' })
const t3: i18next.TranslationFunction = (
  key: string | string[],
  options?: i18next.TranslationOptions,
) => ''
const t4: i18next.TranslationFunction = (
  key: KeyList | KeyList[],
  options?: i18next.TranslationOptions,
) => ''

i18next.exists('friend');
i18next.exists(['friend', 'tree']);
i18next.exists('friend', { myVar: 'someValue' });
i18next.exists(['friend', 'tree'], { myVar: 'someValue' });

i18next.t("friend", { myVar: "someValue" });
i18next.t(["friend", "tree"], { myVar: "someValue" });
i18next.t("friend", { myVar: "someValue" });
i18next.t(["friend", "tree"], { myVar: "someValue" });

// NOTION: disable no-unnecessary-generics for generic pattern test.
/* tslint:disable:no-unnecessary-generics */
interface ExWithT extends i18next.WithT {
  t(keys: Keys|Keys[], options?: i18next.TranslationOptions): R;
  t(keys: Keys|Keys[], options?: i18next.TranslationOptions): R;
  t(keys: Keys|Keys[]): R;
}

type OtherKeyList = "private" | "public";