flutter_localization 0.1.9 flutter_localization: ^0.1.9 copied to clipboard
Flutter Localization is a package use for in-app localization with map data. More easier and faster to implement and inspired by the flutter_localizations itself.
import 'package:flutter/material.dart';
import 'package:flutter_localization/flutter_localization.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
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();
}
void _onTranslatedLanguage(Locale? locale) {
setState(() {});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: _localization.supportedLocales,
localizationsDelegates: _localization.localizationsDelegates,
home: const SettingsScreen(),
);
}
}
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
final FlutterLocalization _localization = FlutterLocalization.instance;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(AppLocale.title.getString(context))),
body: Container(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Current language is: ${_localization.getLanguageName()}'),
const SizedBox(height: 64.0),
Row(
children: [
Expanded(
child: ElevatedButton(
child: const Text('English'),
onPressed: () {
_localization.translate('en');
},
),
),
const SizedBox(width: 8.0),
Expanded(
child: ElevatedButton(
child: const Text('ភាសាខ្មែរ'),
onPressed: () {
_localization.translate('km');
},
),
),
const SizedBox(width: 8.0),
Expanded(
child: ElevatedButton(
child: const Text('日本語'),
onPressed: () {
_localization.translate('ja', save: false);
},
),
),
],
),
],
),
),
);
}
}
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: 'ローカリゼーション'};
}