json_intl 1.0.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

json_intl #

Flutter Internationalization library based on Json files

Getting Started #

Add a new asset folder to your pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter
  json_intl: [version]

flutter:
  assets:
    - assets/intl/

Add the translation delegate settings to your MaterialWidget and the list of supported languages:

MaterialApp(
  localizationsDelegates: const [
    JsonIntlDelegate(),
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: const [
    Locale('en'),
    Locale('fr'),
  ],
    home: ...,
);

On iOS, an extra step is necessary: add supported languages to ios/Runner/Info.plist:

<key>CFBundleLocalizations</key>
<array>
    <string>en</string>
    <string>fr</string>
</array>

Create the translation files in assets/intl/:

  • strings.json is the default and fallback file. Any translation not found in another file will be taken here.
  • strings-XX.json one file for each language. strings-fr.json French translations, strings-de.json for German, etc.
  • strings-XX-AA.json can also be used for specific countries, like strings-en-uk.json

Using the translations #

The content of the files is a json dictionary containing a key / value list:

{
    "app_name": "Flutter Demo",
    "increment": "Increment",
    "title": "Flutter Demo Home Page"
}

to get the values in the dart code, use:

JsonIntl.of(context).get('app_name');

or

context.tr('app_name');

Plurals #

The json entry will look like this:

{
    "cart": {
        "one": "{{ count }} item in your Shopping cart",
        "other": "{{ count }} items in your Shopping cart"
    },
}

Other values are supported for specific values depending on the language: "zero", "one", "two", "few", "many", "other"

To use it in the application:

JsonIntl.of(context).count(itemCount, 'cart');

The variable {{ count }} is automatically populated with itemCount.

Gender #

The json entry will look like this:

{
    "child": {
        "male": "The boy",
        "female": "The girl",
        "neutral": "The kid"
    },
}

To use it in the application:

JsonIntl.of(context).gender(JsonIntlGender.female, 'child');

Mixing Plural and Gender #

The json entry will look like this:

{
    "child": {
        "male": {
          "one": "a boy",
          "many": "{{ count }} boys",
        },
        "female": {
          "one": "a girl",
          "many": "{{ count }} girls",
        },
        "neutral": {
          "one": "a child",
          "many": "{{ count }} children",
        }
    },
}

To use it in the application:

JsonIntl.of(context).translate('child', gender: JsonIntlGender.female, count: 3);

Changelog #

1.0.2 #

  • Add German strings to the example
  • Use external library for mustache

1.0.1 #

  • Lower dependency requirements

1.0.0 #

  • Initial release.

example/lib/main.dart

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

// ignore_for_file: public_member_api_docs

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      // This replaces your [title] attribute, as we need a context to get the
      // translations
      onGenerateTitle: (BuildContext context) =>
          JsonIntl.of(context).get('app_name'),
      home: MyHomePage(),
      localizationsDelegates: const [
        JsonIntlDelegate(), // We add the intl delegate here
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      // Set the list of supported languages in this list.
      // On iOS, you will have to specify the list of languages in the file
      // Info.plist
      supportedLocales: const [
        Locale('de'),
        Locale('en'),
        Locale('fr'),
      ],
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        // A simple translated title
        title: Text(JsonIntl.of(context).get('title')),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            // A string translated depending on a number to count something
            Text(JsonIntl.of(context).count(
              _counter,
              'pushed',
              strict: false,
            )),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        // We can use the BuildContext extension too:
        tooltip: context.tr('increment'),
        child: const Icon(Icons.add),
      ),
    );
  }
}

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate json_intl

2. Use it

The package has the following executables:


$ json_intl

Use this package as a library

1. Depend on it

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


dependencies:
  json_intl: ^1.0.2

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

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • json_intl that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
args ^1.5.2 1.6.0
dart_style ^1.2.9 1.3.6
flutter 0.0.0
intl ^0.16.0 0.16.1
logging ^0.11.0 0.11.4
meta ^1.1.0 1.1.8 1.2.2
path ^1.6.4 1.7.0
simple_mustache ^1.0.0 1.0.0
Transitive dependencies
_fe_analyzer_shared 5.0.0
analyzer 0.39.12
async 2.4.2
charcode 1.1.3
collection 1.14.12 1.14.13
convert 2.1.1
crypto 2.1.5
csslib 0.16.1
glob 1.2.0
html 0.14.0+3
js 0.6.2
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
pub_semver 1.4.4
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
watcher 0.9.7+15
yaml 2.2.1
Dev dependencies
flutter_test
pedantic ^1.8.0 1.9.0 1.9.2
pubspec_extract ^1.2.0