Hikari i18n System
Overview
The Hikari i18n system provides internationalization support for Hikari UI applications. It uses TOML files for language definitions and integrates seamlessly with Tairitsu components.
Features
- Type-safe language keys - Generated with yuuka for deep nested structures
- TOML-based translations - Easy to read and edit
- Context-based access - Use use_i18n() hook in any component
- Language switcher component - Ready-to-use UI for language selection
- Multi-language support - All UN official languages + Japanese and Korean
- RTL support - Right-to-left layout support for Arabic and other RTL languages
Supported Languages
| Language | Code | Enum Variant | Direction |
|---|---|---|---|
| English | en-US | Language::English | LTR |
| Simplified Chinese | zh-CHS | Language::ChineseSimplified | LTR |
| Traditional Chinese | zh-CHT | Language::ChineseTraditional | LTR |
| French | fr-FR | Language::French | LTR |
| Russian | ru-RU | Language::Russian | LTR |
| Spanish | es-ES | Language::Spanish | LTR |
| Arabic | ar-SA | Language::Arabic | RTL |
| Japanese | ja-JP | Language::Japanese | LTR |
| Korean | ko-KR | Language::Korean | LTR |
Quick Start
1. Define TOML Content
Create TOML files for each language:
# en-US.toml
[common.button]
submit = "Submit"
cancel = "Cancel"
[common.navigation]
home = "Home"
about = "About"
# ar-SA.toml (RTL language)
[common.button]
submit = "إرسال"
cancel = "إلغاء"
[common.navigation]
home = "الرئيسية"
about = "حول"
2. Wrap App with I18nProvider
use ;
3. Use i18n in Components
use use_i18n;
Language Switcher
The LanguageSwitcher component provides a ready-to-use UI for switching languages:
use ;
RTL (Right-to-Left) Support
Hikari provides full RTL support for languages like Arabic:
Automatic Direction Detection
The I18nProvider automatically sets the dir attribute based on the language:
ThemeProvider with Direction
The ThemeProvider also supports direction configuration:
use ThemeProvider;
Layout Components and RTL
All layout components automatically adapt to RTL:
use ;
Manual RTL Override
You can override RTL behavior per component:
FlexBox
CSS Logical Properties
Use CSS logical properties for RTL-compatible styles:
/* Instead of: */
/* Use: */
Language Utilities
Check if Language is RTL
use Language;
let lang = Arabic;
if lang.is_rtl
Get Language Direction
let direction = Arabic.direction;
// Returns TextDirection::Rtl
Get Language Native Name
let name = Japanese.native_name;
// Returns "日本語"
Dynamic Language Loading
To load different languages dynamically:
Complete Example
See /examples/website/src/components/i18n_demo.rs for a complete working example.
API Reference
Components
- I18nProvider - Root provider component with automatic RTL detection
- LanguageSwitcher - Language selection UI
Hooks
- use_i18n() - Access i18n context in components
Types
- Language - Supported language enum (9 languages)
- TextDirection - LTR/RTL direction enum
- I18nContext - Context containing language and keys
- I18nKeys - Language key structure
Theme Types
- LayoutDirection - Layout direction (LTR/RTL)
- ThemeContext.direction - Current layout direction
Architecture
I18nProvider (root)
↓
use_context_provider
↓
I18nContext (accessible via use_i18n)
├── language: Language
├── keys: I18nKeys
└── automatic dir="rtl" for RTL languages
↓
ThemeProvider
└── direction: LayoutDirection
↓
Layout Components
└── Automatic RTL adaptation
Best Practices
- 1Keep TOML files organized - Use nested structures for related keys
- 2Use descriptive key names - e.g., common.button.submit instead of btn1
- 3Provide all translations - Ensure all keys exist in all language files
- 4Test language switching - Verify all components update correctly
- 5Test RTL layouts - Verify Arabic layout renders correctly
- 6Use logical CSS properties - Prefer margin-inline-start over margin-left
Future Enhancements
- Pluralization rules
- Date/number formatting
- Lazy loading of language files
- Additional RTL languages (Hebrew, Farsi, Urdu)