Added mechanism for widgets to use built-in I18N

This commit is contained in:
Rainer Simon 2021-09-30 08:44:27 +02:00
parent 401d261a49
commit b0d07b0ef8
1 changed files with 29 additions and 0 deletions

View File

@ -1,4 +1,5 @@
import Polyglot from 'node-polyglot';
import TimeAgo from 'timeago-react';
import * as timeago from 'timeago.js';
import messages_ar from './messages_ar.json';
@ -83,4 +84,32 @@ timeago.register('pt', pt);
timeago.register('sv', sv);
timeago.register('tr', tr);
/**
* Helper function that allows plugins to register their
* own I18N labels
*/
i18n.registerMessages = dict => {
const currentLocale = i18n.locale();
const messages = dict[currentLocale];
if (messages)
i18n.extend(messages);
else
console.warn(`Locale ${currentLocale} not supported by plugin widget - falling back to defaults`);
}
export default i18n;
/**
* For convenience: exposes a pre-localized TimeAgo widget,
* for use in plugins
*/
export const LocalTimeAgo = props => {
return (
<TimeAgo
datetime={props.timestamp}
locale={i18n.locale()} />
)
}