@uimkit/uikit-react
Version:
<img style="width:64px" src="https://mgmt.uimkit.chat/media/img/avatar.png"/>
53 lines (50 loc) • 2.53 kB
JavaScript
import { __assign } from 'tslib';
import React__default, { useContext } from 'react';
import Dayjs from 'dayjs';
import calendar from 'dayjs/plugin/calendar';
import LocalizedFormat from 'dayjs/plugin/localizedFormat';
import { getDisplayName } from './utils/getDisplayName.js';
import { defaultTranslatorFunction } from '../i18n/uimi18n.js';
Dayjs.extend(calendar);
Dayjs.extend(LocalizedFormat);
var isLanguageSupported = function (language) {
var translations = ['de', 'en', 'es', 'fr', 'hi', 'it', 'ja', 'ko', 'nl', 'pt', 'ru', 'tr', 'zh'];
return translations.some(function (translation) { return language === translation; });
};
var isDayOrMoment = function (output) {
return !!(output === null || output === void 0 ? void 0 : output.isSame);
};
var isDate = function (output) {
return !!(output === null || output === void 0 ? void 0 : output.getMonth);
};
var isNumberOrString = function (output) {
return typeof output === 'string' || typeof output === 'number';
};
var defaultDateTimeParser = function (input) { return Dayjs(input); };
var TranslationContext = React__default.createContext({
t: defaultTranslatorFunction,
tDateTimeParser: defaultDateTimeParser,
userLanguage: 'zh',
});
var TranslationProvider = function (_a) {
var children = _a.children, value = _a.value;
return (React__default.createElement(TranslationContext.Provider, { value: value }, children));
};
var useTranslationContext = function (componentName) {
var contextValue = useContext(TranslationContext);
if (!contextValue) {
console.warn("The useTranslationContext hook was called outside of the TranslationContext provider. Make sure this hook is called within a child of the Chat component. The errored call is located in the ".concat(componentName, " component."));
return {};
}
return contextValue;
};
var withTranslationContext = function (Component) {
var WithTranslationContextComponent = function (props) {
var translationContext = useTranslationContext();
return React__default.createElement(Component, __assign({}, props, translationContext));
};
WithTranslationContextComponent.displayName = "WithTranslationContext".concat(getDisplayName(Component));
return WithTranslationContextComponent;
};
export { TranslationContext, TranslationProvider, defaultDateTimeParser, isDate, isDayOrMoment, isLanguageSupported, isNumberOrString, useTranslationContext, withTranslationContext };
//# sourceMappingURL=TranslationContext.js.map