Enable translation without context #191
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, I just completed the PR that enables translation with FlutterI18n instance, and without context. Basically I made BuildContext nullable in 4 functions that required it: translate(..), plural(..), currentLocale(..) and refresh(..). I added an optional named parameter FlutterI18n? instance to all of these, and have added it to I18nText and I18nPlural widgets as well. I decided to go with this kind of implementation to avoid adding additional 4 functions to the FlutterI18n class. IMHO it would make it too bloated. I am not completely satisfied with this solution either, since you need to pass null for BuildContext each time you translate without it, but I didn't have any better ideas how to implement it.
I have added tests as well.
Now you can register a FlutterI18n instance, and call for translator, for instance, somewhere in your data layer, like this:
Whenever you pass an instance that is not null it will avoid trying to use the context. When instance is not passed context is used. Developers must take care not to set both BuildContext and instance to null.