language localization
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
// network_connectivity.dart
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
@@ -22,13 +20,17 @@ class NetworkConnectivity {
|
||||
void initialize() {
|
||||
_subscription = _connectivity.onConnectivityChanged
|
||||
.listen((List<ConnectivityResult> result) {
|
||||
String status = result.toString();
|
||||
onStatusChange(status);
|
||||
if (result.isNotEmpty) {
|
||||
String status = result.toString();
|
||||
onStatusChange(status);
|
||||
|
||||
if (result[0] == ConnectivityResult.wifi ||
|
||||
result[0] == ConnectivityResult.mobile) {
|
||||
if (goRouter.canPop()) {
|
||||
goRouter.pop(true);
|
||||
if (result[0] == ConnectivityResult.wifi ||
|
||||
result[0] == ConnectivityResult.mobile) {
|
||||
if (goRouter.canPop()) {
|
||||
goRouter.pop(true);
|
||||
}
|
||||
} else {
|
||||
goRouter.pushNamed(RouteName.noInternetScreen);
|
||||
}
|
||||
} else {
|
||||
goRouter.pushNamed(RouteName.noInternetScreen);
|
||||
@@ -42,7 +44,8 @@ class NetworkConnectivity {
|
||||
Future<void> checkInternet() async {
|
||||
final connectivityResult = await _connectivity.checkConnectivity();
|
||||
|
||||
if (connectivityResult[0] != ConnectivityResult.none) {
|
||||
if (connectivityResult.isNotEmpty &&
|
||||
connectivityResult[0] != ConnectivityResult.none) {
|
||||
onStatusChange(connectivityResult.toString());
|
||||
} else {
|
||||
onStatusChange(connectivityResult.toString());
|
||||
|
||||
55
lib/core/utils/language/localizations_delegate.dart
Normal file
55
lib/core/utils/language/localizations_delegate.dart
Normal file
@@ -0,0 +1,55 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
class AppLocalizations {
|
||||
final Locale locale;
|
||||
|
||||
AppLocalizations(this.locale);
|
||||
|
||||
static AppLocalizations of(BuildContext context) {
|
||||
return Localizations.of<AppLocalizations>(context, AppLocalizations)!;
|
||||
}
|
||||
|
||||
static const LocalizationsDelegate<AppLocalizations> delegate =
|
||||
_AppLocalizationsDelegate();
|
||||
|
||||
late Map<String, String> _localizedStrings;
|
||||
|
||||
Future<bool> load() async {
|
||||
String jsonString = await rootBundle
|
||||
.loadString('assets/language/${locale.languageCode}.json');
|
||||
Map<String, dynamic> jsonMap = json.decode(jsonString);
|
||||
|
||||
_localizedStrings = jsonMap.map((key, value) {
|
||||
return MapEntry(key, value.toString());
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
String translate(String key) {
|
||||
return _localizedStrings[key] ?? key;
|
||||
}
|
||||
}
|
||||
|
||||
class _AppLocalizationsDelegate
|
||||
extends LocalizationsDelegate<AppLocalizations> {
|
||||
const _AppLocalizationsDelegate();
|
||||
|
||||
@override
|
||||
bool isSupported(Locale locale) {
|
||||
return ['en', 'ar'].contains(locale.languageCode);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<AppLocalizations> load(Locale locale) async {
|
||||
AppLocalizations localizations = AppLocalizations(locale);
|
||||
await localizations.load();
|
||||
return localizations;
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldReload(_AppLocalizationsDelegate old) => false;
|
||||
}
|
||||
Reference in New Issue
Block a user