diff --git a/.gitignore b/.gitignore index acc8fb0..75e160a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ .buildlog/ .history .svn/ +.vscode # IntelliJ related *.iml diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ab4ed6..9062383 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -125,4 +125,9 @@ ## [4.1.0] -- Upgraded packages and SDK version \ No newline at end of file +- Upgraded packages and SDK version + +## [4.1.2] + +- Upgraded packages and SDK version +- Fixed [breaking change](https://docs.flutter.dev/release/breaking-changes/asset-manifest-dot-json) from Flutter \ No newline at end of file diff --git a/lib/src/constants/constants.dart b/lib/src/constants/constants.dart index 94c57cc..c041ec8 100644 --- a/lib/src/constants/constants.dart +++ b/lib/src/constants/constants.dart @@ -1,15 +1,12 @@ -class Constants -{ - static const String assetManifestFilename = 'AssetManifest.json'; +class Constants { + static const String localizedAssetsPath = 'assets/i18n'; - static const String localizedAssetsPath = 'assets/i18n'; + static const String pluralZero = 'zero'; + static const String pluralOne = 'one'; + static const String pluralTwo = 'two'; + static const String pluralFew = 'few'; + static const String pluralMany = 'many'; + static const String pluralOther = 'other'; - static const String pluralZero = 'zero'; - static const String pluralOne = 'one'; - static const String pluralTwo = 'two'; - static const String pluralFew = 'few'; - static const String pluralMany = 'many'; - static const String pluralOther = 'other'; - - static const String pluralValueArg = '{{value}}'; -} \ No newline at end of file + static const String pluralValueArg = '{{value}}'; +} diff --git a/lib/src/services/locale_file_service.dart b/lib/src/services/locale_file_service.dart index c9c0d0b..250e5b0 100644 --- a/lib/src/services/locale_file_service.dart +++ b/lib/src/services/locale_file_service.dart @@ -1,69 +1,60 @@ import 'dart:convert'; import 'package:flutter/services.dart'; -import 'package:flutter_translate/src/constants/constants.dart'; -class LocaleFileService -{ - static Future> getLocaleFiles(List locales, String basePath) async - { - var localizedFiles = await _getAllLocaleFiles(basePath); +class LocaleFileService { + static Future> getLocaleFiles( + List locales, String basePath) async { + var localizedFiles = await _getAllLocaleFiles(basePath); - final files = new Map(); + final files = new Map(); - for(final language in locales.toSet()) - { - var file = _findLocaleFile(language, localizedFiles, basePath); + for (final language in locales.toSet()) { + var file = _findLocaleFile(language, localizedFiles, basePath); - files[language] = file; - } - - return files; + files[language] = file; } - static Future getLocaleContent(String file) async - { - final ByteData? data = await rootBundle.load(file); + return files; + } - if (data == null) return null; - - return utf8.decode(data.buffer.asUint8List()); - } + static Future getLocaleContent(String file) async { + final ByteData? data = await rootBundle.load(file); - static Future> _getAllLocaleFiles(String basePath) async - { - final manifest = await rootBundle.loadString(Constants.assetManifestFilename); + if (data == null) return null; - Map map = jsonDecode(manifest); + return utf8.decode(data.buffer.asUint8List()); + } - var separator = basePath.endsWith('/') ? '' : '/'; + static Future> _getAllLocaleFiles(String basePath) async { + final assetManifest = await AssetManifest.loadFromAssetBundle(rootBundle); + final assets = assetManifest.listAssets(); - return map.keys.where((x) => x.startsWith('$basePath$separator')).toList(); - } + final separator = basePath.endsWith('/') ? '' : '/'; - static String _findLocaleFile(String languageCode, List localizedFiles, String basePath) - { - var file = _getFilepath(languageCode, basePath); + return assets.where((x) => x.startsWith('$basePath$separator')).toList(); + } - if(!localizedFiles.contains(file)) - { - if(languageCode.contains('_')) - { - file = _getFilepath(languageCode.split('_').first, basePath); - } - } + static String _findLocaleFile( + String languageCode, List localizedFiles, String basePath) { + var file = _getFilepath(languageCode, basePath); - if(file == null) - { - throw new Exception('The asset file for the language "$languageCode" was not found.'); - } + if (!localizedFiles.contains(file)) { + if (languageCode.contains('_')) { + file = _getFilepath(languageCode.split('_').first, basePath); + } + } - return file; + if (file == null) { + throw new Exception( + 'The asset file for the language "$languageCode" was not found.'); } - static String? _getFilepath(String languageCode, String basePath) - { - var separator = basePath.endsWith('/') ? '' : '/'; + return file; + } - return '$basePath$separator$languageCode.json'; - } + static String? _getFilepath(String languageCode, String basePath) { + var separator = basePath.endsWith('/') ? '' : '/'; + + return '$basePath$separator$languageCode.json'; + } } diff --git a/lib/src/utils/device_locale.dart b/lib/src/utils/device_locale.dart index ac32e86..74a5bf8 100644 --- a/lib/src/utils/device_locale.dart +++ b/lib/src/utils/device_locale.dart @@ -2,23 +2,22 @@ import 'package:flutter/widgets.dart'; import 'package:universal_io/io.dart'; /// Returns the current device locale -Locale? getCurrentLocale() -{ - return _localeFromString(Platform.localeName); +Locale? getCurrentLocale() { + return _localeFromString(Platform.localeName); } -Locale? _localeFromString(String code) -{ - var separator = code.contains('_') ? '_' : code.contains('-') ? '-' : null; +Locale? _localeFromString(String code) { + var separator = code.contains('_') + ? '_' + : code.contains('-') + ? '-' + : null; - if (separator != null) - { - var parts = code.split(RegExp(separator)); + if (separator != null) { + var parts = code.split(RegExp(separator)); - return Locale(parts[0], parts[1]); - } - else - { - return Locale(code); - } + return Locale(parts[0], parts[1]); + } else { + return Locale(code); + } } diff --git a/pubspec.yaml b/pubspec.yaml index 327ad15..89801d0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: flutter_translate description: Flutter Translate is a fully featured localization / internationalization (i18n) library for Flutter. -version: 4.1.0 +version: 4.1.2 homepage: https://jesway.com repository: https://github.com/Jesway/flutter_translate @@ -11,10 +11,8 @@ dependencies: flutter: sdk: flutter universal_io: ^2.2.2 - intl: ^0.19.0 + intl: ^0.20.2 dev_dependencies: flutter_test: sdk: flutter - -flutter: \ No newline at end of file