internationalization 1.2.0

internationalization #

A project to easily implement internationalization on flutter projects

Configure Internationalization #

Import internationalization package on main.dart.

import 'package:internationalization/internationalization.dart';

call suportedLocales in constructor of your main widget and add locales you might like to use. You may pass language and country code, or just language code.

class App extends StatelessWidget {
  App() {
    suportedLocales.addAll([
      const Locale('pt'),
      const Locale('en', 'US'),
    ]);
  }
}

Now pass the suportedLocales to suportedLocales and the delegates to localizationsDelegates. For InternationalizationDelegate you pass a default locale and a path where the JSON files will be placed

@override
Widget build(BuildContext context) {
    return MaterialApp(
        supportedLocales: suportedLocales,
        localizationsDelegates: [
            InternationalizationDelegate(
                defaultLocale: Locale('pt'),
                path: "assets/strings",
            ),
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
        ],
    );
}

In pubspec.yaml put the path that you've passed to InternationalizationDelegate

flutter:
  assets:
    - ./assets/strings/

Usage #

Strings.of(context).valueOf("key") //To get a simple string
Strings.of(context).valueOf("key", args: ["A", "B", "C"]) //To get a interpoled string
Strings.of(context).pluralOf("key", 0) //To get a plural string
Strings.of(context).pluralOf("key", 0, args: ["A", "B", "C"]) //To get a plural interpoled string
Strings.of(context).valueOf("key", args: ["A", "B", "C"], namedArgs: {"named_arg_key": "Named arg"}) //To get a interpoled name string

JSON file #

Crete JSON files with locale language code and country code. Ex.: pt.json, en_US.json, etc... Note: create JSON files inside folder you passed to InternationalizationDelegate

{
    "simple_string": "Simple string value",
    "interpolation_string": "Interpoleted {0} string {1}",
    "simple_plurals" : {
        "zero": "No information",
        "one": "A item",
        "other": "Many itens"
    },
    "interpolation_plurals": {
        "zero": "No information {0} with {1}",
        "one": "A item {0} with {1}",
        "other": "Many itens {0} with {1}"
    },
    "interpolation_string_with_named_args": "Interpoleted {0} string with ::named_arg_key::"
}

[1.2.0] (Agust 16, 2019)

  • Removed need to pass country code

[1.1.0+1] (Agust 16, 2019)

  • README example for named interpolation

[1.1.0] (Agust 16, 2019)

  • Added suport for named interpolation

[1.0.1] (Agust 6, 2019)

  • Null safety for locales

[1.0.0+1] (July 27, 2019)

  • Refactor code

[1.0.0] (July 25, 2019)

  • Initial release

example/README.md

internationalization_example #

A example project for internationalization

Getting Started #

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Use this package as a library

1. Depend on it

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


dependencies:
  internationalization: ^1.2.0

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:internationalization/internationalization.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
50
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]
75
Learn more about scoring.

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

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/internationalization.dart.

Run flutter format to format lib/internationalization.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
intl 0.15.8
meta 1.1.6 1.1.7
path 1.6.2 1.6.4
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test