flutter_localization 0.1.8 flutter_localization: ^0.1.8 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';
import 'primary_button_widget.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 _translator = 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: ${_translator.getLanguageName()}'),
const SizedBox(height: 64.0),
Row(
children: [
Expanded(
child: PrimaryButtonWidget(
label: AppLocale.title.getString(context),
onPressed: () => _translator.translate('en'),
),
),
Expanded(
child: PrimaryButtonWidget(
label: AppLocale.title.getString(context),
onPressed: () => _translator.translate('km'),
),
),
Expanded(
child: PrimaryButtonWidget(
label: AppLocale.title.getString(context),
onPressed: () => _translator.translate('ja'),
),
),
],
),
],
),
),
);
}
}
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: 'ローカリゼーション'};
}