flutter_localization_plus 0.0.1 copy "flutter_localization_plus: ^0.0.1" to clipboard
flutter_localization_plus: ^0.0.1 copied to clipboard

Advanced localization with pluralization, gender-specific text, and RTL support

example/lib/main.dart

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Localization Plus Example',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        useMaterial3: true,
      ),
      home: const LocalizationExample(),
    );
  }
}

class LocalizationExample extends StatefulWidget {
  const LocalizationExample({super.key});

  @override
  State<LocalizationExample> createState() => _LocalizationExampleState();
}

class _LocalizationExampleState extends State<LocalizationExample> {
  late FlutterLocalizationPlus localization;
  int itemCount = 0;
  Gender selectedGender = Gender.neutral;

  @override
  void initState() {
    super.initState();

    // Initialize translations
    final translations = {
      'en': {
        'app_title': 'Localization Example',
        'welcome_message': 'Welcome to the localization example!',
        'item_count_zero': 'No items',
        'item_count_one': '1 item',
        'item_count_other': '{{count}} items',
        'greeting_male': 'Hello sir!',
        'greeting_female': 'Hello madam!',
        'greeting_neutral': 'Hello!',
        'change_language': 'Change Language',
        'add_item': 'Add Item',
        'remove_item': 'Remove Item',
        'select_gender': 'Select Gender',
        'current_locale': 'Current Locale: {{locale}}',
        'is_rtl': 'RTL: {{rtl}}',
      },
      'ar': {
        'app_title': 'مثال الترجمة',
        'welcome_message': 'مرحباً بك في مثال الترجمة!',
        'item_count_zero': 'لا توجد عناصر',
        'item_count_one': 'عنصر واحد',
        'item_count_other': '{{count}} عناصر',
        'greeting_male': 'مرحباً سيدي!',
        'greeting_female': 'مرحباً سيدتي!',
        'greeting_neutral': 'مرحباً!',
        'change_language': 'تغيير اللغة',
        'add_item': 'إضافة عنصر',
        'remove_item': 'إزالة عنصر',
        'select_gender': 'اختر الجنس',
        'current_locale': 'اللغة الحالية: {{locale}}',
        'is_rtl': 'من اليمين إلى اليسار: {{rtl}}',
      },
      'fr': {
        'app_title': 'Exemple de Localisation',
        'welcome_message': 'Bienvenue dans l\'exemple de localisation!',
        'item_count_zero': 'Aucun élément',
        'item_count_one': '1 élément',
        'item_count_other': '{{count}} éléments',
        'greeting_male': 'Bonjour monsieur!',
        'greeting_female': 'Bonjour madame!',
        'greeting_neutral': 'Bonjour!',
        'change_language': 'Changer de Langue',
        'add_item': 'Ajouter un Élément',
        'remove_item': 'Supprimer un Élément',
        'select_gender': 'Sélectionner le Genre',
        'current_locale': 'Locale Actuelle: {{locale}}',
        'is_rtl': 'RTL: {{rtl}}',
      },
    };

    localization = FlutterLocalizationPlus(
      locale: 'en',
      fallbackLocale: 'en',
      translations: translations,
    );
  }

  void _changeLanguage(String languageCode) {
    setState(() {
      localization.setLocale(languageCode);
    });
  }

  void _addItem() {
    setState(() {
      itemCount++;
    });
  }

  void _removeItem() {
    if (itemCount > 0) {
      setState(() {
        itemCount--;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(localization.translate('app_title')),
        actions: [
          PopupMenuButton<String>(
            onSelected: _changeLanguage,
            itemBuilder: (context) => [
              const PopupMenuItem(
                value: 'en',
                child: Text('English'),
              ),
              const PopupMenuItem(
                value: 'ar',
                child: Text('العربية'),
              ),
              const PopupMenuItem(
                value: 'fr',
                child: Text('Français'),
              ),
            ],
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Text(localization.translate('change_language')),
            ),
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              localization.translate('welcome_message'),
              style: Theme.of(context).textTheme.headlineSmall,
            ),
            const SizedBox(height: 24),

            // Current locale info
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      localization.translate('current_locale', args: {
                        'locale': localization.locale,
                      }),
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                    const SizedBox(height: 8),
                    Text(
                      localization.translate('is_rtl', args: {
                        'rtl': localization.isRTL().toString(),
                      }),
                    ),
                  ],
                ),
              ),
            ),

            const SizedBox(height: 24),

            // Item counter with pluralization
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      localization.pluralize(
                        'item_count',
                        count: itemCount,
                        zero: localization.translate('item_count_zero'),
                        one: localization.translate('item_count_one'),
                        other: localization.translate('item_count_other'),
                      ),
                      style: Theme.of(context).textTheme.titleLarge,
                    ),
                    const SizedBox(height: 16),
                    Row(
                      children: [
                        ElevatedButton(
                          onPressed: _addItem,
                          child: Text(localization.translate('add_item')),
                        ),
                        const SizedBox(width: 8),
                        ElevatedButton(
                          onPressed: itemCount > 0 ? _removeItem : null,
                          child: Text(localization.translate('remove_item')),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ),

            const SizedBox(height: 24),

            // Gender selection
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      localization.translate('select_gender'),
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                    const SizedBox(height: 16),
                    Wrap(
                      spacing: 8,
                      children: Gender.values.map((gender) {
                        return ChoiceChip(
                          label: Text(gender.name),
                          selected: selectedGender == gender,
                          onSelected: (selected) {
                            if (selected) {
                              setState(() {
                                selectedGender = gender;
                              });
                            }
                          },
                        );
                      }).toList(),
                    ),
                    const SizedBox(height: 16),
                    Text(
                      localization.genderize(
                        'greeting',
                        gender: selectedGender,
                        male: localization.translate('greeting_male'),
                        female: localization.translate('greeting_female'),
                        neutral: localization.translate('greeting_neutral'),
                      ),
                      style: Theme.of(context).textTheme.titleMedium,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
1
likes
160
points
16
downloads

Documentation

API reference

Publisher

verified publisherbechattaoui.dev

Weekly Downloads

Advanced localization with pluralization, gender-specific text, and RTL support

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, intl

More

Packages that depend on flutter_localization_plus