fast_localization 0.3.0

Flutter Android iOS web

Fast localization solution for flutter apps using Dart's `Map`

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!

1
likes
100
pub points
42%
popularity

Publisher

highestweb.com

Fast localization solution for flutter apps using Dart's `Map`

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (LICENSE)

Dependencies

flutter, flutter_localizations, shared_preferences

More

Packages that depend on fast_localization