simple_localization 1.1.1

Flutter Android iOS web

Package to make it easy to implement localizations in your packages and applications.

simple_localization #

Implement localizations in your packages and applications simply.

import 'package:simple_localization/simple_localization.dart';

How to internationalize your app or package #

First, I recommend that you declare an enumeration with the message names to make it easier to find messages

enum WidgetMessages { message1, message2 }

Below is an example localization, with the translation of message1 andmessage2 into English, Spanish and Portuguese.

If the application language does not match the supported languages that are declared in supportedLocales, the default language entered indefaultLocale will be used.

class ExampleLocalizations extends SimpleLocalizations {
  static ExampleLocalizations of(BuildContext context) {
    return SimpleLocalizations.of<ExampleLocalizations>(context, (locale) => ExampleLocalizations(locale));
  }

  ExampleLocalizations(Locale locale) : super(locale);

  @override
  Locale get defaultLocale => Locale('en');

  @override
  Iterable<Locale> get suportedLocales => [
    Locale('en'),
    Locale('es'),
    Locale('pt'),
  ];

  @override
  Map<dynamic, Map<dynamic, String>> get localizedValues => {
    'en': {
      WidgetMessages.message1: 'First message',
      WidgetMessages.message2: 'Second message',
    },
    'es': {
      WidgetMessages.message1: 'Primer mensaje',
      WidgetMessages.message2: 'Segundo mensaje',
    },
    'pt': {
      WidgetMessages.message1: 'Primeira mensagem',
      WidgetMessages.message2: 'Segunda mensagem',
    }
  };
}

/// This class is only needed if you are developing a package, and you want to
/// make it available to you to develop a way to customize messages.
class ExampleLocalizationsDelegate extends SimpleLocalizationsDelegate<ExampleLocalizations> {
  ExampleLocalizationsDelegate(customLocalization) : super(customLocalization);
}

Below will be shown how to get messages according to app location

Text(DemoLocalizations.of(context)[WidgetMessages.message1]);

It is not necessary to declare localization in localizationsDelegates of MaterialApp, but it is important that you enter supportedLocales to specify the languages supported by your application.

When specifying other locations (other than 'en'), you will need to add in the supportedLocales ofMaterialApp the locations GlobalMaterialLocalizations.delegate andGlobalWidgetsLocalizations.delegate, missing these locations will generate an application error.

In your pubspec.yaml add follow dependencie:

  flutter_localizations:
    sdk: flutter

And in your MaterialApp, add localizations in supportedLocales

MaterialApp(
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
)

Customize package locations #

It is often used in package internationalizations, when the developer needs to modify the default messages used in package, for that you need to extend the location of your package, and implement BasicLocalizationsDelegate.

First let's create the custom internationalization that will extend the default package location.

class CustomLocalization extends ExampleLocalizations {
  CustomLocalization(Locale locale) : super(locale);

  @override
  Map<dynamic, Map<dynamic, String>> get customValues => {
    'en': {
      WidgetMessages.message1: 'Custom first message',
      WidgetMessages.message2: 'Custom second message',
    },
    'es': {
      WidgetMessages.message1: 'Primer mensaje personalizado',
      WidgetMessages.message2: 'Segundo mensaje personalizado',
    },
    'pt': {
      WidgetMessages.message1: 'Primeira mensagem personalizada',
      WidgetMessages.message2: 'Segunda mensagem personalizada',
    }
  };
}

And finally, inform the delegate in localizationsDelegates of your apps MaterialApp.

MaterialApp(
  localizationsDelegates: [
    ExampleLocalizationsDelegate((locale) => CustomLocalization(locale)),
  ],
)
0
likes
100
pub points
28%
popularity

Package to make it easy to implement localizations in your packages and applications.

Repository (GitHub)
View/report issues

Documentation

API reference

Uploader

ricardo.crescenti@gmail.com

License

unknown (LICENSE)

Dependencies

flutter

More

Packages that depend on simple_localization