locale_switcher 0.4.0 copy "locale_switcher: ^0.4.0" to clipboard
locale_switcher: ^0.4.0 copied to clipboard

A widget for switching the locale of your application with 2 lines of code.

locale_switcher #

A widget for switching the locale of your application.

codecov

Content #

About #

This package allows you to add locale-switching functionality to your app with just a few lines of code.

It depends on intl package (not tested with other localization packages).

Features #

  • contains nice a widget to switch locale,
    • which provide list of locales of you app with additional option to use system locale,
  • Store last selected locale in [SharedPreferences] (optional),
  • Provides a [ValueNotifier] to dynamically change the app's locale.
  • Observes changes in the system locale.

Usage #

  1. Wrap MaterialApp or CupertinoApp with LocaleManager:
  @override
Widget build(BuildContext context, WidgetRef ref) {
  return LocaleManager(
      child: MaterialApp(
        locale: LocaleManager.locale.value,
        localizationsDelegates: AppLocalizations.localizationsDelegates,
        supportedLocales: AppLocalizations.supportedLocales,
  //...
  1. Add LocaleSwitcher widget anywhere into your app.
  2. Or use SelectLocaleButton or just use showSelectLocaleDialog.

Note: localization should be set up before you start to use this package, if there some problems - please, check next section and/or intl documentation, before reporting bug.

Checking that intl package is setup correctly: #

The following instruction is from intl package, so you probably already did them:

In pubspec.yaml:


dependencies: # in this section
  intl: 
  flutter_localizations:
    sdk: flutter
dev_dependencies: # in this section 
  build_runner:  
flutter: # in this section 
  generate: true  

Optionally - in l10n.yaml:

arb-dir: lib/src/l10n
template-arb-file: intl_en.arb
output-dir: lib/src/l10n/generated
output-localization-file: app_localizations.dart
untranslated-messages-file: desiredFileName.txt
preferred-supported-locales: [ "en", "vi", "de" ]
nullable-getter: false

Example #

Online Example here

Example Code

TODO: #

  • ❌ Test with other localization system
  • ❌ Customize switcher
  • ❌ Allow to change language-flag assignment
  • ❌ Finish showOtherLocales button

FAQ #

- How to use localization outside of MaterialApp(or CupertinoApp):

Here is a useful example, although it is not depend on this package:


import 'package:flutter_gen/gen_l10n/app_localizations.dart';

/// Access localization through locale
extension LocaleWithDelegate on Locale {
  /// Get class with translation strings for this locale.
  AppLocalizations get tr => lookupAppLocalizations(this);
}

Locale("en").tr.example
// or 
LocaleManager.locale.value.tr.example
3
likes
0
pub points
56%
popularity

Publisher

unverified uploader

A widget for switching the locale of your application with 2 lines of code.

Homepage

License

unknown (license)

Dependencies

animated_toggle_switch, circle_flags, cupertino_icons, flutter, flutter_localizations, intl, shared_preferences

More

Packages that depend on locale_switcher