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

outdated

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

localize_and_translate #

Flutter localization in easy steps, really simple

Pub Example

Screenshots #

screenshot screenshot

Tutorial #

Video #

Methods #

Method Job
init() initialize things, before runApp()
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/langs/ar.json' | 'assets/langs/en.json'
  • structure should look like
{
    "appTitle": "تطبيق تجريبى", 
    "buttonTitle": "English", 
    "googleTest": "تجربة ترجمة جوجل", 
    "textArea": "هذا مجرد نموذج للتأكد من اداء الأداة"
}
  • define them as assets in pubspec.yaml
flutter:
  assets:
    - assets/langs/en.json
    - assets/langs/ar.json

Migrate from 2.x.x to 3.x.x #

  • Replace
LIST_OF_LANGS = ['ar', 'en'];
LANGS_DIR = 'assets/langs/';
await translator.init();
  • With
await translator.init(
    localeDefault: LocalizationDefaultType.device,
    languagesList: <String>['ar', 'en'],
    assetsDirectory: 'assets/langs/',
    apiKeyGoogle: '<Key>', // NOT YET TESTED
);

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(
    localeDefault: LocalizationDefaultType.device,
    languagesList: <String>['ar', 'en'],
    assetsDirectory: 'assets/langs/',
    apiKeyGoogle: '<Key>', // NOT YET TESTED
  );

  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 googleTranslate("test", from: 'en', to: 'ar')
  • 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(translator.translate('appTitle')),
        // centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: <Widget>[
            SizedBox(height: 50),
            Text(
              translator.translate('textArea'),
              textAlign: TextAlign.center,
              style: TextStyle(fontSize: 35),
            ),
            OutlineButton(
              onPressed: () {
                translator.setNewLanguage(
                  context,
                  newLanguage: translator.currentLanguage == 'ar' ? 'en' : 'ar',
                  remember: true,
                  restart: true,
                );
              },
              child: Text(translator.translate('buttonTitle')),
            ),
            OutlineButton(
              onPressed: () async {
                print(await translator.translateWithGoogle(
                  key: 'رجل',
                  from: 'ar',
                ));
              },
              child: Text(translator.translate('googleTest')),
            ),
          ],
        ),
      ),
    );
  }
}

Show some ❤️ and star the repo #

Fork Star Watch

Project Created & Maintained By #

Mohamed Sayed #

Software Engineer | In ❤️ with Flutter

Note : All Contibutions Are Welcomed #

Contributions #

131
likes
0
pub points
93%
popularity

Publisher

verified publishermsayed.net

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

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

flutter, flutter_localizations, http, shared_preferences

More

Packages that depend on localize_and_translate