Skip to content

Commit

Permalink
added defaultLocale functions
Browse files Browse the repository at this point in the history
  • Loading branch information
magnatronus committed Feb 20, 2023
1 parent 6b22dd9 commit 772d897
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion example/ios/Flutter/AppFrameworkInfo.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>9.0</string>
<string>11.0</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
6 changes: 3 additions & 3 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 7 additions & 5 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 46;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -166,7 +166,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1020;
LastUpgradeCheck = 1300;
ORGANIZATIONNAME = "";
TargetAttributes = {
97C146ED1CF9000F007C117D = {
Expand Down Expand Up @@ -209,6 +209,7 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -245,6 +246,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
LastUpgradeVersion = "1300"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
4 changes: 4 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,9 @@
</array>
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
29 changes: 25 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MyApp extends StatefulWidget {
class _MyAppState extends State<MyApp> {
List? _languages = [];
String? _currentLocale;
String? _defaultLocale;
bool _isSupported = false;

@override
Expand All @@ -32,7 +33,14 @@ class _MyAppState extends State<MyApp> {
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,
Expand All @@ -47,7 +55,7 @@ class _MyAppState extends State<MyApp> {
Text("Preferred Languages: "),
Text(_languages.toString()),
ElevatedButton(
onPressed: _getPreferedLanguages,
onPressed: _getPreferredLanguages,
child: Text("Refresh"),
),
SizedBox(height: 8),
Expand Down Expand Up @@ -87,10 +95,23 @@ class _MyAppState extends State<MyApp> {

Future<void> _initPlatformState() async {
_getCurrentLocale();
_getPreferedLanguages();
_getDefaultLocale();
_getPreferredLanguages();
_checkForSupport();
}

Future<void> _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<void> _getCurrentLocale() async {
try {
final currentLocale = await Devicelocale.currentLocale;
Expand All @@ -103,7 +124,7 @@ class _MyAppState extends State<MyApp> {
}
}

Future<void> _getPreferedLanguages() async {
Future<void> _getPreferredLanguages() async {
try {
final languages = await Devicelocale.preferredLanguages;
print((languages != null)
Expand Down
18 changes: 17 additions & 1 deletion lib/devicelocale.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -64,6 +64,22 @@ class Devicelocale {
.cast<Locale>();
}

/// 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<String?> 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<Locale?> 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<String?> get currentLocale async {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 772d897

Please sign in to comment.