react-translate-maker
Version:
React translation module. Internationalize your great project.
38 lines (32 loc) • 781 B
JSX
// @flow
import { useContext, useCallback } from 'react';
import TranslateContext from './TranslateContext';
type Props = {
onError?: Function,
onChange?: Function,
children: Function,
};
export default function LocaleSwitch(props: Props) {
const { onError, onChange, children } = props;
const { translate } = useContext(TranslateContext);
const handleChange = useCallback(async (locale: string) => {
try {
await translate.setLocale(locale);
if (onChange) {
onChange(locale);
}
} catch (e) {
if (onError) {
onError(e);
}
}
}, []);
return children({
locale: translate.getLocale(),
changeLocale: handleChange,
});
}
LocaleSwitch.defaultProps = {
onError: undefined,
onChange: undefined,
};