polylingual 0.0.2

polylingual #

PolyLingual is a plug-in that provides a means of storing and updating String translations for a Flutter app. These translations are defined and updated using a simple JSON file similar to the one below.

{
    "en": {
        "greeting": "Hello, how are you?",
        "welcome": "you're welcome, my friend",
        "thankyou": "thank you very much",
        "good": "very well",
        "about": "Information"

    },
    "es": {
        "greeting": "¿Hola como estas?",
        "welcome": "de nada amiga",
        "thankyou": "muchas gracias",
        "good": "muy bien",
        "about": "Información"

    }
}

How PolyLingual Works #

The first thing you MUST do is initialise PolyLingual with a default Translation map (or load the existing one). This is done in main() before the main widget is loaded to make sure we have all the strings available.

void main() async {

  // set our required default(fallback) language and initialise PolyLingual
  PolyLingual.defaultLanguageCode = "es";
  await PolyLingual.initialise("res/strings.json");


  // run your app here
  runApp(DemoApp());
  
}

You also need to do the following to the project pubspec.yaml file

  flutter_localizations:
    sdk: flutter 

as well as define and add an initial translation string file as an asset (see example for more details)

  assets:
  #  - images/a_dot_burr.jpeg
  #  - images/a_dot_ham.jpeg
    - res/strings.json

Then, whenever you need to display a localised string somewhere just use

PolyLingual.of(context).string("greeting")

where 'greeting' is a key in the translation file (take a look at the example strings.json file for the correct format).

Updating the Translation Map #

You can also update the app translation map using a new translation file in case you need to update an existing string or correct a typo. In the example this is hard coded, but your app could have check for updates button that would download the new file and auto update the translations.

iOS #

Please note that for iOS you will also need to add any supported languages to the Runner info.plist file. Read here for more info

Getting Started #

Take a look at the example project to see how to use PolyLingual to create a multi language app that can be updated simply by providng an updated JSON file.

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

[0.0.2] - 4 Feb 2019.

  • Updated documents and app description.

[0.0.1] - 4 Feb 2019.

  • Initial release.

example/lib/main.dart

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

/// Load the First Widget and initialise PolyLingual
void main() async {
  // set our required default(fallback) language and initialise PolyLingual
  PolyLingual.defaultLanguageCode = "es";
  await PolyLingual.initialise("res/strings.json");

  // run your app here
  runApp(DemoApp());
}

/// Simple Demo of using PolyLingual to set up app translations
/// and then update the translation file
///
/// for the demo we support 2 languages English and Spanish
class DemoApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'DemoApp',
      home: LanguageScreen(),
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale("es"),
        const Locale("en"),
      ],
    );
  }
}

// Simple Screen Widget to show the use of PolyLingual
class LanguageScreen extends StatefulWidget {
  @override
  _LanguageScreenState createState() => _LanguageScreenState();
}

class _LanguageScreenState extends State<LanguageScreen> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text("PolyLingual Demo")),
        body: ListView(
          padding: EdgeInsets.all(30.0),
          children: <Widget>[
            Text(
              PolyLingual.of(context).string("greeting"),
              style: TextStyle(color: Colors.blue),
            ),
            Text(
              PolyLingual.of(context).string("good"),
              style: TextStyle(color: Colors.orange),
            ),
            Text(
              PolyLingual.of(context).string("thankyou"),
              style: TextStyle(color: Colors.blue),
            ),
            Text(
              PolyLingual.of(context).string("welcome"),
              style: TextStyle(color: Colors.orange),
            ),
            RaisedButton(
              /// The example uses a defined map to update translations, but this could also come from a downloaded file
              onPressed: () async {
                Map update = {
                  "en": {
                    "greeting": "Hi, how are you?",
                    "welcome": "Thanks very much",
                    "thankyou": "cheers mate!",
                    "good": "Not So bad",
                    "about": "Information"
                  },
                  "es": {
                    "greeting": "¿Hola como estas?",
                    "welcome": "de nada amiga",
                    "thankyou": "muchas gracias",
                    "good": "muy bien",
                    "about": "Información"
                  }
                };
                await PolyLingual.updateLanguageStrings(update);
                setState(() {});
              },
              child: Text("Update EN translation data"),
            ),
          ],
        ));
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  polylingual: ^0.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:polylingual/polylingual.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
46
Learn more about scoring.

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

  • Dart: 2.5.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.2

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (path_provider).

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
path_provider ^0.5.0 0.5.0+1 1.3.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test