Exemplo n.º 1
0
// 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;
    }
    t('key'); // -> same as i18next.t
});

// is the same as
Exemplo n.º 2
0
function interpolation(t: i18next.TFunction) {
  // key = 'hello {{what}}'
  t('key', { what: i18next.format('world', 'uppercase') }); // -> hello WORLD

  t('key', { what: 'i18next', how: 'great' });

  const author = {
    name: 'Jan',
    github: 'jamuhl',
  };
  t('key', { author });

  t('keyEscaped', { myVar: '<img />' });
  // -> "no danger &lt;img &#x2F;&gt;"

  t('keyUnescaped', { myVar: '<img />' });
  // -> "dangerous <img />"

  t('keyEscaped', {
    myVar: '<img />',
    interpolation: { escapeValue: false },
  });
  // -> "no danger <img />" (obviously could be dangerous)

  t('key', { count: 0 }); // -> "items"
  t('key', { count: 1 }); // -> "item"
  t('key', { count: 5 }); // -> "items"
  t('key', { count: 100 }); // -> "items"
  t('keyWithCount', { count: 0 }); // -> "0 items"
  t('keyWithCount', { count: 1 }); // -> "1 item"
  t('keyWithCount', { count: 5 }); // -> "5 items"
  t('keyWithCount', { count: 100 }); // -> "100 items"

  t('key1_interval', { postProcess: 'interval', count: 1 }); // -> "one item"
  t('key1_interval', { postProcess: 'interval', count: 4 }); // -> "a few items"
  t('key1_interval', { postProcess: 'interval', count: 100 }); // -> "a lot of items"

  // not matching into a range it will fallback to
  // the regular plural form
  t('key2_interval', { postProcess: 'interval', count: 1 }); // -> "one item"
  t('key2_interval', { postProcess: 'interval', count: 4 }); // -> "a few items"
  t('key2_interval', { postProcess: 'interval', count: 100 }); // -> "100 items"
  t('friend', { context: 'male', count: 1 }); // -> "A boyfriend"
  t('friend', { context: 'female', count: 100 }); // -> "100 girlfriends"
  t('tree', { returnObjects: true, something: 'gold' });
  // -> { res: 'added gold' }

  t('array', { returnObjects: true });
  // -> ['a', 'b', 'c']
  t('arrayJoin', { joinArrays: '+' });
  // -> "line1+line2+line3"

  t('arrayJoinWithInterpolation', {
    myVar: 'interpolate',
    joinArrays: ' ',
  });
  // -> "you can interpolate"

  t('arrayOfObjects.0.name');
  // -> "tom"
}