Last updated:
0 purchases
fluent i18n
Why fluent_i18n? #
There are times that you want to access localization text without context,
For example, in a model class.
This package use singleton to load Fluent Translation List (FTL) files. Localized strings can be accessed without the present of context (of course, it can also be accessed via context as well), it also provides more convenient approach of writing less code and still localizing all the segments of the app.
Getting Started #
đŠ Installation #
Add to your pubspec.yaml:
dependencies:
fluent_i18n: <last_version>
copied to clipboard
Create folder and add translation files like this
assets
âââ i18n
âââ {languageCode}.ftl //only language code
âââ {languageCode}_countryCode}.ftl //or full locale code
copied to clipboard
Example:
assets
âââ i18n
âââ en.ftl
âââ en_US.ftl
copied to clipboard
Declare your assets localization directory in pubspec.yaml:
flutter:
assets:
- assets/i18n/
copied to clipboard
â ī¸ Note on iOS #
For translation to work on iOS you need to add supported locales to
ios/Runner/Info.plist as described here.
Example:
<key>CFBundleLocalizations</key>
<array>
<string>en</string>
<string>ja</string>
</array>
copied to clipboard
âī¸ Configuration app #
Add EasyLocalization widget like in example
import 'package:fluent_i18n/fluent_i18n.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
const List<Locale> SUPPORTED_LOCALES = [
const Locale('ja'),
const Locale('en'),
];
Locale locale = SUPPORTED_LOCALES.first;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
locale: locale,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
FluentLocalizationsDelegate(SUPPORTED_LOCALES),
],
supportedLocales: SUPPORTED_LOCALES,
);
}
}
copied to clipboard
Full example
Usage #
đĨ Change locale setLocale() #
You can change the locale by calling
await FluentLocalizations.setLocale(locale);
copied to clipboard
This will change the current locale and load the corresponding ftl file.
Alternatively, you can call (this is what setLocale() does):
await FluentLocalizations.ofLocale(locale).load();
copied to clipboard
đĨ Translate getMessage() #
Main function for translate your language keys
final i18n = FluentLocalizations.current();
Text(i18n.getMessage('home-title'))
copied to clipboard
Or if you maintain the locale yourself, you can use
final i18n = FluentLocalizations.ofLocale(currentLocale);
...
// Load somewhere
await i18n.load();
...
Text(i18n.getMessage('home-title'))
copied to clipboard
âšī¸ No breaking changes, you can use old the static method FluentLocalizations.of(context)
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.