freya-i18n crate, powered by Project Fluent. Fluent is a modern localization system with a simple syntax and powerful features.
Quick Start
Create Fluent translation files:en-US.ftl
es-ES.ftl
Configuration
Language Identifiers
Use thelangid! macro to create language identifiers:
Static Locales
Embed translations in your binary:Dynamic Locales
Load translations from the filesystem:Fallback Language
Set a fallback for missing translations:Auto-Discovery (Optional)
Automatically load all.ftl files from a directory:
Initialization
Component-Scoped
Create i18n context for the component tree:Global i18n
Share i18n across multiple windows:Translation Macros
t! - Translate
Basic translation:te! - Translate with Error Handling
Handle translation errors:tid! - Get Translation ID
Check if translation exists:Fluent Syntax
Simple Messages
Messages with Variables
Messages with Attributes
Pluralization
Selectors
References
Reuse messages:Term References
API Usage
Getting I18n Context
Translating Programmatically
Changing Language
Getting Current Language
Updating Fallback
Language Switcher
Create a reusable language selector:Error Handling
Best Practices
- Consistent IDs - Use kebab-case for message IDs:
user-profile,sign-in - Organize by feature - Group related messages in files
- Descriptive IDs - Use meaningful names:
button-savenotbtn1 - Default to English - Use English as fallback language
- Test all languages - Verify translations render correctly
- Handle missing translations - Always have fallbacks
- Context in comments - Document where messages are used
Complete Example
Multi-language app with user preferences:i18n/en-US.ftl
i18n/es-ES.ftl
i18n/fr.ftl