flutter_localization
Flutter Localization 是一个用于应用内本地化的包,支持地图数据。实现起来更加简单快速,灵感来自 flutter_localizations。
Flutter Localization 是一个用于应用内本地化的包,支持使用 Map 数据或 JSON 资源文件,灵感来自 Flutter SDK 的 flutter_localizations(带 s)
{"sdk":"flutter"}{"sdk":"flutter"}{"sdk":"flutter"}^1.1.1^3.1.3^2.3.1^2.5.5^2.1.8{"sdk":"flutter"}^6.0.0以下为英文项目原文快照,最新内容请访问 GitHub。
Flutter Localization is a package used for in-app localization with Map data or JSON assets. It is designed to be easy and fast to implement. This package is inspired by the Flutter SDK flutter_localizations itself. Follow the steps below to use the package, or you can check out a small example project of the package.
Buy Me a Coffee Buy Me a Coffee at ko-fi.com
From version 0.4.0 onward, we support 2 ways of language source (in-memory Map data and JSON asset file).
Create a dart file which will contain all the Map data of the locale language your app need. You can change the file
name, class name, and file path whatever you like. Example:
mixin AppLocale {
static const String title = 'title';
static const Map<String, dynamic> EN = {title: 'Localization'};
static const Map<String, dynamic> KM = {title: 'ការធ្វើមូលដ្ឋានីយកម្ម'};
static const Map<String, dynamic> JA = {title: 'ローカリゼーション'};
}
You can also store your translations in JSON files and load them via assets. Asset's path is not restricted but the file
name is. You need to name the file follow your supported language code. For example, create assets/i18n/en.json:
{
"title": "Localization"
}
Then declare the assets in your pubspec.yaml:
flutter:
assets:
- assets/i18n/en.json
- assets/i18n/km.json
- assets/i18n/ja.json
WidgetsFlutterBinding.ensureInitialized()
and await FlutterLocalization.instance.ensureInitialized() before runApp() function like below.Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await FlutterLocalization.instance.ensureInitialized();
runApp(const MyApp());
}
final FlutterLocalization localization = FlutterLocalization.instance;
main.dart or the MaterialApp in your project.@override
void initState() {
super.initState();
localization.onTranslatedLanguage = _onTranslatedLanguage;
// Initialize with map-based
_initializeLocalizationMapBased();
// or JSON asset base on your language source
_initializeLocalizationJsonAsset();
// make sure to use only 1 type of source
}
// The setState call here is required to rebuild the app after language changes.
void _onTranslatedLanguage(Locale? locale) {
setState(() {});
}
void _initializeLocalizationMapBased() {
localization.init(
initLanguageCode: 'en',
source: LocalizationSource.map,
mapLocales: const [
MapLocale('en', AppLocale.EN),
MapLocale('km', AppLocale.KM),
MapLocale('ja', AppLocale.JA),
],
);
}
void _initializeLocalizationJsonAsset() {
localization.init(
initLanguageCode: 'en',
source: LocalizationSource.jsonAsset,
jsonLocales: const [
JsonLocale('en', 'assets/i18n/en.json'),
JsonLocale('km', 'assets/i18n/km.json'),
JsonLocale('ja', 'assets/i18n/ja.json'),
],
);
}
supportedLocales and localizationsDelegates to the MaterialApp@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: localization.supportedLocales,
localizationsDelegates: localization.localizationsDelegates,
home: const SettingsScreen(),
);
}
translate() function anytime you want to translate the app and provide it with the language codeElevatedButton(
child: const Text('English'),
onPressed: () {
localization.translate('en');
},
);
getString() extension by providing the context
(the AppLocale.title is the constant from mixin class above)AppLocale.title.getString(context);
localization.getLanguageName(languageCode: 'en'); // English
localization.getLanguageName(languageCode: 'km'); // ភាសាខ្មែរ
localization.getLanguageName(languageCode: 'ja'); // 日本語
localization.getLanguageName(); // get language name depend on current app locale
scriptCode and countryCode are optional, this might return only
the languageCode.localization.currentLocale.localeIdentifier;
JsonLocale model.LocalizationSource enum to choose between LocalizationSource.map and LocalizationSource.jsonAsset.From version 0.3.0 onward, there is a major update that breaks the code in the initialize flow. Please re-check the README document at the beginning of the Project Configuration section to see more. The breaking change is related to:
main() function from void to Future<void> async for package ensureInitialized functionensureInitialized() function to init the core functionality of the packageAdded Strings Util and Context Extension for helping with localization text that are dynamic base on language. Check the usage below or the example here.
Strings.format('Hello %a, this is me %a.', ['Dara', 'Sopheak']);
// Result: Hello Dara, this is me Sopheak.
context.formatString('This is %a package, version %a.', [AppLocale.title, 'LATEST'])
// Result: This is Localization package, version LATEST.
You can provide the font family in the MapLocale model at the init() function that can be
from Assets or GoogleFonts
package.
// font family from asset
MapLocale('en', AppLocale.EN, fontFamily: 'MuseoSans');
// or from GoogleFonts package
MapLocale('en', AppLocale.EN, fontFamily: GoogleFonts.lato().fontFamily);
Lastly, provide the font family to the MaterialApp's ThemeData
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: localization.supportedLocales,
localizationsDelegates: localization.localizationsDelegates,
home: const SettingsScreen(),
theme: ThemeData(fontFamily: localization.fontFamily),
);
}