@figliolia/react-hooks
Version:
A small collection of simple React Hooks you're probably rewriting on a regular basis
23 lines (22 loc) • 909 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useLocale = void 0;
const react_1 = require("react");
const getLocale = (defaultLocale) => {
var _a, _b;
return (_b = (_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.language) !== null && _b !== void 0 ? _b : defaultLocale;
};
const useLocale = (defaultLocale = "en-us") => {
const [locale, setLocale] = (0, react_1.useState)(getLocale(defaultLocale));
const onChange = (0, react_1.useCallback)(() => {
setLocale(getLocale(defaultLocale));
}, [defaultLocale]);
(0, react_1.useEffect)(() => {
window.addEventListener("languagechange", onChange);
return () => {
window.removeEventListener("languagechange", onChange);
};
}, [onChange]);
return locale;
};
exports.useLocale = useLocale;