internationalization 4.1.0 copy "internationalization: ^4.1.0" to clipboard
internationalization: ^4.1.0 copied to clipboard

A project to easily implement internationalization on flutter projects

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:internationalization/internationalization.dart';

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

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      supportedLocales: const [
        Locale('pt'),
        Locale('en'),
      ],
      localeResolutionCallback: _localeResolutionCallback,
      localizationsDelegates: [
        _internationalizationDelegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }

  InternationalizationDelegate get _internationalizationDelegate {
    return InternationalizationDelegate(
      translationsPath: 'assets/translations/',
      suportedLocales: [
        const Locale('pt'),
        const Locale('en'),
      ],
    );
  }

  Locale _localeResolutionCallback(Locale? locale, Iterable<Locale> supportedLocales) {
    return supportedLocales.firstWhere(
      (supportedLocale) {
        return supportedLocale.languageCode == locale?.languageCode;
      },
      orElse: () => const Locale('pt'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

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

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const TextIntl(
              'd',
              parent: ['a', 'b', 'c'],
            ),
            const TextIntl('lblPushedTimes'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
copied to clipboard
20
likes
150
points
222
downloads

Publisher

unverified uploader

Weekly Downloads

2024.09.15 - 2025.03.30

A project to easily implement internationalization on flutter projects

Repository (GitHub)

Documentation

API reference

License

BSD-2-Clause (license)

Dependencies

flutter, flutter_localizations, intl

More

Packages that depend on internationalization