Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const _supportedLocales = ['en', 'fr', 'ru'];
class ExampleLocalizations {
final Translations translations;
const ExampleLocalizations(this.translations);
static final _translations = <String, Translations Function()>{
'en': () => const Translations(),
'fr': () => const Translations_fr(),
'ru': () => const Translations_ru(),
};
static const LocalizationsDelegate<ExampleLocalizations> delegate =
_ExampleLocalizationsDelegate();
static final List<Locale> supportedLocales =
_supportedLocales.map((x) => Locale(x)).toList();
static Future<ExampleLocalizations> load(Locale locale) =>
Future.value(ExampleLocalizations(_translations[locale.languageCode]()));
static Translations of(BuildContext context) =>
Localizations.of<ExampleLocalizations>(context, ExampleLocalizations)
.translations;
}
class _ExampleLocalizationsDelegate
extends LocalizationsDelegate<ExampleLocalizations> {
const _ExampleLocalizationsDelegate();
@override
bool isSupported(Locale locale) =>
_supportedLocales.contains(locale.languageCode);
@override
Future<ExampleLocalizations> load(Locale locale) =>
ExampleLocalizations.load(locale);
@override
bool shouldReload(LocalizationsDelegate<ExampleLocalizations> old) => false;
}
@BeliliFahem

This comment has been minimized.

Copy link

@BeliliFahem BeliliFahem commented Sep 7, 2020

I have the following complication error:
error: The return type 'Translations_fr' isn't a 'Translations', as required by the closure's context. (return_of_invalid_type_from_closure at ... )

@foxanna

This comment has been minimized.

Copy link
Owner Author

@foxanna foxanna commented Sep 16, 2020

Do you have any error for Translations_ru class? If not, maybe you have a different .yaml file name for French?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment