fluent_localization
Package that allows you to set up and use translations in an easy and quick way.
Getting Started
Add dependencies
fluent_localization: ^1.6.0
Add language folder to Flutter assets
flutter:
assets:
- "assets/languages/"
Create assets files
// assets/languages/en.json
{
"hello": "Hello {name}!",
"title": "Welcome"
}
Build module
void main() async {
await Fluent.build([
LocalizationModule(),
]);
runApp(App());
}
Type-Safe Generation (NEW 🚀)
To avoid using strings keys manually, you can generate type-safe keys using the built-in generator.
1. Run the generator
Run this command in your project root:
fvm dart run fluent_localization:generate [inputPath] [outputPath] [baseLocale]
By default, it uses:
inputPath:assets/languagesoutputPath:lib/localization_keys.g.dartbaseLocale:en(usesen.jsonas source)
Example using Spanish as the base language:
fvm dart run fluent_localization:generate assets/languages lib/localization_keys.g.dart es
This will create a file at lib/localization_keys.g.dart using es.json to generate the keys.
2. Use it in your code
Import the generated file and use the context.loc extension:
// Simple key
Text(context.loc.title)
// Key with arguments
Text(context.loc.hello(name: 'John'))
Manual Usage
If you prefer not to use the generator:
final hello = context.tr('hello', args: {'name': 'John'});
Example