applocale 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 60

applocale #

A Flutter plugin to enable support for internationalization (i18n) or different language with json files.

Usage #

A simple usage example:

Project Structure #

project_structure

lang.json contents

//en.json
{
  "title": "Awesome!",
  "hello": "Hello",
  "message": "This is English!!!",
  "subDetail": {
    "greeting": "{hello} {name}!!!",
    "runtimeText": "I have proof, you can replace {replacement}"
  }
}

//bn.json
{
  "title": "অভূতপূর্ব!",
  "hello": "নমস্কার",
  "message": "ইহা বাংলা!!!",
  "subDetail": {
    "runtimeText": "আমি জানি যে {replacement}কে যে কোনও দিন চলে যেতে হবে।"
  }
}

Add the language directory as assets in pubspec.yaml

# pubspec.yaml
# add dependencies
dependencies:
  applocale: <latest-version>

flutter:
  # add the whole directory containing language json files as an asset
  assets:
    - i18n/

Now the code

// main.dart
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:applocale/applocale.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

// define supported Language lists
var get _supportedLanguages => ["en", "en_us", "bn"];
var get _defaultLanguage => "en";

void main(List<String> args) => runApp(FlutterDemoApp());

class FlutterDemoApp extends StatefulWidget {
  @override
  _FlutterDemoApp createState() => _FlutterDemoApp();
}

class _FlutterDemoApp extends State<FlutterDemoApp> {
  LocaleDelegate _localeDelegate;
  
  @override
  void initState() {
    super.initState();
      // initialize _localeDelegate
    _localeDelegate = LocaleDelegate.init(_supportedLanguages, _defaultLanguage);
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
        supportedLocales: _localeDelegate.supportedLocales, // Step I
        localizationsDelegates: [
          _localeDelegate, // Step II
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        title: 'Flutter Demo',
        home: FlutterDemo(),
      );
}

class FlutterDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // since LocaleDelegate is already initialized & ready
    var appLocale = LocaleDelegate.of(context); // Step III
    // In case some additional values can be set now
    appLocale.updateValue({'name': 'জয়ন্তী'});

    return Scaffold(
      appBar: AppBar(title: Text(appLocale.localValue('title'))),
      body: ListView(
        children: <Widget>[
          Center(child: Text(appLocale.localValue('subDetail.greeting'))),
          Center(child: Text(
            appLocale.localValue('subDetail.runtimeText', {'replacement': 'Individual'}))),
          Center(child: Text(appLocale.localValue('message'))),
        ],
      ),
    );
  }
}

Project Structure #

App with EnglishChange system languageApp with Bengali

App with English > Change system language > App with Bengali

App with English

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.3 #

  • minor refactoring & package upgrade

1.0.2 #

  • degrade package path

1.0.1 #

  • upgrade package path

1.0.0 #

  • first major release :)

0.3.16 #

  • added LocaleDelegate.changeLanguage

0.3.15 #

  • update documentation

0.3.14 #

  • Fix bugs: introducing deep copy with _updateMap

0.3.13 #

  • update AppLocale.localValue to support runtime substitution with additional values

0.3.12 #

  • upgrade interpolation package

0.3.11 #

  • upgrade interpolation package
  • update example & documentation

0.3.10 #

  • support for interpolation (string substitution) inside language.json files

0.3.9 #

  • updated AppLocale.localValue()

0.3.8 #

  • Added community documents

0.3.7 #

  • update API documentation

0.3.6 #

  • bug fixes
  • API documentation

0.3.5 #

  • fixing minor bugs

0.3.4 #

  • removed many sticky static substances
  • added AppLocale.updateValue()
  • removed AppLocale.delegate()

0.3.3 #

  • LocaleDelegate

0.3.2 #

  • null check for getLocale

0.3.1 #

  • change in example & test

0.3.0 #

  • new LocaleDelegate.init() for compact initiation
  • updated Documentation
  • updated example
  • updated tests

0.2.0 #

  • out of dev cycle
  • updated LocaleDelegate._getSupportedLocale()
  • added preliminary tests
  • updated Readme

0.1.3-dev+1 #

  • update example

0.1.2-dev+1 #

  • 1st set of implementation

0.1.1-dev+1 #

  • Initial version, nominal changes to check pub publish

0.1.0-dev+1 #

  • Initial version, created by Stagehand

example/applocale_example.dart

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:applocale/applocale.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

// define supported Language lists
Map<String, String> get _supportedLanguages =>
    <String, String>{"en": "English", "en_us": "English(USA)", "bn": "Bengali"};
String get _defaultLanguage => "en";

void main(List<String> args) => runApp(FlutterDemoApp());

class FlutterDemoApp extends StatefulWidget {
  @override
  _FlutterDemoApp createState() => _FlutterDemoApp();
}

class _FlutterDemoApp extends State<FlutterDemoApp> {
  LocaleDelegate _localeDelegate;

  List<String> _getSupportedLanguages() =>
      _supportedLanguages.entries.map((l) => l.key).toList();

  @override
  void initState() {
    super.initState();
    // initialize _localeDelegate
    _localeDelegate =
        LocaleDelegate.init(_getSupportedLanguages(), _defaultLanguage);
  }

  @override
  Widget build(BuildContext context) => MaterialApp(
        supportedLocales: _localeDelegate.supportedLocales, // Step I
        localizationsDelegates: [
          _localeDelegate, // Step II
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate
        ],
        title: 'Flutter Demo',
        home: FlutterDemo(),
      );
}

class FlutterDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // since LocaleDelegate is already initialized & ready
    var appLocale = LocaleDelegate.of(context); // Step III
    // In case some additional values can be set now
    appLocale.updateValue({'name': 'জয়ন্তী'});

    return Scaffold(
      appBar: AppBar(title: Text(appLocale.localValue('title'))),
      body: ListView(
        children: <Widget>[
          Center(child: Text(appLocale.localValue('subDetail.greeting'))),
          Center(
              child: Text(appLocale.localValue(
                  'subDetail.runtimeText', {'replacement': 'Individual'}))),
          Center(child: Text(appLocale.localValue('message'))),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  applocale: ^1.0.3

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

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

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

because of import path [applocale] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
interpolation ^1.0.1 1.0.1
path ^1.6.4 1.6.4 1.7.0
Transitive dependencies
collection 1.14.12 1.14.13
intl 0.16.1
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test