flutter_i18n 0.10.0 copy "flutter_i18n: ^0.10.0" to clipboard
flutter_i18n: ^0.10.0 copied to clipboard

outdated

i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:flutter_i18n/flutter_i18n_delegate.dart';
import 'package:flutter_i18n/widgets/I18nPlural.dart';
import 'package:flutter_i18n/widgets/I18nText.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

Future main() async {
  final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
      useCountryCode: false,
      fallbackFile: 'en',
      path: 'assets/i18n',
      forcedLocale: Locale('es'));
  WidgetsFlutterBinding.ensureInitialized();
  await flutterI18nDelegate.load(null);
  runApp(MyApp(flutterI18nDelegate));
}

class MyApp extends StatelessWidget {
  final FlutterI18nDelegate flutterI18nDelegate;

  MyApp(this.flutterI18nDelegate);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
      localizationsDelegates: [
        flutterI18nDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate
      ],
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  MyHomeState createState() => MyHomeState();
}

class MyHomeState extends State<MyHomePage> {
  Locale currentLang;
  int clicked = 0;

  @override
  void initState() {
    super.initState();
    Future.delayed(Duration.zero, () async {
      setState(() {
        currentLang = FlutterI18n.currentLocale(context);
      });
    });
  }

  changeLanguage() {
    setState(() {
      currentLang =
          currentLang.languageCode == 'en' ? Locale('it') : Locale('en');
    });
  }

  incrementCounter() {
    setState(() {
      clicked++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(FlutterI18n.translate(context, "title"))),
      body: Builder(builder: (BuildContext context) {
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              I18nText("label.main", Text(""),
                  translationParams: {"user": "Flutter lover"}),
              I18nPlural("clicked.times", clicked, Text("")),
              FlatButton(
                  onPressed: () async {
                    incrementCounter();
                  },
                  child: Text(FlutterI18n.translate(
                      context, "button.label.clickMea",
                      fallbackKey: "button.label.clickMe"))),
              FlatButton(
                  onPressed: () async {
                    changeLanguage();
                    await FlutterI18n.refresh(context, currentLang);
                    Scaffold.of(context).showSnackBar(SnackBar(
                      content: Text(FlutterI18n.translate(
                          context, "button.toastMessage")),
                    ));
                  },
                  child: Text(
                      FlutterI18n.translate(context, "button.label.language")))
            ],
          ),
        );
      }),
    );
  }
}
197
likes
0
pub points
97%
popularity

Publisher

unverified uploader

i18n made easy for Flutter. With flutter_i18n you can make your app international, using just a simple .json file!

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_localizations, yaml

More

Packages that depend on flutter_i18n