From 772d8970c06bcd2522d3d19e05db99ae3afabd60 Mon Sep 17 00:00:00 2001 From: Steve Rogers Date: Mon, 20 Feb 2023 10:43:00 +0000 Subject: [PATCH] added defaultLocale functions --- CHANGELOG.md | 4 +++ README.md | 7 +++++ example/ios/Flutter/AppFrameworkInfo.plist | 2 +- example/ios/Podfile | 2 +- example/ios/Podfile.lock | 6 ++-- example/ios/Runner.xcodeproj/project.pbxproj | 12 ++++---- .../xcshareddata/xcschemes/Runner.xcscheme | 2 +- example/ios/Runner/Info.plist | 4 +++ example/lib/main.dart | 29 ++++++++++++++++--- lib/devicelocale.dart | 18 +++++++++++- pubspec.yaml | 2 +- 11 files changed, 71 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d459613..21480e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.6.0 + +* 19 Feb 2023 - update for iOS issue, added defaultLocale call + ## 0.5.5 * 30 Aug 2022 - pr to merge macos support diff --git a/README.md b/README.md index 457ce8f..b290ed4 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,13 @@ Since GNU/Linux and POSIX doesn't provide a standard API for getting the preferr ## Updates +### Feb 2023 +Added 2 new functions for this [iOS issue](https://github.com/magnatronus/flutter-devicelocale/issues/38) + +- defaultLocale + - defaultAsLocale + + ### July 2022 Added support for per-app language preferences on Android (https://developer.android.com/about/versions/13/features/app-languages#kotlin) You can now set the per-app language if you have an in app language picker. diff --git a/example/ios/Flutter/AppFrameworkInfo.plist b/example/ios/Flutter/AppFrameworkInfo.plist index 8d4492f..9625e10 100644 --- a/example/ios/Flutter/AppFrameworkInfo.plist +++ b/example/ios/Flutter/AppFrameworkInfo.plist @@ -21,6 +21,6 @@ CFBundleVersion 1.0 MinimumOSVersion - 9.0 + 11.0 diff --git a/example/ios/Podfile b/example/ios/Podfile index f7d6a5e..d207307 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -1,5 +1,5 @@ # Uncomment this line to define a global platform for your project -# platform :ios, '9.0' +# platform :ios, '11.0' # CocoaPods analytics sends network stats synchronously affecting flutter build latency. ENV['COCOAPODS_DISABLE_STATS'] = 'true' diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 78b4754..4758b87 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -15,8 +15,8 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: devicelocale: b22617f40038496deffba44747101255cee005b0 - Flutter: 50d75fe2f02b26cc09d224853bb45737f8b3214a + Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854 -PODFILE CHECKSUM: 8e679eca47255a8ca8067c4c67aab20e64cb974d +PODFILE CHECKSUM: 663715e941f9adb426e33bf9376914006f9ea95b -COCOAPODS: 1.10.1 +COCOAPODS: 1.11.3 diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 02e15b5..bd83904 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 46; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ @@ -166,7 +166,7 @@ 97C146E61CF9000F007C117D /* Project object */ = { isa = PBXProject; attributes = { - LastUpgradeCheck = 1020; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = ""; TargetAttributes = { 97C146ED1CF9000F007C117D = { @@ -209,6 +209,7 @@ /* Begin PBXShellScriptBuildPhase section */ 3B06AD1E1E4923F5004D2608 /* Thin Binary */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -245,6 +246,7 @@ }; 9740EEB61CF901F6004384FC /* Run Script */ = { isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; buildActionMask = 2147483647; files = ( ); @@ -333,7 +335,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; @@ -404,7 +406,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MTL_ENABLE_DEBUG_INFO = YES; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; @@ -453,7 +455,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 11.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SUPPORTED_PLATFORMS = iphoneos; diff --git a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme index a28140c..3db53b6 100644 --- a/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme +++ b/example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme @@ -1,6 +1,6 @@ UIViewControllerBasedStatusBarAppearance + CADisableMinimumFrameDurationOnPhone + + UIApplicationSupportsIndirectInputEvents + diff --git a/example/lib/main.dart b/example/lib/main.dart index 783edcb..474ba63 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -13,6 +13,7 @@ class MyApp extends StatefulWidget { class _MyAppState extends State { List? _languages = []; String? _currentLocale; + String? _defaultLocale; bool _isSupported = false; @override @@ -32,7 +33,14 @@ class _MyAppState extends State { child: ListView( padding: EdgeInsets.all(16), children: [ - Text("Current locale: "), + Text("Default locale: "), + Text('$_defaultLocale'), + ElevatedButton( + onPressed: _getDefaultLocale, + child: Text("Refresh"), + ), + SizedBox(height: 8), + Text("Reported Current locale: "), Text('$_currentLocale'), ElevatedButton( onPressed: _getCurrentLocale, @@ -47,7 +55,7 @@ class _MyAppState extends State { Text("Preferred Languages: "), Text(_languages.toString()), ElevatedButton( - onPressed: _getPreferedLanguages, + onPressed: _getPreferredLanguages, child: Text("Refresh"), ), SizedBox(height: 8), @@ -87,10 +95,23 @@ class _MyAppState extends State { Future _initPlatformState() async { _getCurrentLocale(); - _getPreferedLanguages(); + _getDefaultLocale(); + _getPreferredLanguages(); _checkForSupport(); } + Future _getDefaultLocale() async { + try { + final defaultLocale = await Devicelocale.defaultLocale; + print((defaultLocale != null) + ? defaultLocale + : "Unable to get defaultLocale"); + setState(() => _defaultLocale = defaultLocale); + } on PlatformException { + print("Error obtaining default locale"); + } + } + Future _getCurrentLocale() async { try { final currentLocale = await Devicelocale.currentLocale; @@ -103,7 +124,7 @@ class _MyAppState extends State { } } - Future _getPreferedLanguages() async { + Future _getPreferredLanguages() async { try { final languages = await Devicelocale.preferredLanguages; print((languages != null) diff --git a/lib/devicelocale.dart b/lib/devicelocale.dart index 8cc3786..45639cd 100644 --- a/lib/devicelocale.dart +++ b/lib/devicelocale.dart @@ -1,4 +1,4 @@ -/// Copyright (c) 2019-2022, Steve Rogers. All rights reserved. Use of this source code +/// Copyright (c) 2019-2023, Steve Rogers. All rights reserved. Use of this source code /// is governed by an Apache License 2.0 that can be found in the LICENSE file. import 'dart:async'; import 'dart:io'; @@ -64,6 +64,22 @@ class Devicelocale { .cast(); } + /// Returns a [String] of the DEFAULT device locale made up of the language and the region + /// (e.g. en-US or en_US). + static Future get defaultLocale async { + final List? locales = await preferredLanguages; + if (locales == null || locales.isEmpty) { + return await currentLocale; + } + return locales[0]; + } + + /// Returns the DEFAULT [Locale] of the devices made up of the language and the region + static Future get defaultAsLocale async { + final String? locale = await defaultLocale; + return _getAsLocale(locale, null); + } + /// Returns a [String] of the currently set DEVICE locale made up of the language and the region /// (e.g. en-US or en_US) static Future get currentLocale async { diff --git a/pubspec.yaml b/pubspec.yaml index dddc2a7..5865188 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: devicelocale description: A Flutter package that can be used to extract the locales that are currently defined on a device with the current locale set as the first in the list. -version: 0.5.5 +version: 0.6.0 homepage: https://github.com/magnatronus/flutter-devicelocale environment: