### Flutter Localization Setup and Usage Example Source: https://pub.dev/packages/flutter_localization/versions/0.2.2/example Demonstrates the core setup and usage of the Flutter Localization package. It includes defining locales, initializing the localization system, and integrating it into a Flutter application's build process. This example showcases how to switch languages and display localized strings. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; void main() { runApp(const MyApp()); } mixin AppLocale { static const String title = 'title'; static const String thisIs = 'thisIs'; static const Map EN = { title: 'Localization', thisIs: 'This is %a package, version %a.', }; static const Map KM = { title: 'ការធ្វើមូលដ្ឋានីយកម្ម', thisIs: 'នេះគឺជាកញ្ចប់%a កំណែ%a.', }; static const Map JA = { title: 'ローカリゼーション', thisIs: 'これは%aパッケージ、バージョン%aです。', }; } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final FlutterLocalization _localization = FlutterLocalization.instance; @override void initState() { _localization.init( mapLocales: [ const MapLocale( 'en', AppLocale.EN, countryCode: 'US', fontFamily: 'Font EN', ), const MapLocale( 'km', AppLocale.KM, countryCode: 'KH', fontFamily: 'Font KM', ), const MapLocale( 'ja', AppLocale.JA, countryCode: 'JP', fontFamily: 'Font 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(), theme: ThemeData(fontFamily: _localization.fontFamily), ); } } class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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( children: [ Row( children: [ Expanded( child: ElevatedButton( child: const Text('English'), onPressed: () { _localization.translate('en'); }, ), ), const SizedBox(width: 16.0), Expanded( child: ElevatedButton( child: const Text('ភាសាខ្មែរ'), onPressed: () { _localization.translate('km'); }, ), ), const SizedBox(width: 16.0), Expanded( child: ElevatedButton( child: const Text('日本語'), onPressed: () { _localization.translate('ja', save: false); }, ), ), ], ), const SizedBox(height: 16.0), ItemWidget( title: 'Current Language', content: _localization.getLanguageName(), ), ItemWidget( title: 'Font Family', content: _localization.fontFamily, ), ItemWidget( title: 'Locale Identifier', content: _localization.currentLocale.localeIdentifier, ), ItemWidget( title: 'String Format', content: Strings.format( 'Hello %a, this is me %a.', ['Dara', 'Sopheak'], ), ), ItemWidget( title: 'Context Format String', content: context.formatString( AppLocale.thisIs, [AppLocale.title, 'LATEST'], ), ), ], ), ), ); } } class ItemWidget extends StatelessWidget { const ItemWidget({ super.key, required this.title, required this.content, }); final String? title; final String? content; @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(bottom: 8.0), child: Row( ``` -------------------------------- ### Flutter Localization Package Usage Source: https://pub.dev/packages/flutter_localization/versions/0.3.1/changelog Demonstrates basic usage of the flutter_localization package, including initialization and getting translated strings. This is a general example illustrating the package's core functionality. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; void main() { WidgetsFlutterBinding.ensureInitialized(); FlutterLocalization.initialize( basePath: baseAssetsPath, // or use Map // locales: [ Locale('en', 'US'), Locale('es', 'ES'), Locale('pt', 'BR'), ], ); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( // ... other MaterialApp properties supportedLocales: const [ Locale('en', 'US'), Locale('es', 'ES'), Locale('pt', 'BR'), ], localeResolutionCallback: (current, supported) { if (current != null) { // check for available languages for (Locale locale in supported) { if (locale.languageCode == current.languageCode) { // update the current locale return locale; } } } // return the default locale (first supported) return supported.first; }, home: const MyHomePage(), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Flutter Localization Example'.i18n()), ), body: Center( child: Text('Hello World'.i18n()), ), ); } } ``` -------------------------------- ### Flutter App Initialization and Localization Setup Source: https://pub.dev/packages/flutter_localization/example Initializes the Flutter localization system and runs the main application widget. It defines locale-specific string maps and configures the FlutterLocalization instance to use these maps. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await FlutterLocalization.instance.ensureInitialized(); runApp(const MyApp()); } mixin AppLocale { static const String title = 'title'; static const String thisIs = 'thisIs'; static const Map EN = { title: 'Localization', thisIs: 'This is %a package, version %a.', }; static const Map KM = { title: 'ការធ្វើមូលដ្ឋានីយកម្ម', thisIs: 'នេះគឺជាកញ្ចប់%a កំណែ%a.', }; static const Map JA = { title: 'ローカリゼーション', thisIs: 'これは%aパッケージ、バージョン%aです。', }; } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final FlutterLocalization _localization = FlutterLocalization.instance; @override void initState() { _localization.init( mapLocales: [ const MapLocale( 'en', AppLocale.EN, countryCode: 'US', fontFamily: 'Font EN', ), const MapLocale( 'km', AppLocale.KM, countryCode: 'KH', fontFamily: 'Font KM', ), const MapLocale( 'ja', AppLocale.JA, countryCode: 'JP', fontFamily: 'Font 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(), theme: ThemeData(fontFamily: _localization.fontFamily), ); } } class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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( children: [ Row( children: [ Expanded( child: ElevatedButton( child: const Text('English'), onPressed: () { _localization.translate('en'); }, ), ), const SizedBox(width: 16.0), Expanded( child: ElevatedButton( child: const Text('ភាសាខ្មែរ'), onPressed: () { _localization.translate('km'); }, ), ), const SizedBox(width: 16.0), Expanded( child: ElevatedButton( child: const Text('日本語'), onPressed: () { _localization.translate('ja', save: false); }, ), ), ], ), const SizedBox(height: 16.0), ItemWidget( title: 'Current Language', content: _localization.getLanguageName(), ), ItemWidget( title: 'Font Family', content: _localization.fontFamily, ), ItemWidget( title: 'Locale Identifier', content: _localization.currentLocale.localeIdentifier, ), ItemWidget( title: 'String Format', content: Strings.format( 'Hello %a, this is me %a.', ['Dara', 'Sopheak'], ), ), ItemWidget( title: 'Context Format String', content: context.formatString( AppLocale.thisIs, [AppLocale.title, 'LATEST'], ), ), ], ), ), ); } } class ItemWidget extends StatelessWidget { const ItemWidget({ super.key, required this.title, required this.content, }); final String? title; final String? content; @override Widget build(BuildContext context) { return const Placeholder(); } } ``` -------------------------------- ### Flutter Localization Example: Basic Setup and Usage Source: https://pub.dev/packages/flutter_localization/versions/0.1.10/example This Flutter code demonstrates how to set up and use the flutter_localization package to manage in-app localization. It initializes the package with different locales, defines localized strings using a mixin, and provides a UI to switch between languages. ```dart 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 createState() => _MyAppState(); } class _MyAppState extends State { 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 createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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 EN = {title: 'Localization'}; static const Map KM = {title: 'ការធ្វើមូលដ្ឋានីយកម្ម'}; static const Map JA = {title: 'ローカリゼーション'}; } ``` -------------------------------- ### Example pubspec.yaml dependency Source: https://pub.dev/packages/flutter_localization/versions/0.1.7/install Illustrates the line that will be added to your pubspec.yaml file when adding the flutter_localization dependency. ```yaml dependencies: flutter_localization: ^0.1.7 ``` -------------------------------- ### Flutter Localization Setup and Usage Source: https://pub.dev/packages/flutter_localization/versions/0.1.7/example Demonstrates how to initialize and use the FlutterLocalization package in a Flutter application. It covers setting up supported locales, initializing the translator, and handling language changes. ```dart 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 createState() => _MyAppState(); } class _MyAppState extends State { 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 createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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 EN = {title: 'Localization'}; static const Map KM = {title: 'ការធ្វើមូលដ្ឋានីយកម្ម'}; static const Map JA = {title: 'ローカリゼーション'}; } ``` -------------------------------- ### Flutter Localization Example Source: https://pub.dev/packages/flutter_localization/versions/0.1.11/example Demonstrates how to use the flutter_localization package to set up and manage language localization in a Flutter application. It includes defining locales, initializing the localization service, and handling language changes. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; void main() { runApp(const MyApp()); } mixin AppLocale { static const String title = 'title'; static const Map EN = {title: 'Localization'}; static const Map KM = {title: 'ការធ្វើមូលដ្ឋានីយកម្ម'}; static const Map JA = {title: 'ローカリゼーション'}; } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final FlutterLocalization _localization = FlutterLocalization.instance; @override void initState() { _localization.init( mapLocales: [ const MapLocale('en', AppLocale.EN, fontFamily: 'Font EN'), const MapLocale('km', AppLocale.KM, fontFamily: 'Font KM'), const MapLocale('ja', AppLocale.JA, fontFamily: 'Font 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(), theme: ThemeData(fontFamily: _localization.fontFamily), ); } } class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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: 8.0), Text('Font Family: ${_localization.fontFamily}'), 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); }, ), ), ], ), ], ), ), ); } } ``` -------------------------------- ### Initialize Flutter Localization in main() (Dart) Source: https://pub.dev/packages/flutter_localization/index Ensures that Flutter binding and the localization plugin are initialized before the application starts. This is a crucial step for localization to function correctly. ```dart Future main() async { WidgetsFlutterBinding.ensureInitialized(); await FlutterLocalization.instance.ensureInitialized(); runApp(const MyApp()); } ``` -------------------------------- ### Get Flutter Package Dependencies Source: https://pub.dev/packages/flutter_localization/versions/0.3.2/score/log Fetches all dependencies for the Flutter package. This command ensures that all required libraries are available for analysis and builds. ```bash /home/worker/flutter/stable/bin/flutter --no-version-check pub pub get --no-example ``` -------------------------------- ### Initialize Flutter Localization and Run App Source: https://pub.dev/packages/flutter_localization/versions/0.3.0/example This snippet shows the main entry point of a Flutter application. It initializes the Flutter Localization plugin and ensures it's ready before running the root application widget. This is crucial for early access to localization features. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; Future main() async { WidgetsFlutterBinding.ensureInitialized(); await FlutterLocalization.instance.ensureInitialized(); runApp(const MyApp()); } ``` -------------------------------- ### Initialize Localization with EnsureInitialized Source: https://pub.dev/packages/flutter_localization/versions/0.3.2/changelog Ensures that the localization system is initialized. This is a newer function, and its usage is detailed in the package's README or EXAMPLE files for proper implementation. ```dart await ensureInitialized(); ``` -------------------------------- ### Flutter Localization Widget Example Source: https://pub.dev/packages/flutter_localization/example This snippet shows a Flutter widget that displays localized text, likely part of a UI element within an application using the Flutter Localization package. It uses a Row layout with Expanded widgets to display a title and content, separated by a colon. ```dart return Padding( padding: const EdgeInsets.only(bottom: 8.0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded(child: Text(title ?? '')), const Text(' : '), Expanded(child: Text(content ?? '')), ], ), ); ``` -------------------------------- ### Flutter Localization Setup and Usage Source: https://pub.dev/packages/flutter_localization/versions/0.1.12/example Demonstrates how to initialize and use the FlutterLocalization package within a Flutter application. It covers defining locales, setting the initial language, and handling language changes. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; void main() { runApp(const MyApp()); } mixin AppLocale { static const String title = 'title'; static const Map EN = {title: 'Localization'}; static const Map KM = {title: 'ការធ្វើមូលដ្ឋានីយកម្ម'}; static const Map JA = {title: 'ローカリゼーション'}; } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final FlutterLocalization _localization = FlutterLocalization.instance; @override void initState() { _localization.init( mapLocales: [ const MapLocale( 'en', AppLocale.EN, countryCode: 'US', fontFamily: 'Font EN', ), const MapLocale( 'km', AppLocale.KM, countryCode: 'KH', fontFamily: 'Font KM', ), const MapLocale( 'ja', AppLocale.JA, countryCode: 'JP', fontFamily: 'Font 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(), theme: ThemeData(fontFamily: _localization.fontFamily), ); } } class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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: ${_localization.getLanguageName()}'), const SizedBox(height: 8.0), Text('Font Family: ${_localization.fontFamily}'), const SizedBox(height: 8.0), Text('Identifier: ${_localization.currentLocale.localeIdentifier}'), 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); }, ), ), ], ), ], ), ), ); } } ``` -------------------------------- ### Declare flutter_localization in pubspec.yaml Source: https://pub.dev/packages/flutter_localization/versions/0.2.2/install Example of how the flutter_localization dependency should be declared in the pubspec.yaml file. ```yaml dependencies: flutter_localization: ^0.2.2 ``` -------------------------------- ### Build UI for Language Switching and Displaying Localized Strings Source: https://pub.dev/packages/flutter_localization/versions/0.3.0/example This Dart code builds a Flutter `SettingsScreen` UI. It includes buttons to switch languages ('English', 'ភាសាខ្មែរ', '日本語') and displays current localization information such as language name, font family, and locale identifier. It also demonstrates using localized strings with placeholders and context formatting. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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( children: [ Row( children: [ Expanded( child: ElevatedButton( child: const Text('English'), onPressed: () { _localization.translate('en'); }, ), ), const SizedBox(width: 16.0), Expanded( child: ElevatedButton( child: const Text('ភាសាខ្មែរ'), onPressed: () { _localization.translate('km'); }, ), ), const SizedBox(width: 16.0), Expanded( child: ElevatedButton( child: const Text('日本語'), onPressed: () { _localization.translate('ja', save: false); }, ), ), ], ), const SizedBox(height: 16.0), ItemWidget( title: 'Current Language', content: _localization.getLanguageName(), ), ItemWidget( title: 'Font Family', content: _localization.fontFamily, ), ItemWidget( title: 'Locale Identifier', content: _localization.currentLocale.localeIdentifier, ), ItemWidget( title: 'String Format', content: Strings.format( 'Hello %a, this is me %a.', ['Dara', 'Sopheak'], ), ), ItemWidget( title: 'Context Format String', content: context.formatString( AppLocale.thisIs, [AppLocale.title, 'LATEST'], ), ), ], ), ), ); } } class ItemWidget extends StatelessWidget { const ItemWidget({ super.key, required this.title, required this.content, }); final String? title; final String? content; @override Widget build(BuildContext context) { ``` -------------------------------- ### Declare flutter_localization in pubspec.yaml Source: https://pub.dev/packages/flutter_localization/versions/0.1.8/install Example of how the flutter_localization dependency is declared in the pubspec.yaml file. This ensures the package is correctly linked to your project. ```yaml dependencies: flutter_localization: ^0.1.8 ``` -------------------------------- ### Check Dart and Flutter Versions Source: https://pub.dev/packages/flutter_localization/score/log These commands retrieve the version information for the installed Dart and Flutter SDKs. This is crucial for ensuring compatibility and reproducibility of the analysis environment. ```bash /home/worker/dart/stable/bin/dart --version ``` ```bash /home/worker/flutter/stable/bin/flutter --no-version-check --version --machine ``` -------------------------------- ### Flutter Localization Widget Example Source: https://pub.dev/packages/flutter_localization/versions/0.1.13/example This code snippet demonstrates a basic UI structure within the Flutter Localization package, likely for displaying localized text. It utilizes `Expanded` widgets with `Text` widgets to show title and content, with a separator. ```dart crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded(child: Text(title ?? '')), const Text(' : '), Expanded(child: Text(content ?? '')), ], ), ); } } ``` -------------------------------- ### Initialize localization with Map data in Flutter Source: https://pub.dev/packages/flutter_localization/versions/0.1.14/changelog This feature allows localization to be initialized using a Map instead of a JSON file. The 'initWithMap()' function is provided for this purpose, offering a more flexible approach to localization setup in Flutter. ```dart // Add the possibility to do localization with **Map ** instead of **json file**. Check the **README** or **example** for more information with the **initWithMap()** function ``` -------------------------------- ### Add flutter_localization Dependency Source: https://pub.dev/packages/flutter_localization/install This command adds the flutter_localization package as a dependency to your Flutter project. It automatically updates your pubspec.yaml file and runs `flutter pub get`. ```bash $ flutter pub add flutter_localization ``` -------------------------------- ### Add Custom Delegates to Init Source: https://pub.dev/packages/flutter_localization/versions/0.3.1/changelog Allows adding custom delegates to the initialization process, providing flexibility for more complex localization setups. This feature was introduced in version 0.0.7. ```dart init(customDelegates: [MyCustomLocalizationsDelegate()]); ``` -------------------------------- ### Get Locale Identifier Source: https://pub.dev/packages/flutter_localization/versions/0.3.1/changelog Provides an extension to easily get the locale identifier string (e.g., 'en_US', 'km_KH'). This utility helps in managing and displaying locale information. ```dart currentLocale.identifier; ``` -------------------------------- ### Flutter App Localization Setup and Usage Source: https://pub.dev/packages/flutter_localization/versions/0.1.13/example Demonstrates how to initialize the Flutter Localization package with multiple locales (English, Khmer, Japanese) and integrate it into a Flutter application's main widget. It also shows how to handle language change events and use localized strings in the UI. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; void main() { runApp(const MyApp()); } mixin AppLocale { static const String title = 'title'; static const String thisIs = 'thisIs'; static const Map EN = { title: 'Localization', thisIs: 'This is %a package, version %a.', }; static const Map KM = { title: 'ការធ្វើមូលដ្ឋានីយកម្ម', thisIs: 'នេះគឺជាកញ្ចប់%a កំណែ%a.', }; static const Map JA = { title: 'ローカリゼーション', thisIs: 'これは%aパッケージ、バージョン%aです。', }; } class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final FlutterLocalization _localization = FlutterLocalization.instance; @override void initState() { _localization.init( mapLocales: [ const MapLocale( 'en', AppLocale.EN, countryCode: 'US', fontFamily: 'Font EN', ), const MapLocale( 'km', AppLocale.KM, countryCode: 'KH', fontFamily: 'Font KM', ), const MapLocale( 'ja', AppLocale.JA, countryCode: 'JP', fontFamily: 'Font 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(), theme: ThemeData(fontFamily: _localization.fontFamily), ); } } class SettingsScreen extends StatefulWidget { const SettingsScreen({super.key}); @override State createState() => _SettingsScreenState(); } class _SettingsScreenState extends State { 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( children: [ 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); }, ), ), ], ), const SizedBox(height: 16.0), ItemWidget( title: 'Current Language', content: _localization.getLanguageName(), ), ItemWidget( title: 'Font Family', content: _localization.fontFamily, ), ItemWidget( title: 'Locale Identifier', content: _localization.currentLocale.localeIdentifier, ), ItemWidget( title: 'String Format', content: Strings.format( 'Hello %a, this is me %a.', ['Dara', 'Sopheak'], ), ), ItemWidget( title: 'Context Format String', content: context.formatString( AppLocale.thisIs, [AppLocale.title, 'LATEST'], ), ), ], ), ), ); } } class ItemWidget extends StatelessWidget { const ItemWidget({ super.key, required this.title, required this.content, }); final String? title; final String? content; @override Widget build(BuildContext context) { return Padding( padding: const EdgeInsets.only(bottom: 8.0), child: Row( ``` -------------------------------- ### Get Flutter Localization Instance (Dart) Source: https://pub.dev/packages/flutter_localization/index Obtains the singleton instance of the FlutterLocalization class. This instance is used to access all localization functionalities throughout the application. ```dart final FlutterLocalization localization = FlutterLocalization.instance; ``` -------------------------------- ### Flutter Widget: Displaying Localized Text Source: https://pub.dev/packages/flutter_localization/versions/0.3.0/example This Flutter widget uses a Row layout to display a title and content, potentially localized. It includes padding and alignment properties for visual structure. Dependencies include Flutter's material and foundation libraries. ```dart return Padding( padding: const EdgeInsets.only(bottom: 8.0), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ Expanded(child: Text(title ?? '')), const Text(' : '), Expanded(child: Text(content ?? '')), ], ), ); } } ``` -------------------------------- ### Fetch Git Commits Source: https://pub.dev/packages/flutter_localization/versions/0.3.2/score/log Fetches a specific commit from the 'origin' remote's 'main' branch with a shallow depth and no recursion into submodules. This is an efficient way to get the latest code. ```bash git fetch --depth 1 --no-recurse-submodules origin main ``` -------------------------------- ### Getting Language Name Feature Source: https://pub.dev/packages/flutter_localization/versions/0.2.3/changelog This snippet indicates the availability of a feature to retrieve the name of a language. ```dart // Usage example for getting language name would go here ``` -------------------------------- ### Getting Locale Identifier with LocaleExtension Source: https://pub.dev/packages/flutter_localization/versions/0.2.3/changelog This snippet demonstrates how to use LocaleExtension to obtain the locale identifier string, such as 'en_US'. ```dart String localeIdentifier = Locale('en', 'US').identifier; // Example: 'en_US' ``` -------------------------------- ### Configure Flutter Localization in StatefulWidget Source: https://pub.dev/packages/flutter_localization/versions/0.3.0/example This Dart snippet demonstrates how to configure the `FlutterLocalization` instance within a `StatefulWidget`. It initializes the localization service with a list of `MapLocale` objects, specifying language codes, locale data, country codes, and font families. It also sets up a callback for when the language is translated. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; class MyApp extends StatefulWidget { const MyApp({super.key}); @override State createState() => _MyAppState(); } class _MyAppState extends State { final FlutterLocalization _localization = FlutterLocalization.instance; @override void initState() { _localization.init( mapLocales: [ const MapLocale( 'en', AppLocale.EN, countryCode: 'US', fontFamily: 'Font EN', ), const MapLocale( 'km', AppLocale.KM, countryCode: 'KH', fontFamily: 'Font KM', ), const MapLocale( 'ja', AppLocale.JA, countryCode: 'JP', fontFamily: 'Font 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(), theme: ThemeData(fontFamily: _localization.fontFamily), ); } } ``` -------------------------------- ### Ensure Initialization Function (Dart) Source: https://pub.dev/packages/flutter_localization/versions/0.3.0/changelog Introduced in version 0.3.0 as a breaking change, this function is essential for ensuring the localization service is properly initialized before use. More detailed information is available in the README or EXAMPLE files. ```dart Future ensureInitialized(); // Example usage: await flutterLocalization.ensureInitialized(); ``` -------------------------------- ### Get Locale Identifier Source: https://pub.dev/packages/flutter_localization/versions/0.3.2/changelog Obtains the locale identifier string (e.g., 'en_US', 'fr_FR') from a Locale object using the LocaleExtension. This is useful for displaying or logging locale information. ```dart Locale('en', 'US').identifier; ``` -------------------------------- ### Translate Text and Get Localized String (Dart) Source: https://pub.dev/packages/flutter_localization/index Demonstrates how to trigger a language change and retrieve translated strings using the localization instance and extension methods. The `getString` extension is used with the context to fetch localized values. ```dart ElevatedButton( child: const Text('English'), onPressed: () { localization.translate('en'); }, ); AppLocale.title.getString(context); ``` -------------------------------- ### Added get language name feature (0.0.6) Source: https://pub.dev/packages/flutter_localization/versions/0.2.0/changelog Version 0.0.6 introduced functionality to retrieve the display name of a language. This is useful for presenting language options to users in a more user-friendly format. ```dart import 'package:flutter_localization/flutter_localization.dart'; // Assuming a method like getLanguageName exists: // String languageName = FlutterLocalization.instance.getLanguageName('en'); // Returns 'English' ``` -------------------------------- ### Get Localized String with Context Source: https://pub.dev/packages/flutter_localization/versions/0.3.1/changelog Retrieves a localized string using the `context` for the current locale. This extension method was introduced to simplify access to translated strings and requires a `context` parameter. ```dart context.getString("your_key"); ``` -------------------------------- ### Get Translated String Using Extension (Dart) Source: https://pub.dev/packages/flutter_localization/versions/0 Shows how to retrieve a translated string using the `getString` extension provided by the flutter_localization package. It requires the build context and a constant representing the localization key (e.g., from the `AppLocale` mixin). ```dart AppLocale.title.getString(context); ``` -------------------------------- ### Add LocaleExtension for Locale Identifier (0.1.12) Source: https://pub.dev/packages/flutter_localization/versions/0.2.0/changelog This version adds a `LocaleExtension` that provides a convenient way to get the locale identifier string (e.g., 'en_US', 'km_KH', 'ja_JP') from a `Locale` object. ```dart import 'package:flutter/material.dart'; import 'package:flutter_localization/flutter_localization.dart'; // Example usage: // Locale currentLocale = Locale('en', 'US'); // String localeIdentifier = currentLocale.getStringLocaleIdentifier(); // Returns 'en_US' ``` -------------------------------- ### Get Translated String using Extension (Dart) Source: https://pub.dev/packages/flutter_localization/versions/0.1.9/changelog Provides a convenient extension method on the BuildContext to easily retrieve translated strings. This method requires a context parameter to access localization data. ```dart extension LocalizationExtension on BuildContext { String getString(String key, {List? args, bool save = true}); } ``` -------------------------------- ### Define Localization Strings in Dart Source: https://pub.dev/packages/flutter_localization/versions/0.3.0/example This Dart code defines a mixin `AppLocale` to hold localization strings for different languages (English, Khmer, Japanese). It uses a map structure where keys are string identifiers and values are the translated strings, including placeholders like '%a' for dynamic content. ```dart mixin AppLocale { static const String title = 'title'; static const String thisIs = 'thisIs'; static const Map EN = { title: 'Localization', thisIs: 'This is %a package, version %a.', }; static const Map KM = { title: 'ការធ្វើមូលដ្ឋានីយកម្ម', thisIs: 'នេះគឺជាកញ្ចប់%a កំណែ%a.', }; static const Map JA = { title: 'ローカリゼーション', thisIs: 'これは%aパッケージ、バージョン%aです。', }; } ``` -------------------------------- ### Get Localized String using Extension Source: https://pub.dev/packages/flutter_localization/versions/0.3.2/changelog Retrieves a localized string using an extension method on the BuildContext. This provides a convenient way to access translated strings within your Flutter widgets. ```dart context.getString(translation_key); ```