df_localization 0.5.0 copy "df_localization: ^0.5.0" to clipboard
df_localization: ^0.5.0 copied to clipboard

A package that provides an easy way to add localization support to your Flutter app.

example/lib/main.dart

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

// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return AutoTranslationScope(
      controller: AutoTranslationController(
        remoteDatabaseBroker: const FirestoreDatabseBroker(
          projectId: 'YOUR_FIREBASE_PROJECT_ID',
        ),
        translationBroker: const GeminiTranslatorBroker(
          apiKey: 'YOUR_GOOGLE_TRANSLATOR_API_KEY',
        ),
        persistentDatabaseBroker: const PersistentDatabaseBroker(),
      ),
      builder: (context, child) {
        return MaterialApp(
          locale: AutoTranslationScope.controllerOf(context)?.locale,
          home: Scaffold(
            body: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                spacing: 8.0,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  FilledButton(
                    onPressed: () {
                      final locale = primaryLocale(WidgetsBinding.instance);
                      AutoTranslationScope.controllerOf(
                        context,
                      )?.setLocale(locale);
                    },
                    child: const Text('Default'),
                  ),
                  FilledButton(
                    onPressed: () {
                      AutoTranslationScope.controllerOf(
                        context,
                      )?.setLocale(const Locale('zh', 'CN'));
                    },
                    child: const Text('Chinese (CN)'),
                  ),
                  FilledButton(
                    onPressed: () {
                      AutoTranslationScope.controllerOf(
                        context,
                      )?.setLocale(const Locale('es', 'MX'));
                    },
                    child: const Text('Spanish (MX)'),
                  ),
                  FilledButton(
                    onPressed: () {
                      AutoTranslationScope.controllerOf(
                        context,
                      )?.setLocale(const Locale('de', 'DE'));
                    },
                    child: const Text('German (DE)'),
                  ),

                  Text(
                    'Welcome to this app {__DISPLAY_NAME__}||welcome_message'
                        .tr(args: {'__DISPLAY_NAME__': 'Robert'}),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
2
likes
0
points
42
downloads

Publisher

verified publisherdev-cetera.com

Weekly Downloads

A package that provides an easy way to add localization support to your Flutter app.

Homepage
Repository (GitHub)
View/report issues

Topics

#configuration #locale #localization #state-management #translation

Funding

Consider supporting this project:

www.buymeacoffee.com

License

unknown (license)

Dependencies

args, df_config, df_log, df_pod, df_safer_dart, flutter, google_generative_ai, http, path, shared_preferences, yaml

More

Packages that depend on df_localization