import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; class DemoLocalization { DemoLocalization(this.locale); final Locale locale; static DemoLocalization of(BuildContext context) { return Localizations.of(context, DemoLocalization); } Map _localizedValues; Future load() async { String jsonStringValues = await rootBundle.loadString('lib/languages/${locale.languageCode}.json'); Map mappedJson = json.decode(jsonStringValues); _localizedValues = mappedJson.map((key, value) => MapEntry(key, value.toString())); } String translate(String key) { return _localizedValues[key]; } // static member to have simple access to the delegate from Material App static const LocalizationsDelegate delegate = _DemoLocalizationsDelegate(); } class _DemoLocalizationsDelegate extends LocalizationsDelegate { const _DemoLocalizationsDelegate(); @override bool isSupported(Locale locale) { return ['en', 'es', 'fa', 'hi'].contains(locale.languageCode); } @override Future load(Locale locale) async { DemoLocalization localization = new DemoLocalization(locale); await localization.load(); return localization; } @override bool shouldReload(LocalizationsDelegate old) => false; }