Skip to content

Latest commit

 

History

History

localization

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

localization

Plugin:

Dependency:

flutter_localizations:
  sdk: flutter

Steps

  1. Setup delegates at the root of app

import 'package:localization/generated/i18n.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
MaterialApp(
    title: 'Localization Demo',
    localizationsDelegates: [
        S.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
    ],
    supportedLocales: S.delegate.supportedLocales,
    home: Scaffold(
        body: HomePage(),
));
  1. Setup arb files

Plugin will help you generate English arb file automatically.

File will be located at res/values/strings_en.arb.

{
  "localization_demo": "Localization Demo",
  "button": "Button"
}

You can now refer the values.

AppBar(
	title: Text(S.of(context).localization_demo),
	backgroundColor: Colors.green,
)
  1. LocaleOverrideDelegate

Adding this delegate can help us to change locale efficiently.

class SpecifiedLocalizationDelegate extends LocalizationsDelegate<S> {
  final Locale overriddenLocale;

  const SpecifiedLocalizationDelegate(this.overriddenLocale);

  @override
  bool isSupported(Locale locale) => overriddenLocale != null;

  @override
  Future<S> load(Locale locale) =>
      GeneratedLocalizationsDelegate().load(overriddenLocale);

  @override
  bool shouldReload(SpecifiedLocalizationDelegate old) => true;
}

Initialize it with null locale and put into localizationsDelegates

_localeOverrideDelegate = SpecifiedLocalizationDelegate(null);

localizationsDelegates: [
	_localeOverrideDelegate,
	S.delegate,
	GlobalMaterialLocalizations.delegate,
	GlobalWidgetsLocalizations.delegate,
],

Reassign it when the application's locale changed.

_localeOverrideDelegate = SpecifiedLocalizationDelegate(newLocale);