/* This is the app localizations for multi language This function is used to change the language We used shared preferences to change a whole language of the apps */ import 'dart:async'; import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:shared_preferences/shared_preferences.dart'; class AppLocalizations { final Locale locale; AppLocalizations(this.locale); static AppLocalizations? of(BuildContext context) { return Localizations.of(context, AppLocalizations); } late Map _localizedStrings; Future load() async { // Load the language JSON file from the "lang" folder String jsonString = await rootBundle.loadString('assets/lang/${locale.languageCode}.json'); Map jsonMap = json.decode(jsonString); _localizedStrings = jsonMap.map((key, value) { return MapEntry(key, value.toString()); }); return true; } // This method will be called from every widget which needs a localized text String? translate(String key) { return _localizedStrings[key]; } } class AppLocalizationsDelegate extends LocalizationsDelegate { const AppLocalizationsDelegate(); @override bool isSupported(Locale locale) { // Include all of your supported language codes here return ['fr', 'en', 'id', 'ar', 'zh', 'hi', 'th', 'tk'] .contains(locale.languageCode); } @override Future load(Locale locale) async { final SharedPreferences _pref = await SharedPreferences.getInstance(); String? lCode = _pref.getString('lCode'); String? cCode = _pref.getString('cCode'); if (lCode == null || cCode == null) { await _pref.setString('lCode', locale.languageCode); await _pref.setString('cCode', locale.countryCode!); } else { locale = Locale(lCode, cCode); } AppLocalizations localizations = new AppLocalizations(locale); await localizations.load(); return localizations; } @override bool shouldReload(AppLocalizationsDelegate old) => true; }