localize_and_translate 2.2.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 91

localize_and_translate #

Flutter localization abstract, Really simple

Pub Example

Show some ❤️ and star the repo #

Fork Star Watch

Screenshots #

screenshot screenshot

Tutorial #

Video #

Alt text

Methods #

MethodJob
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
currentLanguageActive language code
localeActive Locale
locals()Locales list
delegatesLocalization 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')),
            ),
          ],
        ),
      ),
    );
  }
}

Contributions #

Known Issues #

  • Lowest Flutter Version (1.12.13)

Project Created & Maintained By #

Mohamed Sayed #

Software Engineer | In ❤️ with Flutter

Donate #

If you found this project helpful or you learned something from the source code and want to thank me, consider buying me a cup of ☕

Note : All Contibutions Are Welcomed #

Next #

  • Improvements

2.2.2 #

  • Docs : Fix

2.2.1 #

  • googleTranslate() : Remove

2.1.2 #

  • Plugin -> Package

2.0.0 : 2.1.1 #

  • translate() enhance
  • translate() with keys
  • googleTranslate() added
  • in error, return given key
  • in not found case return given key
  • FIXs, docs, video docs

1.2.0 | 1.2.1 #

  • Performance Improvements
  • Docs Improvements

1.1.1+6 #

  • add isDirectionRTL method

1.0.1+5 #

  • Documentation Enhancements
  • iOS Delegates

1.0.1 #

  • first lanuch
  • added main methods

0.0.1 #

  • init
  • add description

example/lib/main.dart

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()));
}

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,
      locale: translator.locale,
      supportedLocales: translator.locals(),
    );
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  // String testText = 'google translate test';

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

Use this package as a library

1. Depend on it

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


dependencies:
  localize_and_translate: ^2.2.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:localize_and_translate/localize_and_translate.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
82
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:localize_and_translate/localize_and_translate.dart, package:shared_preferences/shared_preferences.dart] that declares support for platforms: android, ios, macos, web

Package does not support Flutter platform windows

Because of import path [package:localize_and_translate/localize_and_translate.dart, package:shared_preferences/shared_preferences.dart] that declares support for platforms: android, ios, macos, web

Package not compatible with SDK dart

because of import path [localize_and_translate] that is in a package requiring null.

Health suggestions

Fix lib/localize_and_translate.dart. (-1.49 points)

Analysis of lib/localize_and_translate.dart reported 3 hints:

line 8 col 8: Unused import: 'package:translator/translator.dart'.

line 10 col 14: Name non-constant identifiers using lowerCamelCase.

line 11 col 8: Name non-constant identifiers using lowerCamelCase.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
intl ^0.16.0 0.16.1
shared_preferences ^0.5.6+3 0.5.7+3
translator ^0.1.4 0.1.4
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
flutter_web_plugins 0.0.0
http 0.12.1
http_parser 3.1.4
meta 1.1.8
path 1.6.4 1.7.0
pedantic 1.9.0 1.9.1
shared_preferences_macos 0.0.1+10
shared_preferences_platform_interface 1.0.4
shared_preferences_web 0.1.2+7
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8