flutter_locales2 1.0.3 copy "flutter_locales2: ^1.0.3" to clipboard
flutter_locales2: ^1.0.3 copied to clipboard

discontinuedreplaced by: app_local

Easily localize your app to multiple languages by using json file in assets folder and you can change the language without close the app

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_locales2/flutter_locales2.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Locales.init(['en', 'ar', 'fa']); // get last saved language
  // remove await async (to get system language as default)

  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return LocaleBuilder(
      builder: (locale) => MaterialApp(
        title: 'Flutter Locales',
        localizationsDelegates: Locales.delegates,
        supportedLocales: Locales.supportedLocales,
        locale: locale,
        theme: ThemeData(primarySwatch: Colors.blue),
        home: HomeScreen(),
      ),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: LocaleText('localization')),
      body: Center(
        child: LocaleText('welcome', style: TextStyle(fontSize: 32)),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Navigator.push(
              context, MaterialPageRoute(builder: (_) => SettingScreen()));
        },
        child: Icon(Icons.settings),
      ),
    );
  }
}

class SettingScreen extends StatelessWidget {
  const SettingScreen({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(Locales.string(context, 'setting'))),
      // with extension
      // appBar: AppBar(title: Text(context.localeString('setting'))),
      body: Column(
        children: [
          ListTile(
            onTap: () => Locales.change(context, 'en'),
            title: LocaleText('english'),
          ),
          ListTile(
            onTap: () => Locales.change(context, 'ar'),
            title: LocaleText('Arabic'),
          ),
          // to change language with Extension
          ListTile(
            onTap: () => context.changeLocale('fa'),
            title: LocaleText('farsi'),
          ),
          Text('Current Locale: ' +
              Locales.currentLocale(context)!.languageCode),
          // Text('Current Locale: ' + context.currentLocale.languageCode), // with Extension
        ],
      ),
    );
  }
}
1
likes
40
points
12
downloads

Publisher

verified publisherabom.me

Weekly Downloads

Easily localize your app to multiple languages by using json file in assets folder and you can change the language without close the app

License

MIT (license)

Dependencies

flutter, flutter_localizations, intl, shared_preferences

More

Packages that depend on flutter_locales2