Search code examples
flutterdartlocalization

Limit the number of selectable locales


Here is a line of code which returns the locale:

// lib/data/gallery_options.dart

Locale get locale => _locale ?? deviceLocale;

... which can be accessed like:

// lib\studies\shrine\app.dart

locale: GalleryOptions.of(context).locale,

The problem is the options for locales, i.e. supportedLocales come from this file:

.dart_tool/flutter_gen/gen_l10n/gallery_localizations.dart

Screenshot

... which looks to be not part of the repository and automatically generated. It is not editable and gets reloaded discarding all changes.

How can I limit the number of locales selectable by the user?


Solution

  • Solved! 😃 Supported locales of settings page are on file lib/pages/settings.dart and can be limited by modifying this line with this patch:

    -    var supportedLocales =
    -        List<Locale>.from(GalleryLocalizations.supportedLocales);
    +    var supportedLocales = [
    +      const Locale('en'),
    +      const Locale('he'),
    +    ];
         supportedLocales.removeWhere((locale) => locale == deviceLocale);