flutter_localizations sdk 2.15.0

73 views
Skip to first unread message

M

unread,
Dec 13, 2021, 7:32:18 PM12/13/21
to Flutter Development (flutter-dev)

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());
}
Screen Shot 1443-05-10 at 3.23.53 AM.png

M

unread,
Dec 14, 2021, 5:06:02 PM12/14/21
to Flutter Development (flutter-dev)
Just remove 

String? 
But i have issues like video attached 
The problem like #### 1 Seconds just Arabic lang
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/26f0b2ef-ab63-46ae-b1a6-f1a43e97d9f5n%40googlegroups.com.
Screen Recording 1443-05-11 at 12.55.04 AM.mov
Reply all
Reply to author
Forward
0 new messages