Thứ Tư, 17 tháng 2, 2021

Multi language Flutter

 

Multi language Flutter

 


import 'dart:io';
import
'package:flutter/material.dart';
import
'package:flutter_localizations/flutter_localizations.dart';

void main() async {
 
WidgetsFlutterBinding.ensureInitialized();

 
// Get the initial locale values
 
final String defaultSystemLocale = Platform.localeName;
 
final List<Locale> systemLocales = WidgetsBinding.instance.window.locales;

 
// Define locales that our app supports (no country codes, see comments below)
 
final appSupportedLocales = <Locale>[
   
Locale('ru'),
   
Locale('en'),
  ];

 
final MyApp myApp = MyApp(defaultSystemLocale, systemLocales);

  runApp(
     
MaterialApp(
        title:
'MyApp',
        home:
myApp,
        supportedLocales:
appSupportedLocales,
        localizationsDelegates: [
          
// These are default localization delegates that implement the very basic translations for standard controls and date/time formats.
         
GlobalMaterialLocalizations.delegate,
         
GlobalWidgetsLocalizations.delegate,
        ],
      )
  );
}

class MyApp extends StatefulWidget {
 
// Store initial locale settings here, they are unchanged
 
final String initialDefaultSystemLocale;
 
final List<Locale> initialSystemLocales;

 
MyApp(this.initialDefaultSystemLocale, this.initialSystemLocales);

 
@override
 
_MyAppState createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
 
// Store dynamic changeable locale settings here, they change with the system changes
 
String currentDefaultSystemLocale;
 
List<Locale> currentSystemLocales;

 
// Here we read the current locale values
 
void setCurrentValues() {
   
currentSystemLocales = WidgetsBinding.instance.window.locales;
   
currentDefaultSystemLocale = Platform.localeName;
  }

 
@override
 
void initState() {
   
// This is run when the widget is first time initialized
   
WidgetsBinding.instance.addObserver(this); // Subscribe to changes
   
setCurrentValues();
   
super.initState();
  }

 
@override
 
void didChangeLocales(List<Locale> locale) {
   
// This is run when system locales are changed
   
super.didChangeLocales(locale);
   
// Update state with the new values and redraw controls
   
setState(() {
      setCurrentValues();
    });
  }

 
@override
 
Widget build(BuildContext context) {
   
return Scaffold(
      appBar:
AppBar(),
      body:
Column(
        crossAxisAlignment:
CrossAxisAlignment.start,
        mainAxisAlignment:
MainAxisAlignment.start,
        children: <
Widget>[
         
Text('Initial system default locale: ${widget.initialDefaultSystemLocale}.'),
         
Text('Initial language code: ${widget.initialDefaultSystemLocale.split('_')[0]}, country code: ${widget.initialDefaultSystemLocale.split('_')[1]}.'),
         
Text('Initial system locales:'),
         
for (var locale in widget.initialSystemLocales) Text(locale.toString()),
         
Text(''),
         
Text('Current system default locale: ${currentDefaultSystemLocale}.'),
         
Text('Current system locales:'),
         
for (var locale in currentSystemLocales) Text(locale.toString()),
         
Text(''),
         
Text('Selected application locale: ${Localizations.localeOf(context).toString()}.'),
         
Text(''),
         
Text('Current date: ${Localizations.of<MaterialLocalizations>(context, MaterialLocalizations).formatFullDate(DateTime.now())}.'),
         
Text('Current time zone: ${DateTime.now().timeZoneName} (offset ${DateTime.now().timeZoneOffset}).'),
        ],
      ),
    );
  }
}

 

Custom Lang class:

1. BaseLanguage.dart

import 'dart:io';

import 'lang_en.dart';
import 'lang_vi.dart';

abstract class BaseLanguage {
 
String hello;
}

/// Usage:
/// BaseLanguage lang;
/// @override
//   void initState() {
//     lang = LangUtils.getLang();
//     String testTitle = lang.hello;
//   }

class LangUtils {
 
static BaseLanguage getLang() {
   
String defaultLocale = Platform.localeName; //ex: en_US
   
print('defaultLocale = $defaultLocale');

   
switch (defaultLocale) {
     
case 'en_US':
       
return LangEn();
     
default:
       
return LangVi();
    }
  }
}

 

2. LangEn.dart

import 'BaseLanguage.dart';

// en_US
class LangEn implements BaseLanguage {
 
@override
 
String hello = 'hello';
}

 

3. LangVi.dart

import 'BaseLanguage.dart';

// vi_VN
class LangVi implements BaseLanguage {
 
@override
 
String hello = 'Xin chào';
}