houdini/app/javascript/hooks/useIntl.ts

30 lines
971 B
TypeScript
Raw Normal View History

2020-10-20 21:57:09 +00:00
// License: LGPL-3.0-or-later
import * as React from "react";
import type { FormatNumberOptions, IntlShape as ParentIntlShape } from "react-intl";
2020-10-20 21:57:09 +00:00
import { Money } from "../common/money";
export type FormatMoneyOptions = Omit<FormatNumberOptions,'style'|'unit'|'unitDisplay'|'currency'>;
2020-10-20 21:57:09 +00:00
export type IntlShape = ParentIntlShape & {
2020-10-20 21:57:09 +00:00
/**
* Format a monetary value as a string given the locale
*
* @param {Money} amount the monetary value to convert to a string
* @param {FormatMoneyOptions} [opts] options for controlling how the string should be formatted
* @returns {string}
*/
formatMoney(amount: Money, opts?: FormatMoneyOptions): string;
};
export const IntlContext = React.createContext<IntlShape>(null as IntlShape);
2020-10-20 21:57:09 +00:00
/**
* Use just like `useIntl` for getting strings for the current locale.
*
* @export
* @returns {IntlShape}
2020-10-20 21:57:09 +00:00
*/
export default function useIntl() : IntlShape {
const context = React.useContext(IntlContext);
2020-10-20 21:57:09 +00:00
return context;
}