localize_and_translate 4.1.1 copy "localize_and_translate: ^4.1.1" to clipboard
localize_and_translate: ^4.1.1 copied to clipboard

Flutter localization in easy steps, simple ways to localize and translate your app

localize_and_translate #

Flutter localization in easy steps

Pub Example

Screenshots #

screenshot screenshot

Tutorial #

Video #

Methods #

Method Job
init() initialize things, before runApp()
"word".tr() word translation - string extension
translate('word') word translation
translate('word',{"key":"value"}) word translation with replacement arguments
setNewLanguage(context,newLanguage:'en',restart: true, remember: true,) change language
isDirectionRTL() is Direction RTL check
currentLanguage Active language code
locale Active Locale
locals() Locales list
delegates Localization Delegates

Installation #

  • add .json translation files as assets
  • For example : 'assets/lang/ar.json' | 'assets/lang/en.json'
  • structure should look like
{
    "appTitle": "تطبيق تجريبى", 
    "buttonTitle": "English", 
    "textArea": "هذا مجرد نموذج للتأكد من اداء الأداة"
}
  • define them as assets in pubspec.yaml
flutter:
  assets:
    - assets/lang/en.json
    - assets/lang/ar.json

Initialization #

  • Add imports to main.dart
  • Make main() async and do the following
  • Ensure flutter activated WidgetsFlutterBinding.ensureInitialized()
  • Initialize await translator.init(); with neccassry parameters
  • Inside runApp() wrap entry class with LocalizedApp()
  • Note : make sure you define it's child into different place "NOT INSIDE"
import 'package:flutter/material.dart';
import 'package:localize_and_translate/localize_and_translate.dart';

main() async {
  // if your flutter > 1.7.8 :  ensure flutter activated
  WidgetsFlutterBinding.ensureInitialized();

  await translator.init(
    localeType: LocalizationDefaultType.device,
    languagesList: <String>['ar', 'en'],
    assetsDirectory: 'assets/lang/',
  );

  runApp(
    LocalizedApp(
      child: MyApp(),
    ),
  );
}
  • LocalizedApp() child example -> MaterialApp()
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
      localizationsDelegates: translator.delegates, // Android + iOS Delegates
      locale: translator.locale, // Active locale
      supportedLocales: translator.locals(), // Locals list
    );
  }
}

Usage #

  • use translate("appTitle")
  • use setNewLanguage(context, newLanguage: 'ar', remember: true, restart: true);
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(),
      appBar: AppBar(
        title: Text('appTitle'.tr()),
        // centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            SizedBox(height: 50),
            Text(
              'textArea'.tr(),
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 35),
            ),
            OutlineButton(
              onPressed: () {
                translator.setNewLanguage(
                  context,
                  newLanguage: translator.currentLanguage == 'ar' ? 'en' : 'ar',
                  remember: true,
                  restart: true,
                );
              },
              child: Text('buttonTitle'.tr()),
            ),
          ],
        ),
      ),
    );
  }
}

❤️ this #

Fork Star Watch

Project Created & Maintained By #

Mohamed Sayed #

Software Engineer | In ❤️ with Flutter

Note : All Contibutions Are Welcomed #

Contributions #

134
likes
130
points
810
downloads

Publisher

verified publishermsayed.net

Weekly Downloads

Flutter localization in easy steps, simple ways to localize and translate your app

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, flutter_localizations, http, shared_preferences

More

Packages that depend on localize_and_translate