Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

localize_and_translate

flutter localization in human way, help with localizing your app.

tutorial

video

methods

method job
init() initialize things, before runapp()
translate('word') word translation
translate('word',{"key":"value"}) word translation with replacement arguments
<!– googletranslate('word', from: 'en', to: 'ar')
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" : "تطبيق",
  "textarea" : "thisi is just a test text"
}
  • define them as assets in pubspec.yaml
flutter:
  assets:
    - assets/langs/en.json
    - assets/langs/ar.json

initialization

  • add imports to main.dart
  • make main() async and do the following
  • ensure flutter activated widgetsflutterbinding.ensureinitialized()
  • define languages list list_of_langs
  • define assets directory langs_dir
  • initialize await translator.init();
  • 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();

  list_of_langs = ['ar', 'en']; // define languages
  langs_dir = 'assets/langs/'; // define directory
  await translator.init(); // intialize

  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 statefulwidget {
  @override
  _homestate createstate() => _homestate();
}

class _homestate extends state<home> {
  string testtext =
      translator.currentlanguage == 'ar' ? 'جار الترجمة' : 'translating..';

  @override
  void initstate() {
    super.initstate();
    future.delayed(duration.zero, () async {
      testtext = await translator.googletranslate(
        testtext,
        from: 'en',
        to: translator.currentlanguage,
      );
      setstate(() {});
    });
  }

  @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),
            ),
            text(
              testtext,
              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')),
            ),
          ],
        ),
      ),
    );
  }
}


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.