localize_and_translate 1.0.1+5

  • Readme
  • Changelog
  • Example
  • Installing
  • new75

localize_and_translate #

localization and translation was never easier, simple way to translate your flutter apps and make it international

Screenshots #

screenshot screenshot

Methods #

MethodJob
currentLanguageactive language code
localeactive Locale
init()initialize things, before runApp()
translate('word')word translation
setNewLanguage(context,'en',restart: true, remember: true,)change language
locals()locales list

How To Use #

  1. add localize_and_translate: any as dependencies in pubspec.yaml
  2. run flutter pub get into app folder
  3. add .json translation files as assets
  • For example : 'assets/langs/ar.json' | 'assets/langs/en.json'
  • structure should look like
{
  "appTitle" : "Example",
  "textArea" : "Thisi is just a test text",
}
{
  "appTitle" : "تجربة",
  "textArea" : "هذا مجرد نموذج للتأكد من اداء الاداة",
}
  • define them as assets into pubspec.yaml
flutter:
  assets:

    - assets/langs/en.json
    - assets/langs/ar.json

  • run flutter pub get
  1. add imports to main.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:localize_and_translate/localize_and_translate.dart';
import 'package:flutter_localizations/flutter_localizations.dart';`
  1. wrap app entry into LocalizedApp() ** make sure you define it's child into different place "NOT INSIDE" **
  runApp(
    LocalizedApp(
      child: MyApp(),
    ),
  );
  1. convert your main() method to async, we will need next
Future<void> main() async {
  runApp(
    LocalizedApp(
      child: MyApp(),
    ),
  );
}
  1. add WidgetsFlutterBinding.ensureInitialized(); at very first of main()
  2. inside main() define: Languages + Root dir, then call await translator.init();
  • so now your main() should look like this
Future<void> 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(),
    ),
  );
}
  1. define your LocalizedApp() child as MaterialApp() like this
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(), // re Route
      localizationsDelegates: [ // Android + iOS Delegates 
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      locale: translator.locale, // active locale
      supportedLocales: translator.locals(), // locals list
    );
  }
}
  1. Enjoy
  • we use translate(word)
  • setNewLanguage(languageCode) : and it's parameters
class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @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,
          children: <Widget>[
            SizedBox(height: 50),
            Text(
              translator.translate('textArea'), // Translation
              style: TextStyle(fontSize: 35),
            ),
            SizedBox(height: 150),
            OutlineButton(
              onPressed: () { // setNewLanguage 'ar'
                translator.setNewLanguage(
                  context,
                  newLanguage: 'ar',
                  remember: true,
                  restart: true,
                );
              },
              child: Text('العربية'),
            ),
            OutlineButton(
              onPressed: () { // setNewLanguage 'en'
                translator.setNewLanguage(
                  context,
                  newLanguage: 'en',
                  remember: true,
                  restart: true,
                );
              },
              child: Text('English'),
            ),
          ],
        ),
      ),
    );
  }
}

Complete Example #

Author #

Mohamed Sayed

  • Fork   Star   Watches
  • Plugin   Example

My Plugins #

Next #

  • Improvements

1.0.1+5 #

  • Documentation Enhancements
  • iOS Delegates

1.0.1 #

  • first lanuch
  • added main methods

0.0.1 #

  • init
  • add description

example/README.md

localize_and_translate_example #

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:localize_and_translate/localize_and_translate.dart';

Future<void> 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: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
      ],
      locale: translator.locale,
      supportedLocales: translator.locals(),
    );
  }
}

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

class _HomeState extends State<Home> {
  @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,
          children: <Widget>[
            SizedBox(height: 50),
            Text(
              translator.translate('textArea'),
              style: TextStyle(fontSize: 35),
            ),
            SizedBox(height: 150),
            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: ^1.0.1+5

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]
51
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

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

  • Dart: 2.7.0
  • pana: 0.13.4
  • Flutter: 1.12.13+hotfix.5

Health suggestions

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

Analysis of lib/localize_and_translate.dart reported 4 hints:

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

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

line 88 col 46: 'ancestorStateOfType' is deprecated and shouldn't be used. Use findAncestorStateOfType instead. This feature was deprecated after v1.12.1..

line 89 col 13: 'TypeMatcher' is deprecated and shouldn't be used. TypeMatcher has been deprecated because it is no longer used in framework(only in deprecated methods). This feature was deprecated after v1.12.1..

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
flutter_localizations 0.0.0
shared_preferences ^0.5.6 0.5.6
Transitive dependencies
collection 1.14.11 1.14.12
flutter_web_plugins 0.0.0
intl 0.16.0 0.16.1
meta 1.1.8
path 1.6.4
shared_preferences_macos 0.0.1+3
shared_preferences_platform_interface 1.0.1
shared_preferences_web 0.1.2+2
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8