import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; class AppLocalizations { static final AppLocalizations _singleton = new AppLocalizations._internal(); AppLocalizations._internal(); static AppLocalizations get instance => _singleton; Map _localisedValues; Locale _currentLocale; Future load(Locale locale) async { String jsonContent = await rootBundle .loadString("locale/locale_${locale.languageCode}.json"); _localisedValues = json.decode(jsonContent); _currentLocale = locale; return this; } String getLocale() { return _currentLocale.languageCode; } String text(String key) { return _localisedValues[key] ?? "$key not found"; } } class AppLocalizationsDelegate extends LocalizationsDelegate { const AppLocalizationsDelegate(); @override bool isSupported(Locale locale) => ['en', 'pl', 'de'].contains(locale.languageCode); @override Future load(Locale locale) { return AppLocalizations.instance.load(locale); } @override bool shouldReload(AppLocalizationsDelegate old) => true; }