I need to use react-int outside a component and particularly inside an util file. In order to accomplish that i'am using this code https://gist.github.com/genadyp/435f4e264cb6e377836cf63bee8987d8 But i am facing an issue with eslint that fails and it does not accept using require inside a function and using a dynamic file path too.
here is eslint output:
error Unexpected require() global-require error Calls to require() should use string literals import/no-dynamic-require
Any advice and suggestions will be greatly appreciated.
//util.js
export function formatMessage(t, locale) {
if (t=== 0 || t === 2400) {
const translations
=require(`src/locales/${locale.toLowerCase()}.json`));
const intlProvider = new IntlProvider({ locale, messages:
translations }, {});
const mes = defineMessages({
morning: {
id: 'greeting.morning',
defaultMessage: 'hello',
},
evening: {
id: 'greeting.evening',
defaultMessage: 'good evening',
},
});
const { intl } = intlProvider.getChildContext();
return t === 0 ? intl.formatMessage(mes.morning) :
intl.formatMessage(mes.evening);
}
}