React & Preact Integrations
Relevant source files
The following files were used as context for generating this wiki page:
React and Preact integrations provide framework-specific bindings for the i18n-micro core logic. These packages enable reactive translations, localized routing, and full TypeScript support within non-Nuxt environments. Both integrations share a consistent API, including a provider component and a custom hook for translation access.
Sources: packages/react/README.md, packages/preact/README.md
Architecture and Dependencies
The framework bindings function as thin wrappers around the shared domain logic. They depend on core workspace packages to ensure performance and consistency across the monorepo.
Core Dependencies
Both integrations rely on the following internal packages:
@i18n-micro/core: Provides translation and pluralization logic.@i18n-micro/runtime: Manages messages and fetch operations.@i18n-micro/types: Provides shared TypeScript definitions.
Sources: packages/react/package.json:54-58, packages/preact/package.json:49-53
Integration Structure
The diagram shows how framework-specific bindings consume framework-agnostic core logic to serve end-user applications.
Sources: AGENTS.md
Core Components and Hooks
The integrations expose a standardized API to manage internationalization state and provide translation functions to the component tree.
API Summary
| Component/Hook | Purpose | Description |
|---|---|---|
createI18n | Initialization | Configures the i18n instance with locales and messages. |
I18nProvider | Context Provider | Wraps the application to provide i18n context to children. |
useI18n | Consumption | A hook that returns translation functions (t, tc) and state. |
Sources: packages/react/README.md:38-42, packages/preact/README.md:43-47
Initialization Flow
You must initialize the i18n instance before providing it to your application. The createI18n function accepts configuration including locale, fallbackLocale, and the messages dictionary.
The sequence shows the data flow from initialization at the entry point to consumption within a deep component.
Sources: packages/react/README.md:16-36, packages/preact/README.md:15-41
React Specifics
The React integration uses use-sync-external-store to manage reactive state. This ensures compatibility with React's concurrent rendering features and maintains store synchronization.
Peer Dependencies
react: Required peer dependency.react-router-dom: Optional peer dependency for route-specific features.use-sync-external-store: Internal dependency for state management.
Sources: packages/react/package.json:59-71
Accessibility
Recent updates improved ARIA attributes in both React and Preact components to enhance screen reader support.
Sources: packages/react/CHANGELOG.md:42-43, packages/preact/CHANGELOG.md:46-47
Preact Specifics
The Preact integration is designed for Preact version 10 or 11. It supports reactive translations and route-specific logic using routers like wouter or preact-router.
Version Compatibility
Components use HTMLAttributes and TargetedMouseEvent. These require Preact ≥10.27.2. While Preact 11 is supported, it is ESM-only; the package provides an ESM build to accommodate this.
Sources: packages/preact/README.md:3-8
Usage Example
The following example demonstrates standard usage in a component using the useI18n hook.
import { useI18n } from '@i18n-micro/react' // or @i18n-micro/preact
function LanguageSwitcher() {
const { t, tc, locale, setLocale } = useI18n()
return (
<div>
<p>{t('greeting', { name: 'User' })}</p>
<p>{tc('apples', 10)}</p>
<button onClick={() => setLocale('fr')}>French</button>
</div>
)
}
Sources: packages/react/README.md:38-51, packages/preact/README.md:43-56
Summary
React and Preact integrations extend the i18n-micro ecosystem into the broader JavaScript frontend landscape. By utilizing shared @i18n-micro core packages, they maintain the project's goals of small bundles and low runtime costs while providing idiomatic hooks and context for their respective frameworks. Key features include reactive translation updates via useI18n and accessible UI components.