本文整理汇总了TypeScript中i18next.format函数的典型用法代码示例。如果您正苦于以下问题:TypeScript format函数的具体用法?TypeScript format怎么用?TypeScript format使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了format函数的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: t
// 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
示例2: interpolation
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 <img />"
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"
}