forked from egovernments/DIGIT-Works
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #279 from odisha-muktasoft/ref-dynamic-localization
Hive no-SQL database has been implemented in replace of flutter secure storage for the localisation issues.
- Loading branch information
Showing
29 changed files
with
2,004 additions
and
1,120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
BASE_URL='https://works-dev.digit.org/' | ||
BASE_URL='https://mukta-uat.digit.org/' | ||
MDMS_API_PATH='egov-mdms-service/v1/_search' | ||
GLOBAL_ASSETS='https://works-dev.digit.org/works-dev-asset/worksGlobalConfig.json' | ||
TENANT_ID='pg' | ||
GLOBAL_ASSETS='https://mukta-uat.digit.org/mukta-uat-bucket-s3/muktaGlobalConfig.json' | ||
TENANT_ID='statea' | ||
CONNECT_TIMEOUT="120000" | ||
RECEIVE_TIMEOUT="120000" | ||
SEND_TIMEOUT="120000" | ||
ENV_NAME="DEV" | ||
ENV_NAME="UAT" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 34 additions & 51 deletions
85
frontend/works_shg_app/lib/blocs/localization/app_localization.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,56 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:universal_html/html.dart' as html; | ||
import 'package:works_shg_app/blocs/localization/localization.dart'; | ||
import 'package:hive_flutter/hive_flutter.dart'; | ||
import 'package:works_shg_app/data/schema/localization.dart'; | ||
|
||
import '../../models/localization/localization_model.dart'; | ||
import '../../services/local_storage.dart'; | ||
import '../../utils/constants.dart'; | ||
import 'app_localizations_delegate.dart'; | ||
|
||
class AppLocalizations { | ||
final Locale? locale; | ||
final Locale locale; | ||
|
||
AppLocalizations(this.locale); | ||
AppLocalizations( | ||
this.locale, | ||
); | ||
static AppLocalizations of(BuildContext context) { | ||
return Localizations.of<AppLocalizations>(context, AppLocalizations)!; | ||
} | ||
|
||
static List<LocalizationMessageModel> localizedStrings = | ||
<LocalizationMessageModel>[]; | ||
static const LocalizationsDelegate<AppLocalizations> delegate = | ||
AppLocalizationsDelegate(); | ||
|
||
Future<List<LocalizationMessageModel>?> getLocalizationLabels() async { | ||
dynamic localLabelResponse; | ||
if (kIsWeb) { | ||
localLabelResponse = html.window.sessionStorage[ | ||
'${locale?.languageCode}_${locale?.countryCode}' ?? '']; | ||
} else { | ||
localLabelResponse = await storage.read( | ||
key: '${locale?.languageCode}_${locale?.countryCode}'); | ||
} | ||
await Future.delayed(const Duration(seconds: 1)); | ||
if (localLabelResponse != null && localLabelResponse.trim().isNotEmpty) { | ||
return localizedStrings = jsonDecode(localLabelResponse) | ||
.map<LocalizationMessageModel>( | ||
(e) => LocalizationMessageModel.fromJson(e)) | ||
.toList(); | ||
} else { | ||
localizedStrings = BlocProvider.of<LocalizationBloc>( | ||
scaffoldMessengerKey.currentContext!) | ||
.state | ||
.maybeWhen( | ||
orElse: () => [], | ||
loaded: (List<LocalizationMessageModel>? localization) { | ||
return localization; | ||
}) ?? | ||
[]; | ||
|
||
return localizedStrings; | ||
static List<dynamic> localizedStrings = <dynamic>[]; | ||
|
||
// Returns instance of custom localizations delegate | ||
static LocalizationsDelegate<AppLocalizations> getDelegate() => | ||
const AppLocalizationsDelegate(); | ||
|
||
/* it fetches data from hive box based on the locale selection: | ||
- store the list of data to localizedStrings | ||
- for searching increasing efficiency | ||
*/ | ||
Future<bool> load({required String locale}) async { | ||
// Clear the list before loading localized strings | ||
localizedStrings.clear(); | ||
|
||
// Get box for localization | ||
final box = Hive.box<KeyLocaleModel>('keyValueModel'); | ||
// Convert values to list | ||
final List<KeyLocaleModel> ll = box.values.toList(); | ||
if (ll.isNotEmpty) { | ||
final localizationList = | ||
ll.firstWhere((element) => element.locale == locale); | ||
|
||
if (localizationList.localizationsList != null) { | ||
localizedStrings.addAll(localizationList.localizationsList!); | ||
} | ||
} | ||
} | ||
|
||
Future<bool> load() async { | ||
if (scaffoldMessengerKey.currentContext != null) { | ||
await getLocalizationLabels(); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
translate( | ||
String localizedValues, | ||
) { | ||
// Find index of localized string | ||
var index = | ||
localizedStrings.indexWhere((medium) => medium.code == localizedValues); | ||
// Return localized string if found, otherwise return original value | ||
return index != -1 ? localizedStrings[index].message : localizedValues; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.