import 'dart:convert'; import 'package:test_sa/models/subtitle.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; class AppLocalization { AppLocalization(this.locale); final Locale locale; static AppLocalization of(BuildContext context) { return Localizations.of(context, AppLocalization); } Subtitle _subtitle; Subtitle get subtitle => _subtitle; Future load() async { String jsonStringValues = await rootBundle.loadString('assets/subtitles/${locale.languageCode}_subtitle.json'); _subtitle = Subtitle.fromJson(json.decode(jsonStringValues)); } // 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','ar'].contains(locale.languageCode); } @override Future load(Locale locale) async { AppLocalization localization = new AppLocalization(locale); await localization.load(); return localization; } @override bool shouldReload(LocalizationsDelegate old) => false; }