useKanjou
The useKanjou hook allows you to consume the i18n context provided by <KanjouProvider>. It returns a set of functions to format messages, dates, numbers, and more, as well as the current locale.
Import
ts
import { useKanjou } from '@kanjou/react'Returns
useKanjou returns a KanjouInstance with the following properties:
locale(string): The currently active locale.t(id, values?): Formats a message as a string. Aliased toformatMessage.rich(id, values?): Formats a message to aReactNode(supports React components inside messages). Aliased toformatRich.formatDate(date, options?): Formats aDateor timestamp.formatTime(date, options?): Formats aDateor timestamp using time formatting.formatNumber(number, options?): Formats anumberorbigint.formatPlural(value, options?): Returns the plural category for a number.formatList(list, options?): Formats an iterable list of strings.formatDisplayName(code, options?): Formats translations of language, region, or script codes.formatRelativeTime(value, unit, options?): Formats a relative time (e.g. "2 days ago").formatDuration(duration, options?): Formats a duration object.
Usage
Basic Translation
tsx
import { useKanjou } from '@kanjou/react'
export function MyComponent() {
const kanjou = useKanjou()
return (
<div>
{/* Type-safe message keys and parameters! */}
<h1>{kanjou.t('hello_world')}</h1>
<p>{kanjou.t('welcome', { name: 'Alice' })}</p>
</div>
)
}E2E Type Safety
With Kanjou's types configured, the t() and rich() functions enforce that both the message id and the values object exactly match what is defined in your MessageFormat 2 dictionaries.
Rich Text Formatting
Use the rich (or formatRich) function when you need to embed React components (like links, bold text, etc.) within your translated messages.
tsx
import { useKanjou } from '@kanjou/react'
export function RichNotice() {
const kanjou = useKanjou()
return (
<p>
{kanjou.rich('read_more')}
{/* Note: Components like 'link' and 'bold' must be configured globally in your <KanjouProvider> */}
</p>
)
}Date and Number Formatting
Kanjou provides high-performance, automatically cached wrappers around native Intl formatters.
tsx
import { useKanjou } from '@kanjou/react'
export function Stats() {
const kanjou = useKanjou()
return (
<ul>
<li>Views: {kanjou.formatNumber(1000000)}</li>
<li>Last Updated: {kanjou.formatDate(new Date(), { dateStyle: 'short' })}</li>
</ul>
)
}