Flutter Localization is a package use for in-app localization with Map data. Easier and faster to implement. This package is inspired by the Flutter SDK flutter_localizations itself. Follow the step below to use the package, or you can check out a small example project of the package.
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: 'ローカリゼーション'};
}
final FlutterLocalization localization = FlutterLocalization.instance;
@override
void initState() {
localization.init(
mapLocales: [
const MapLocale('en', AppLocale.EN),
const MapLocale('km', AppLocale.KM),
const MapLocale('ja', AppLocale.JA),
],
initLanguageCode: 'en',
);
localization.onTranslatedLanguage = _onTranslatedLanguage;
super.initState();
}
// the setState function here is a must to add
void _onTranslatedLanguage(Locale? locale) {
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: localization.supportedLocales,
localizationsDelegates: localization.localizationsDelegates,
home: const SettingsScreen(),
);
}
ElevatedButton(
child: const Text('English'),
onPressed: () {
localization.translate('en');
},
);
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
localization.currentLocale.localeIdentifier;
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),
);
}
Added Strings Util and Context Extension for helping with localization text that are dynamic base on language. Check the usage below or the example here.
As for Strings Util, it just formats string normally from the list of arguments to the full text string.
Strings.format('Hello %a, this is me %a.', ['Dara', 'Sopheak']);
// Result: Hello Dara, this is me Sopheak.
As for Context Extension, the full text and arguments you provide, will use to check and get data from the string source. If the result is null, it will return the key that use to get the resource string.
context.formatString('This is %a package, version %a.', [AppLocale.title, 'LATEST'])
// Result: This is Localization package, version LATEST.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。