flutter_easy_translate 1.0.6 copy "flutter_easy_translate: ^1.0.6" to clipboard
flutter_easy_translate: ^1.0.6 copied to clipboard

Flutter Easy Translate is a fully featured localization / internationalization (i18n) library for Flutter.

Fully featured localization / internationalization (i18n) library for Flutter.

pub package License: MIT Flutter.io

Flutter Easy Translate #

Flutter Easy Translate is a fully featured localization / internationalization (i18n) library for Flutter.

It lets you define translations for your content in different languages and switch between them easily.

⚠️ This is a maintained and enhanced fork of the original Flutter Translate created by Jesway Labs, LLC, licensed under the MIT License. This project builds upon the solid foundation of the original Flutter Translate, adding new features, improvements, and ongoing maintenance.

Features #

  • Very easy to use
  • Mobile, Web and Desktop support
  • Pluralization and Duals support
  • Supports both languageCode (en) and languageCode_countryCode (en_US) locale formats
  • Automatically save & restore the selected locale with a simple implementation
  • Full support for right-to-left locales
  • Fallback locale support in case the system locale is unsupported
  • Supports both inline or nested JSON

Documentation #

Installation #

For the most up-to-date setup steps, see the Installation Guide.

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

dependencies:
  flutter_easy_translate: <latest version>

Install packages from the command line (or from your editor):

flutter pub get

Configuration #

Import flutter_easy_translate:

import 'package:flutter_easy_translate/flutter_easy_translate.dart';

Place the json localization files in a folder of your choice within the project.

By default flutter_easy_translate will search for localization files in the assets/i18n directory in your project's root.

Declare your assets localization directory in pubspec.yaml

flutter:
  assets:
    - assets/i18n/

In the main function create the localization delegate and start the app, wrapping it with LocalizedApp

void main() async
{
  var delegate = await LocalizationDelegate.create(
        fallbackLocale: 'en_US',
        supportedLocales: ['en_US', 'es', 'fa']);

  runApp(LocalizedApp(delegate, MyApp()));
}

If the assets directory for the localization files is different than the default one (assets/i18n), you need to specify it:

 var delegate = await LocalizationDelegate.create(
      ...
        basePath: 'assets/i18n/'
      ...

Example MyApp:

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    var localizationDelegate = LocalizedApp.of(context).delegate;

    return LocalizationProvider(
      state: LocalizationProvider.of(context).state,
      child: MaterialApp(
        title: 'Flutter Easy Translate',
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          localizationDelegate
        ],
        supportedLocales: localizationDelegate.supportedLocales,
        locale: localizationDelegate.currentLocale,
        theme: ThemeData(primarySwatch: Colors.blue),
        home: MyHomePage(),
        ),
      );
  }
}

Usage #

Translate a string:

translate('your.localization.key');

Translate with arguments;

translate('your.localization.key', args: {'argName1': argValue1, 'argName2': argValue2});

Translate with pluralization:

translatePlural('plural.demo', yourNumericValue);

JSON:

"plural": {
    "demo": {
       "zero": "Please start pushing the 'plus' button.",
	"one": "You have pushed the button one time.",
	"two": "You have pushed the button two times.",
	"other": "You have pushed the button {{value}} times."
    }
}

Change the language:

@override
Widget build(BuildContext context) {
...
  ...
    changeLocale(context, 'en_US');
  ...
...
}

Automatically saving and restoring the selected locale #

Flutter Easy Translate can automatically save the selected locale and restore it after application restart.

This can be done by passing an implementation of ITranslatePreferences during delegate creation:

 var delegate = await LocalizationDelegate.create(
      ...
        preferences: TranslatePreferences()
      ...

Example implementation using the shared_preferences package:

import 'dart:ui';
import 'package:flutter_easy_translate/flutter_easy_translate.dart';
import 'package:shared_preferences/shared_preferences.dart';

class TranslatePreferences implements ITranslatePreferences {
  static const String _selectedLocaleKey = 'selected_locale';

  @override
  Future<Locale?> getPreferredLocale() async {
    final preferences = await SharedPreferences.getInstance();

    if (!preferences.containsKey(_selectedLocaleKey)) return null;

    String? locale = preferences.getString(_selectedLocaleKey);

    return locale == null ? null : localeFromString(locale);
  }

  @override
  Future savePreferredLocale(Locale locale) async {
    final preferences = await SharedPreferences.getInstance();

    await preferences.setString(_selectedLocaleKey, localeToString(locale));
  }
}

And don't forget to reference the shared_preferences package in pubspec.yaml

dependencies:
  shared_preferences: <latest version>

Example #

https://github.com/belabiedredouane/flutter_easy_translate/tree/main/example

Issues #

Please file any issues, bugs or feature request here.

License #

This project is licensed under the MIT License

Maintainers #

1
likes
160
points
360
downloads
screenshot

Publisher

unverified uploader

Weekly Downloads

Flutter Easy Translate is a fully featured localization / internationalization (i18n) library for Flutter.

Homepage
Repository (GitHub)
View/report issues

Topics

#i18n #localization #flutter-translate #easy-localization

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_localizations, intl

More

Packages that depend on flutter_easy_translate