Problem localizing custom error messages at app level
Environment
Package version: ^17.0.1
Describe your question
It works correctly when working with hardcoded strings as specified in the example.
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return ReactiveFormConfig(
validationMessages: {
ValidationMessage.required: (error) => 'Field must not be empty',
ValidationMessage.email: (error) => 'Must enter a valid email',
},
child: MaterialApp(
home: Scaffold(
body: const Center(
child: Text('Hello Flutter Reactive Forms!'),
),
),
),
);
}
}
However, when I try to localize with the Intl package, I get an error because the related context has not been created yet.
ValidationMessage.required: (error) => AppLocalizations.of(context)!.validation_required,
ValidationMessage.email: (error) => AppLocalizations.of(context)!.validation_email,
it starts to give null because the context material has not yet been created by the app and your app will not load the translations. If you try to use ReactiveFormConfig under the material app, this time the validationMessages property will not be loaded properly and no translations will occur.
Am I doing something wrong or is this how it's supposed to work?
Hi @muratgorken,
You are right, there is nothing wrong in your code. As you mentioned it will fail if the official internationalization approach is used, since the localizations are only loaded in the context of the MaterialApp widget. Thanks to your issue I'm actually considering deprecate this "Global Config". If you find a workaround, feel free to add a PR or let me know.
Thanks