multilingual 1.0.0 copy "multilingual: ^1.0.0" to clipboard
multilingual: ^1.0.0 copied to clipboard

Plug-in for multilingual settings in apps.

Multilingual #

Multilingual is a powerful Flutter localization plugin that simplifies managing multiple languages in your app. It allows dynamic text translation, runtime language switching, and text replacement.

Features #

  • Dynamic Translation: Use .trans() to easily translate text based on the current locale.
  • Custom Text Replacement: Replace specific text dynamically using .trans({'key': 'value'});.
  • Key Existence Check: Verify if a specific key exists with context.isTrans('key');.
  • Support for Multiple Languages: Seamlessly manage translations for multiple locales.

Usage #

Basic Setup #

Wrap your app with the Multilingual widget and define the supported languages:

import 'package:flutter/material.dart';
import 'package:multilingual/extensions.dart';
import 'package:multilingual/multilingual.dart';
import 'package:multilingual/multilingual_controller.dart';

void main() {
  runApp(Multilingual(
      languages: {
        Locale('ko', 'KR'): {'lan': '한국어', 'text_button': '언어변경'},
        Locale('en', 'US'): {'lan': 'English', 'text_button': 'Language change'},
        Locale('ja', 'JP'): {'lan': '日本語', 'text_button': '言語変更'},
      },
      builder: (context, controller) {
        return MyApp(controller);
      }
  ));
}

Creating Your App #

Pass the MultilingualController to your app and use .trans() for translations:

class MyApp extends StatefulWidget {
  const MyApp(this.controller, {super.key});

  final MultilingualController controller;

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      localizationsDelegates: widget.controller.localizationsDelegates,
      supportedLocales: widget.controller.supportedLocales,
      locale: widget.controller.locale,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('lan'.trans()),
              TextButton(
                onPressed: () {
                  Locale? locale = widget.controller.locale;
                  Locale loc;
                  if (locale.toString() == 'ko_KR') {
                    loc = const Locale('en', 'US');
                  } else if (locale.toString() == 'en_US') {
                    loc = const Locale('ja', 'JP');
                  } else {
                    loc = const Locale('ko', 'KR');
                  }
                  widget.controller.setLocale(loc);
                },
                child: Text('text_button'.trans()),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Checking Key Existence #

Use context.isTrans('key') to check if a translation key exists:

if (context.isTrans('key')) {
  print('Translation key exists!');
} else {
  print('Translation key does not exist.');
}

Custom Text Replacement #

Replace specific text dynamically:

Text('Original text'.trans({'Original text': 'Replaced text'}));

Example Output #

  • Default Language: Korean (ko_KR)
    • Button Text: 언어변경
  • Switch to English (en_US)
    • Button Text: Language change
  • Switch to Japanese (ja_JP)
    • Button Text: 言語変更

Contributing #

Feel free to contribute to Multilingual by submitting issues or pull requests on the repository.

License #

This project is licensed under the MIT License.

3
likes
0
points
54
downloads

Publisher

unverified uploader

Weekly Downloads

Plug-in for multilingual settings in apps.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_localizations, flutter_web_plugins, plugin_platform_interface, web

More

Packages that depend on multilingual