diff --git a/lib/src/language_builder.dart b/lib/src/language_builder.dart index c68aed8..c04648f 100644 --- a/lib/src/language_builder.dart +++ b/lib/src/language_builder.dart @@ -53,7 +53,7 @@ class _LanguageBuilderState extends State with UpdateLanguage { if ((widget.forceRebuild == true || (widget.forceRebuild == null && _languageHelper._forceRebuild))) { if (_languageHelper._states.add(this)) { - printDebug( + printDebug(() => 'Added $this to the states because the `forceRebuild` is `true`'); } } else { @@ -63,17 +63,17 @@ class _LanguageBuilderState extends State with UpdateLanguage { // so all the posible `root` widgets have definitely been added to the list // of the states. So we just need to add the state that its' parent is null. if (getRoot == null && _languageHelper._states.add(this)) { - printDebug('Added $this to the states'); + printDebug(() => 'Added $this to the states'); } else { - printDebug('$this was already contained in the states'); + printDebug(() => '$this was already contained in the states'); } } - printDebug('Length of the states: ${_languageHelper._states.length}'); + printDebug(() => 'Length of the states: ${_languageHelper._states.length}'); } @override void dispose() { - printDebug('Removed $this from the states'); + printDebug(() => 'Removed $this from the states'); _languageHelper._states.remove(this); super.dispose(); } diff --git a/lib/src/language_helper.dart b/lib/src/language_helper.dart index a7202bb..243ba83 100644 --- a/lib/src/language_helper.dart +++ b/lib/src/language_helper.dart @@ -257,7 +257,7 @@ class LanguageHelper { // When the `data` is empty, a temporary data will be added. if (_dataProviders.isEmpty) { - printDebug( + printDebug(() => 'The `data` is empty, we will use a temporary `data` for the developing state'); _dataProviders = [ LanguageDataProvider.data({LanguageCodes.en: {}}) @@ -299,7 +299,7 @@ class LanguageHelper { // Sync with device only track the changing of the device language, // so it will not use the device language for the app at the first time. prefs.setString(_deviceCodeKey, currentCode.code); - printDebug( + printDebug(() => 'Sync with device saved the current language to local database.'); } else { // We only consider to change the app language when the device language @@ -308,9 +308,9 @@ class LanguageHelper { if (currentCode != prefCode) { finalCode = currentCode; prefs.setString(_deviceCodeKey, currentCode.code); - printDebug('Sync with device applied the new device language'); + printDebug(() => 'Sync with device applied the new device language'); } else { - printDebug('Sync with device used the current app language'); + printDebug(() => 'Sync with device used the current app language'); } } } @@ -320,7 +320,7 @@ class LanguageHelper { if (isOptionalCountryCode && finalCode.locale.countryCode != null) { // Try to use the `languageCode` only if the `languageCode_countryCode` // is not available - printDebug( + printDebug(() => 'language does not contain the $finalCode => Try to use the `languageCode` only..'); try { tempCode = LanguageCodes.fromCode(finalCode.locale.languageCode); @@ -331,17 +331,17 @@ class LanguageHelper { } if (tempCode == null) { - printDebug( + printDebug(() => 'Unable to use the `languageCode` only => Change the code to ${codes.first}'); } else { - printDebug( + printDebug(() => 'Able to use the `languageCode` only => Change the code to $tempCode'); } finalCode = tempCode ?? codes.first; } - printDebug('Set `currentCode` to $finalCode'); + printDebug(() => 'Set `currentCode` to $finalCode'); _currentCode = finalCode; _data.addAll(await _dataProvider.getData(code)); @@ -377,7 +377,7 @@ class LanguageHelper { _addData(data: getData, database: _data, overwrite: overwrite); _codes.addAll(await data.getSupportedCodes()); if (activate) change(code); - printDebug( + printDebug(() => 'The new `data` is added and activated with overwrite is $overwrite'); } @@ -397,7 +397,7 @@ class LanguageHelper { _addData(data: getData, database: _dataOverrides, overwrite: overwrite); _codesOverrides.addAll(await dataOverrides.getSupportedCodes()); if (activate) change(code); - printDebug( + printDebug(() => 'The new `dataOverrides` is added and activated with overwrite is $overwrite'); } @@ -421,14 +421,14 @@ class LanguageHelper { final stringParams = params.map((key, value) => MapEntry(key, '$value')); if (!codes.contains(toCode) && !codesOverrides.contains(toCode)) { - printDebug( + printDebug(() => 'Cannot translate this text because $toCode is not available in `data` and `dataOverrides` ($text)'); return _replaceParams(text, stringParams); } final translated = _dataOverrides[toCode]?[text] ?? _data[toCode]![text]; if (translated == null) { - printDebug('This text is not contained in current $toCode ($text)'); + printDebug(() => 'This text is not contained in current $toCode ($text)'); return _replaceParams(text, stringParams); } @@ -445,25 +445,25 @@ class LanguageHelper { /// Change the language to this [code] Future change(LanguageCodes toCode) async { if (!codes.contains(toCode)) { - printDebug('$toCode is not available in `data` or `dataOverrides`'); + printDebug(() => '$toCode is not available in `data` or `dataOverrides`'); if (!_useInitialCodeWhenUnavailable) { - printDebug( + printDebug(() => 'Does not allow using the initial code => Cannot change the language.'); return; } else { if (codes.contains(_initialCode)) { - printDebug( + printDebug(() => '`useInitialCodeWhenUnavailable` is true => Change the language to $_initialCode'); _currentCode = _initialCode; } else { - printDebug( + printDebug(() => '`useInitialCodeWhenUnavailable` is true but the `initialCode` is not available in `data` or `dataOverrides` => Cannot change the language'); return; } } } else { - printDebug('Set currentCode to $toCode'); + printDebug(() => 'Set currentCode to $toCode'); _currentCode = toCode; } @@ -478,7 +478,7 @@ class LanguageHelper { _dataOverrides.addAll(await _dataOverridesProvider.getData(code)); } - printDebug('Change language to $toCode for ${_states.length} states'); + printDebug(() => 'Change language to $toCode for ${_states.length} states'); for (var state in _states) { state.updateLanguage(); } @@ -490,13 +490,13 @@ class LanguageHelper { // Save to local memory if (_isAutoSave) { - printDebug('Save this $toCode to local memory'); + printDebug(() => 'Save this $toCode to local memory'); SharedPreferences.getInstance().then((pref) { pref.setString(_autoSaveCodeKey, toCode.code); }); } - printDebug('Changing completed!'); + printDebug(() => 'Changing completed!'); } /// Change the [useInitialCodeWhenUnavailable] value @@ -589,7 +589,7 @@ class LanguageHelper { buffer.write('=================================================='); buffer.write('\n'); - printDebug(buffer.toString()); + printDebug(() => buffer.toString()); return buffer.toString(); } @@ -665,7 +665,7 @@ class LanguageHelper { String fallback, ) { if (!params.containsKey(translateCondition.param)) { - printDebug( + printDebug(() => 'The params does not contain the condition param: ${translateCondition.param}'); return _replaceParams(fallback, params); } @@ -676,7 +676,7 @@ class LanguageHelper { conditions[param] ?? conditions['default'] ?? conditions['_']; if (translated == null) { - printDebug( + printDebug(() => 'There is no result for key $param of condition ${translateCondition.param}'); return _replaceParams(fallback, params); } diff --git a/lib/src/models/language_data_provider.dart b/lib/src/models/language_data_provider.dart index 38f27b3..4d5debd 100644 --- a/lib/src/models/language_data_provider.dart +++ b/lib/src/models/language_data_provider.dart @@ -12,7 +12,7 @@ class LanguageDataProvider { try { return await rootBundle.loadString(path); } catch (_) { - printDebug('The $path does not exist in the assets'); + printDebug(() => () => 'The $path does not exist in the assets'); } return Future.value(''); } diff --git a/lib/src/utils/print_debug.dart b/lib/src/utils/print_debug.dart index 95e2084..bfabe2f 100644 --- a/lib/src/utils/print_debug.dart +++ b/lib/src/utils/print_debug.dart @@ -2,6 +2,6 @@ import 'package:flutter/foundation.dart'; import 'package:language_helper/src/language_helper.dart'; /// Internal function, print debug log -void printDebug(Object? object) => LanguageHelper.instance.isDebug - ? debugPrint('[Language Helper] $object') +void printDebug(Object? Function() object) => LanguageHelper.instance.isDebug + ? debugPrint('[Language Helper] ${object()}') : null; diff --git a/lib/src/utils/serializer.dart b/lib/src/utils/serializer.dart index ef5a6dd..1ab7b29 100644 --- a/lib/src/utils/serializer.dart +++ b/lib/src/utils/serializer.dart @@ -48,16 +48,18 @@ Map languageDataValuesFromMap(Map map) { /// | | | |- en.json /// | | | |- vi.json void exportJson(LanguageData data, String path) { - printDebug('==========================================================='); - printDebug('Exporting Json...'); + printDebug( + () => '==========================================================='); + printDebug(() => 'Exporting Json...'); _exportJsonCodes(data, path); _exportJsonLanguages(data, path); - printDebug('Exported Json'); - printDebug('==========================================================='); + printDebug(() => 'Exported Json'); + printDebug( + () => '==========================================================='); } void _exportJsonCodes(LanguageData data, String path) { - printDebug('Creating codes.json...'); + printDebug(() => 'Creating codes.json...'); JsonEncoder encoder = const JsonEncoder.withIndent(' '); @@ -66,11 +68,11 @@ void _exportJsonCodes(LanguageData data, String path) { final codes = data.keys.map((e) => e.code).toList(); desFile.writeAsStringSync(encoder.convert(codes)); - printDebug('Created codes.json'); + printDebug(() => 'Created codes.json'); } void _exportJsonLanguages(LanguageData data, String path) { - printDebug('Creating languages json files...'); + printDebug(() => 'Creating languages json files...'); JsonEncoder encoder = const JsonEncoder.withIndent(' '); @@ -83,5 +85,5 @@ void _exportJsonLanguages(LanguageData data, String path) { desFile.writeAsStringSync(data); } - printDebug('Created languages json files'); + printDebug(() => 'Created languages json files'); } diff --git a/lib/src/utils/utils.dart b/lib/src/utils/utils.dart index 141b5fc..cf9e038 100644 --- a/lib/src/utils/utils.dart +++ b/lib/src/utils/utils.dart @@ -24,11 +24,11 @@ class Utils { if (response.statusCode == 200) { return utf8.decode(response.bodyBytes); } else { - printDebug( + printDebug(() => 'Failed to load data from URL. Status code: ${response.statusCode}'); } } catch (e) { - printDebug('Error fetching data from URL: $e'); + printDebug(() => 'Error fetching data from URL: $e'); } return '';