flutter_translate 1.1.0 copy "flutter_translate: ^1.1.0" to clipboard
flutter_translate: ^1.1.0 copied to clipboard

outdated

The internationalization (i18n) library for Flutter. It lets you define translations for your content in different languages and switch between them easily.

example/lib/main.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:flutter_translate/helpers.dart';

void main() async {
  var delegate = await LocalizationDelegate.create(
          fallbackLanguage: 'en', // required - fallback language in case the device language is not supported
          supportedLanguages: ['en', 'es_ES', 'fa'], // required - the list of supported languages, translation files are required to be present
          basePath: 'assets/i18n'); // optional - default is 'assets/i18n'

  runApp(LocalizedApp(delegate, MyApp()));
}

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    var localizationDelegate = LocalizedApp.of(context).delegate;
    var state = LocalizationProvider.of(context).state;

    return LocalizationProvider(
      state: state,
      child: MaterialApp(
          title: 'Flutter Translate Demo',
          localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            localizationDelegate
          ],
          supportedLocales: localizationDelegate.configuration.supportedLocales,
          locale: state.currentLocale,
          theme: ThemeData(primarySwatch: Colors.blue),
          home: MyHomePage(),
          ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(translate('app.title')),
      ),
      body:  Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            CupertinoButton.filled(
              child: Text(translate('button.change_language')),
              padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 36.0),
              onPressed: () => _onActionSheetPress(context),
              ),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.display1,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                IconButton(
                  icon: Icon(Icons.remove_circle),
                  iconSize: 48,
                  onPressed: () {setState(() {});},
                  ),
                IconButton(
                  icon: Icon(Icons.add_circle),
                  color: Colors.blue,
                  iconSize: 48,
                  onPressed: () {setState(() {});},
                  ),
              ],
            )

          ],
        ),
      ),
     /* floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),*/ // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void showDemoActionSheet({BuildContext context, Widget child}) {

    showCupertinoModalPopup<String>(
      context: context,
      builder: (BuildContext context) => child,
      ).then((String value) {
      if (value != null) {
        setState(() {
          LocalizationProvider.of(context).state.changeLanguage(localeFromString(value));
        });
      }
    });
  }

  void _onActionSheetPress(BuildContext context) {
    showDemoActionSheet(
      context: context,
      child: CupertinoActionSheet(
        title: Text(translate('language.selection.title')),
        message: Text(translate('language.selection.message')),
        actions: <Widget>[
          CupertinoActionSheetAction(
            child: Text(translate('language.name.english')),
            onPressed: () => Navigator.pop(context, 'en'),
            ),
          CupertinoActionSheetAction(
            child: Text(translate('language.name.spanish')),
            onPressed: () => Navigator.pop(context, 'es_ES'),
            ),
          CupertinoActionSheetAction(
            child: Text(translate('language.name.persian')),
            onPressed: () => Navigator.pop(context, 'fa'),
            ),
        ],
        cancelButton: CupertinoActionSheetAction(
          child: Text(translate('button.cancel')),
          isDefaultAction: true,
          onPressed: () => Navigator.pop(context, null),
          ),
        ),
      );
  }
}
256
likes
0
pub points
97%
popularity

Publisher

verified publisherjesway.com

The internationalization (i18n) library for Flutter. It lets you define translations for your content in different languages and switch between them easily.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter

More

Packages that depend on flutter_translate