Skip to content

DevicePreview locale not working well with easy_localization #257

Description

@Dan1ell

I'm using EasyLocalization (and GoRouter, in case it is relevant).

EasyLocalization requires: locale: context.locale,
DevicePreview requires: locale: DevicePreview.locale(context),

When setting the line to what DevicePreview needs, locale switching becomes unreliable. In particular:

  • from my app ui: doesn't work at all
  • from device preview console: only direction switching works, so if going from english to arabic, the text will go RTL, but nothing else will change.

Any lines of code in the app that reference context.locale become non-functional. e.g.:

 value: context.locale,
   onChanged: (value) {
      context.setLocale(value!);
   },

final languageCode = context.locale.languageCode;

etc.

My main.dart

  runApp(
    DevicePreview(
      enabled: !kReleaseMode,
      tools: const [
        ...DevicePreview.defaultTools,        
      ],
      builder: (context) => EasyLocalization(
          supportedLocales: locales,
          path: 'assets/translations',
          fallbackLocale: const Locale('en'),
          child: MultiProvider(
            providers: [
              ChangeNotifierProvider(create: (context) => ThemeChangeNotifier()),
            ],
            child: App(),
          )),
    ),
  );

My app.dart

  return MaterialApp.router(
      routerDelegate: router.routerDelegate,
      routeInformationParser: router.routeInformationParser,
      routeInformationProvider: router.routeInformationProvider,
      debugShowCheckedModeBanner: false,
      restorationScopeId: 'app',
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale, // works in my app, but not in device preview
      locale: DevicePreview.locale(context), // does not work in my app, works poorly in device preview
      theme: lightThemeData,
      darkTheme: darkThemeData,
      themeMode: context.watch<ThemeChangeNotifier>().themeMode,
      builder: DevicePreview.appBuilder,
    );

My flutter doctor summary

[✓] Flutter (Channel stable, 3.27.4, on macOS 15.2 24C101 darwin-arm64,
    locale en-US)
[✓] Android toolchain - develop for Android devices (Android SDK version
    34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2024.2)
[✓] VS Code (version 1.97.2)
[✓] Connected device (3 available)
[✓] Network resources

What I would like is to set 'locale: DevicePreview.locale(context)' once, and have locale switching continue to work normally from both my app ui and from the device preview console.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions