simple_localization 1.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • new57

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)),
  ],
)

1.1.1 #

  • Fixed class name 'BasicLocalizationsDelegate' for SimpleLocalizationsDelegate

1.1.0 #

  • Added customValues in SimpleLocalizations to allow you to customize just a few messages.

1.0.0 #

  • Initial version

example/README.md

example_localization #

Sample app to show how simple_localization package works

example_localization

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  simple_localization: ^1.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:simple_localization/simple_localization.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
14
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
57
Learn more about scoring.

We analyzed this package on Dec 4, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/classes/simple_localization.dart.

Run flutter format to format lib/classes/simple_localization.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test
pedantic ^1.8.0