I used many translation languages, and all of them have problems downloading the translation in the first seconds, as it appears in the form of Arabic language boxes. What is the solution? So I decided to try this example and do not mention the problem in it, but I wanted to upgrade it to 2.15 and faced a problem attached to the picture Is there a solution
main.dart can can add json like assets/languages/languages-${locale.languageCode}.json
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
class AppLocalization {
AppLocalization(this.locale);
final Locale locale;
static AppLocalization? of(BuildContext context) {
return Localizations.of<AppLocalization>(context, AppLocalization);
}
Map<String, String>? _sentences;
Future<bool> load() async {
String jsonString = await rootBundle.loadString("assets/languages/languages-${locale.languageCode}.json");
Map<String, dynamic> _result = json.decode(jsonString);
this._sentences = new Map();
_result.forEach((String key, dynamic value) {
this._sentences![key] = value.toString();
});
return true;
}
String? trans(String key) {
return this._sentences![key];
}
}
class AppLocalizationDelegate extends LocalizationsDelegate<AppLocalization> {
const AppLocalizationDelegate();
@override
bool isSupported(Locale locale) => ['ar', 'en'].contains(locale.languageCode);
@override
Future<AppLocalization> load(Locale locale) async {
AppLocalization localizations = new AppLocalization(locale);
await localizations.load();
print("Load ${locale.languageCode}");
return localizations;
}
@override
bool shouldReload(AppLocalizationDelegate old) => false;
}
class MyApp extends StatefulWidget {
static void setLocale(BuildContext context, Locale locale) {
_MyAppState? state = context.findAncestorStateOfType<_MyAppState>();
state?.setLocale(locale);
}
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Locale? _locale;
void setLocale(Locale locale) {
setState(() {
_locale = locale;
});
}
@override
Widget build(BuildContext context) {
// Firebase.initializeApp();
return new MaterialApp(
debugShowCheckedModeBanner: false,
locale:_locale,
supportedLocales: [
const Locale('ar', 'SA'),
const Locale('en', 'US')
],
localizationsDelegates: [
const AppLocalizationDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
localeResolutionCallback: (Locale? locale, Iterable<Locale> supportedLocales) {
for (Locale supportedLocale in supportedLocales) {
if (supportedLocale.languageCode == locale?.languageCode || supportedLocale.countryCode == locale?.countryCode) {
return supportedLocale;
}
}
return supportedLocales.first;
},
title: 'Flutter Internationalization',
home: new MyPage(),
);
}
}
class MyPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Center(
child: new Text(
AppLocalization.of(context)!.trans('hello_world')
),
),
);
}
}
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(MyApp());
}