flutter_translaty 1.0.12 copy "flutter_translaty: ^1.0.12" to clipboard
flutter_translaty: ^1.0.12 copied to clipboard

Manage your translations easily with translaty.io. Built on top of easy_localization

example/lib/main.dart

import 'dart:developer';

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

import 'generated/locale_keys.g.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();

  runApp(
    EasyLocalization(
      supportedLocales: const [
        Locale('en'),
        Locale('de'),
        Locale('ru'),
      ],
      path: 'assets/translations',
      startLocale: const Locale('en'),
      saveLocale: false,
      child: const MyApp(),
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key}) : super(key: key);

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

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

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

  void _decreaseCounter() {
    setState(() {
      counter--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const _Title("Normal translation"),
            Text(tr(LocaleKeys.single_key)),
            const _Title("Arguments"),
            Text(
              tr(
                LocaleKeys.key_with_args,
                namedArgs: {'countryName': 'France', "vehicle": "Bike"},
              ),
            ),
            Text(
              tr(
                LocaleKeys.key_with_args,
                namedArgs: {'countryName': 'Peru', "vehicle": "Plane"},
              ),
            ),
            const _Title("Plurals"),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                ElevatedButton(
                  onPressed: () => _decreaseCounter(),
                  child: const Text("-"),
                ),
                const SizedBox(width: 32),
                Text(
                  plural(
                    LocaleKeys.plural_key,
                    counter,
                    namedArgs: {
                      "count": counter.toString(),
                    },
                  ),
                ),
                const SizedBox(width: 32),
                ElevatedButton(
                  onPressed: () => _incrementCounter(),
                  child: const Text("+"),
                ),
              ],
            ),
            const _Title("Change language"),
            ChangeLanguageWidget(afterChange: () {
              setState(() {});
            }),
            const _Title("Device locale"),
            Text(context.deviceLocale.toString()),
          ],
        ),
      ),
    );
  }
}

class _Title extends StatelessWidget {
  final String text;
  const _Title(this.text, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(16),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            text,
            style: const TextStyle(
              fontSize: 22,
              fontWeight: FontWeight.bold,
            ),
          ),
          const Divider(
            height: 2,
            thickness: 2,
          ),
        ],
      ),
    );
  }
}

class ChangeLanguageWidget extends StatelessWidget {
  final Function() afterChange;
  const ChangeLanguageWidget({
    Key? key,
    required this.afterChange,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final locales = context.supportedLocales;
    return Wrap(
      spacing: 8,
      children: locales.map(
        (locale) {
          return ElevatedButton(
            child: Text(locale.toLanguageTag()),
            onPressed: () async {
              log(locale.toString(), name: toString());

              await context.setLocale(locale);
              afterChange.call();
            },
          );
        },
      ).toList(),
    );
  }
}
1
likes
50
points
67
downloads

Publisher

unverified uploader

Weekly Downloads

Manage your translations easily with translaty.io. Built on top of easy_localization

Homepage
View/report issues

License

MIT (license)

Dependencies

args, easy_logger, flutter, flutter_localizations, intl, path, pubspec_yaml, shared_preferences, translaty_fetcher, yaml

More

Packages that depend on flutter_translaty