locale_switcher 0.8.0 locale_switcher: ^0.8.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.
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 widget to switch locale - LocaleSwitcher:
- which includes a list of locales for you app with additional option to use system locale,
- it offers several constructors: LocaleSwitcher.toggle, LocaleSwitcher.menu or LocaleSwitcher.custom,
- Contains a button SelectLocaleButton to open popup window,
- Optionally stores the last selected locale in [SharedPreferences],
- Provides a LocaleManager.realLocaleNotifier to dynamically change the app's locale (and notifier LocaleManager.locale to listen to locale changes).
- Observes changes in the system locale.
Usage #
- Wrap
MaterialApp
orCupertinoApp
with LocaleManager:
@override
Widget build(BuildContext context, WidgetRef ref) {
return LocaleManager(
child: MaterialApp(
locale: LocaleManager.locale.value,
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
//...
- Add LocaleSwitcher widget anywhere into your app.
- 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.
Troubleshooting #
Checking that intl package is setup correctly: #
The following instructions are 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 #
TODO: #
- ❌ Test with other localization system
FAQ #
- How to change order of languages?
Languages are shown in the same order as they listed in l10n.yaml.
- How to change flag of language?
Use LocaleManager.reassign
parameter like this:
LocaleManager(
reassign: {'en': ['GB', 'English', <Your_icon_optional>]}
// (first two options are required, third is optional)
// first option is the code of country which flag you want to use
...
)
- 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