fast_localization 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

fast_localization #

Fast localization solution for flutter apps using Dart's Map. (no context needed for every single localization).

Getting Started #

Add the following to your dependencies in pubspec.yaml

fast_localization: <last_version>

Examples #

Minimal (Out of the box fast_localization MaterialApp) #

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

void main() async {
  final en = {
    "title": "Demo",
    "welcome": "Hello World!",
  };

  final ar = {
    "title": "عرض",
    "welcome": "أهلاً بالعالم!",
  };

  final locales = {
    Locale('en'): en,
    Locale('ar'): ar,
  };

  await Localization.load(locales);

  runApp(LocalizationApp(
    title: () => Localization.translate('title'),
    home: () => HomeScreen(),
  ));
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      ...
    );
  }
}

When to use:

When you want the home and title of MaterialApp only.

MaterialApp(title: 'App Name', home: HomeScreen());

Full example:

Check out example/fast_localization.dart

Flexible #

You need to install flutter_localizations

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

void main() async {
  final en = {
    "title": "Demo",
    "welcome": "Hello World!",
  };

  final ar = {
    "title": "عرض",
    "welcome": "أهلاً بالعالم!",
  };

  final locales = {
    Locale('en'): en,
    Locale('ar'): ar,
  };

  await Localization.load(locales);

  runApp(LocalizationApp(
    child: (context) => MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      ...
    );
  }
}

When to use:

When you want to use own MaterialApp, CupertinoApp, or WidgetsApp.

Full example:

Check out example/flexible.dart

Using LocalizationApp(...) #

Minimal #

ArgumentTypeExampleRequired
titleFunction => String() => t('title')Yes
homeFunction => Widget() => HomeScreen()Yes
themeThemeDataThemeData.dark()No

Flexible #

ArgumentTypeExampleRequired
childFunction(BuildContext) => Widget(context) => MyApp()Yes

You need to use either Minimal or Flexible

Using Localization #

Functions #

FunctionParametersReturns
Localization.load(...)(Map<Locale, Map<String, dynamic>> languages)Future
Localization.changeLocale(...)(Locale locale, [BuildContext context])Future
Localization.translate(...)(String key, [Map<String, String> params])String
Localization.t(...) (Alias)(String key, [Map<String, String> params])String

Fields #

FieldTypeDescription
Localization.localeLocaleGet current locale
Localization.supportedLocalesList < Locale >Get supported locales
Localization.languageCountryStringGet current locale to string, e.g. 'en_US'

There is a global alias for Localization.translate(key, params) which is t(key, params)!

Localization Declarations #

Flat #

final en = {
  "title": "Demo",
  "welcome": "Hello World!",
};

Usage:

Localization.translate('welcome');
Localization.t('welcome');
t('welcome');

Nested #

final en = {
  "app": {
    "name": "Demo",
    "description": "A demo app",
  },
};

Usage:

Localization.translate('app.name');
Localization.t('app.name');
t('app.name');

With Parameters #

final en = {
  "info": {
    "name": "My name is {{name}}",
  },
};

Usage:

Localization.translate('info.name', {"name": "Hasan"});
Localization.t('info.name', {"name": "Hasan"});
t('info.name', {"name": "Hasan"});

TODO #

  • [ ] Locale fallback
  • [x] Parameters support
  • [x] Nested localizations
  • [x] Auto save/load local
  • [x] Language code only support Locale('en')
  • [x] Language and country code support Locale('en', 'US')
  • [x] Add tests

PRs are always welcome and appreciated!

[0.3.0] - 2020-02-09 #

  • Hide internal files

[0.2.0] - 2020-02-09 #

  • Add usages

[0.1.0] - 2020-02-09 #

  • Add translate funcion alias Localization.t(key, params)
  • Add translate funcion alias t(key, params)!

[0.0.4] - 2020-02-09 #

  • Make changeLocale() testable
  • Add tests

[0.0.3] - 2020-02-09 #

  • Add examples

[0.0.2] - 2020-02-08 #

  • Add proper description
  • Formatting codes

[0.0.1] - 2020-02-08 #

  • Initial release.

example/fast_localization.dart

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

// 2. import fast localization
import 'package:fast_localization/fast_localization.dart';

// 3. add async to main function
void main() async {
  // 4. declare locales as you like, either as follows or import them
  final locales = {
    Locale('en'): {
      "title": "Demo",
      "welcome": "Hello World!",
    },
    Locale('ar'): {
      "title": "عرض",
      "welcome": "أهلاً بالعالم!",
    },
  };

  // 5. load fast localization
  await Localization.load(locales);

  // 6. use LocalizationApp
  runApp(LocalizationApp(
    title: () => Localization.translate(
        'title'), // MaterialApp(title) | required with home: only
    home: () => HomeScreen(), // MaterialApp(home) | required with home: only
    theme: ThemeData.light(), // MaterialApp(theme) | optional
  ));
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(Localization.translate('title')),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('😉', style: TextStyle(fontSize: 50)),
            Text(Localization.translate('title'),
                style: TextStyle(fontSize: 25)),
            SizedBox(height: 60),
            FlatButton(
              child: Text('English'),
              onPressed: () {
                Localization.changeLocale(Locale('en'), context);
              },
            ),
            FlatButton(
              child: Text('عربي'),
              onPressed: () {
                Localization.changeLocale(Locale('ar'), context);
              },
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  fast_localization: ^0.3.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:fast_localization/fast_localization.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
17
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]
59
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform android

Because:

  • package:fast_localization/fast_localization.dart that imports:
  • package:fast_localization/src/localization_app.dart that imports:
  • package:fast_localization/src/localization.dart that imports:
  • package:fast_localization/src/localization_settings.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: linux

Package does not support Flutter platform ios

Because:

  • package:fast_localization/fast_localization.dart that imports:
  • package:fast_localization/src/localization_app.dart that imports:
  • package:fast_localization/src/localization.dart that imports:
  • package:fast_localization/src/localization_settings.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: linux

Package does not support Flutter platform macos

Because:

  • package:fast_localization/fast_localization.dart that imports:
  • package:fast_localization/src/localization_app.dart that imports:
  • package:fast_localization/src/localization.dart that imports:
  • package:fast_localization/src/localization_settings.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: linux

Package does not support Flutter platform web

Because:

  • package:fast_localization/fast_localization.dart that imports:
  • package:fast_localization/src/localization_app.dart that imports:
  • package:fast_localization/src/localization.dart that imports:
  • package:fast_localization/src/localization_settings.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: linux

Package does not support Flutter platform windows

Because:

  • package:fast_localization/fast_localization.dart that imports:
  • package:fast_localization/src/localization_app.dart that imports:
  • package:fast_localization/src/localization.dart that imports:
  • package:fast_localization/src/localization_settings.dart that imports:
  • package:shared_preferences/shared_preferences.dart that declares support for platforms: android, ios, linux, macos, web

Package not compatible with SDK dart

Because:

  • fast_localization that is 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
shared_preferences ^0.5.6+1 0.5.8
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
flutter_web_plugins 0.0.0
intl 0.16.1
meta 1.1.8 1.2.2
path 1.6.4 1.7.0
path_provider_linux 0.0.1+2
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
shared_preferences_linux 0.0.2+1
shared_preferences_macos 0.0.1+10
shared_preferences_platform_interface 1.0.4
shared_preferences_web 0.1.2+7
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test