UNPKG

@rockpack/localazer

Version:

This module can help you organize localization in your React application

21 lines (20 loc) 751 B
import Jed from 'jed'; import { isValidElement } from 'react'; import { renderToStaticMarkup } from 'react-dom/server'; export const i18n = new Jed({ domain: 'messages' }); const l = (text, context) => () => (context ? i18n.pgettext(context, text) : i18n.gettext(text)); const nl = (singular, plural, amount, context) => () => (context ? i18n.npgettext(context, singular, plural, amount) : i18n.ngettext(singular, plural, amount)); const sprintf = (...args) => () => (i18n.sprintf.apply(i18n, args.map(item => { if (isValidElement(item)) { return renderToStaticMarkup(item); } else if (typeof item === 'function') { return item(); } return item; }))); export { l, nl, sprintf };