marcglasberg / i18n_extension

Flutter package: Easy and powerful internationalization using Dart extensions.
Other
294 stars 64 forks source link

There are no translations in 'en_us' #136

Closed adriancsbna closed 7 months ago

adriancsbna commented 7 months ago

Hello.

I am using i18n_extensions for a new App and I have run into a problem. I'm setting the default language to Spanish, but for some reason, the App tries to translate to en_us and I get the following error: There are no translations in 'en_us' for "continue".

I don't have that language set anywhere, only Spanish. Here is the code of the MaterialApp and the i18n.

import 'package:i18n_extension/i18n_extension.dart';

extension Localization on String {
  static const _t = ConstTranslations(
    'es',
    {
      'continue': {
        'es': 'Continuar',
      },
      'onBoarding1Title': {
        'es': 'Onboarding 1',
      },
      'onBoarding1Description': {
        'es': 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec auctor, nunc nec lacinia.',
      }
    },
  );

  String get i18n => localize(this, _t);

  String fill(List<Object> params) => localizeFill(this, params);
}
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        useMaterial3: true,

      ),
      initialRoute: "/",
      routes: {
        '/': (context) => const SplashScreen(),
        '/on_boarding': (context) => const OnBoardingScreen(),
      },
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [Locale('es')],
      locale: const Locale('es'),
    );
  }

I am using Flutter 3.19.1 and i18n_extension: ^11.0.11

marcglasberg commented 7 months ago

@adriancsbna You have to wrap your widgets with the I18n() widget. Did you do that?

From the documentation:

Setup

Wrap your widget tree with the I18n widget, below the MaterialApp, together with the localizationsDelegates and the supportedLocales:

import 'package:i18n_extension/i18n_widget.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

...

@override
Widget build(BuildContext context) {
  return MaterialApp(
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', 'US'),
        const Locale('pt', 'BR'),
      ],
      home: I18n(child: ...) // Here !!!!!!!!!!!!!!!!!!!!!!!
  );
}

The code home: I18n(child: ...) shown above will translate your strings to the current system locale. Or you can override it with your own locale, like this:

I18n(
  initialLocale: Locale('pt', 'BR'),
  child: ...
adriancsbna commented 7 months ago

Wow, right. When using routes and not a home I totally forgot about it.

I write below the code I used, in my case it is to wrap the widgets in the routes:

  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        useMaterial3: true,

      ),
      initialRoute: "/",
      routes: {
        '/': (context) => const SplashScreen(),
        '/on_boarding': (context) => I18n(child: const OnBoardingScreen()), // Here!!
      },
      localizationsDelegates: const [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [Locale('es')],
      locale: const Locale('es'),
    );
  }

Thank you very much @marcglasberg