react-intlayer
Version:
Easily internationalize i18n your React applications with type-safe multilingual content management.
34 lines (32 loc) • 1.15 kB
JavaScript
import { getServerContext } from "../serverContext.mjs";
import { IntlayerServerContext } from "../IntlayerServerProvider.mjs";
import { relativeTime } from "@intlayer/core";
//#region src/server/format/useRelativeTime.ts
/**
* Client-side React hook for accessing a localized relative time formatter.
*
* This hook:
* - Reads the current locale from {@link useLocaleBase}.
* - Creates a new relative time formatter with {@link createRelativeTime}.
* - Returns a function that can format time differences into localized strings.
*
* Example:
* ```tsx
* const relativeTime = useRelativeTime();
* const formatted = relativeTime(new Date("2024-08-01"), new Date());
* // e.g., "2 weeks ago"
* ```
*
* @returns {ReturnType<typeof createRelativeTime>} A relative time formatting function
* bound to the current client locale.
*/
const useRelativeTime = () => {
const locale = getServerContext(IntlayerServerContext);
return (...args) => relativeTime(args[0], args[1] ?? /* @__PURE__ */ new Date(), {
...args[2],
locale: args[2]?.locale ?? locale
});
};
//#endregion
export { useRelativeTime };
//# sourceMappingURL=useRelativeTime.mjs.map