flutter_localizations_plus

pub package pub points GitHub Issues GitHub Forks GitHub Stars GitHub License

An enhanced Flutter localization one-stop solution that streamlines multilingual integration for seamless app development.

Platform Support

Android iOS MacOS Web Linux Windows

Requirements

  • Flutter >=3.0.0 <4.0.0
  • Dart: ^2.17.0
  • sprintf: ^7.0.0

Getting started

published on pub.dev, run this Flutter command

flutter pub add flutter_localizations_plus

Steps for Usage in Dart

  • IMPORTANT: locales directory which contains locale JSON files MUST declared in pubspec.yaml.
    flutter:
       assets:
          - locales/     # for multiple languages
  • locale JSON file named after ${languageCode}_${localeCode}.json eg. en_US.json.
{
  "@@locale": "en_US",
  "welcome_tips": "You don’t know about me, without you have read a book by the name of The Adventures of Tom Sawyer; but that ain’t no matter. That book was made by Mr. Mark Twain, and he told the truth, mainly. There was things which he stretched, but mainly he told the truth.",
  "local_time_caption": "Current local time: %s",
  "flight_broadcast_test": "Hello %s from %s."
}
  • Initializes Translations with locales need to support and other optional parameters.
  • IMPORTANT: Ensure locales supported JSON files are present in the locales/ directory.
  // [{locale, name, abbr, region, i10n, fallback}] e.g. [{locale: "en_US", name: "English (United States)", abbr: "en", region: "US"}]
    List<Map<String, dynamic>> formatted = Translations.support([
      Localization.en_US, 
      Localization.zh_Hans, 
      Localization.fr_CA, 
      Localization.pt_BR
    ], selected: Platform.localeName, fallback: Localization.en_US);
  • Add delegates (LocalizationsPlusDelegate and FallbackCupertinoLocalizationsDelegate) to localizationsDelegates and assign supportedLocales with Translations.supportedLocales for WidgetsApp Created.
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          supportedLocales: Translations.supportedLocales,
          localizationsDelegates: const [
            LocalizationsPlusDelegate(),
            FallbackCupertinoLocalizationsDelegate()
            // ... more localization delegates
          ],
          home: const Home(),
        );
      }
    }
  • Retrieve locale JSON strings by key with support sprintf-style arguments
    // 1. Use sprintf-style ordered arguments for dynamic formatting.
    Translations.of(context).text("local_time_caption", DateTime.now());
    Translations.of(context).text("flight_broadcast_test", ["flutter_localizations_plus", "pub.dev"]);
    
    // 2. Fetches raw string in locale file
    Translations.of(context).text("welcome_tips");
    
  • Manually updates app's locale (e.g., from UI language settings page selections).
    // [{locale, name, abbr, region, i10n, fallback}] e.g. [{locale: "en_US", name: "English (United States)", abbr: "en", region: "US"}]
    List<LocaleConfig> allSupported = Translations.allSupported;
    int idxSelected = 1;
    String locale = allSupported[idxSelected].locale; // Localization.en_US;
    await Translations.changeLanguage(locale);

Additional information

Feel free to file an issue if you have any problem.