Correct implementations of Localization within the MVVM architecture pattern

419

This question was answered for me on Reddit, I just wanted to add to it here in case anyone had a similar issue.

The basic premise is to use keys when using methods without context (such as in view models). Then use a translate function in the view when the context is accessible.

Share:
419
Jack Kirwan
Author by

Jack Kirwan

Updated on December 19, 2022

Comments

  • Jack Kirwan
    Jack Kirwan over 1 year

    I am new to flutter and am currently developing an app using the MVVM architecture pattern described in many of FilledStacks tutorials but am having issues with deciding what is the best way to manage context in the View Models.

    I followed the Internationalization tutorial in the flutter docs for implementation of i18n and l10n which results in the need for BuildContext whenever a localized string is needed.

    I am currently passing context from the Views build method as an arg to methods in which localized strings are used such as methods which return error text or for alert dialogues but this seems incorrect.

    Is there a cleaner way to return Strings from the View Model without passing BuildContext as an argument while maintaining the "hot reload" of language if the user changes the device's language?