react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
46 lines (43 loc) • 1.55 kB
JavaScript
'use client';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.cjs');
const require_client_IntlayerProvider = require('../IntlayerProvider.cjs');
let react = require("react");
let _intlayer_core_formatters = require("@intlayer/core/formatters");
//#region src/client/format/useIntl.ts
/**
* React client hook that provides a locale-bound `Intl` object.
*
* It acts exactly like the native `Intl` object, but acts as a proxy to:
* 1. Inject the current locale automatically if none is provided.
* 2. Use the performance-optimized `CachedIntl` under the hood.
*
* @example
* ```tsx
* const intl = useIntl(); // uses context locale
*
* // Standard API, but no need to pass 'en-US' as the first argument
* const formatted = new intl.NumberFormat({
* style: 'currency',
* currency: 'USD'
* }).format(123.45);
* ```
*
* @example
* ```tsx
* const intl = useIntl();
*
* // You can still override the locale if needed
* const date = new intl.DateTimeFormat({ locale: 'fr-FR' }).format(new Date());
* // or
* const date2 = new intl.DateTimeFormat('fr-FR').format(new Date());
* ```
*/
const useIntl = (locale) => {
const { locale: contextLocale } = (0, react.useContext)(require_client_IntlayerProvider.IntlayerClientContext) ?? {};
const currentLocale = locale ?? contextLocale;
return (0, react.useMemo)(() => (0, _intlayer_core_formatters.bindIntl)(currentLocale), [currentLocale]);
};
//#endregion
exports.useIntl = useIntl;
//# sourceMappingURL=useIntl.cjs.map