diff --git a/CHANGELOG.md b/CHANGELOG.md index bbbbbec..5a04da5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.1 - 15 Feb 2019 + +* Added currentLocale call + ## 0.1.0 * Removed Swift code. Now uses Objective C for iOS implementation diff --git a/README.md b/README.md index 1dff8a7..2027fba 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,10 @@ then ``` List languages = await Devicelocale.preferredLanguages; +String locale = await Devicelocale.currentLocale; ``` -this should return a list of the preferred/current language locales setup on the device, with the current one being the first in the list. +this should return a list of the preferred/current language locales setup on the device, with the current one being the first in the list or just the currently set device locale. ## iOS diff --git a/android/.classpath b/android/.classpath new file mode 100644 index 0000000..eb19361 --- /dev/null +++ b/android/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/android/.project b/android/.project new file mode 100644 index 0000000..aea9379 --- /dev/null +++ b/android/.project @@ -0,0 +1,23 @@ + + + devicelocale + Project android created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.buildship.core.gradleprojectnature + + diff --git a/android/.settings/org.eclipse.buildship.core.prefs b/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..6aa97a9 --- /dev/null +++ b/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,2 @@ +connection.project.dir=../example/android +eclipse.preferences.version=1 diff --git a/android/src/main/java/com/example/devicelocale/DevicelocalePlugin.java b/android/src/main/java/com/example/devicelocale/DevicelocalePlugin.java index 216c699..350b609 100644 --- a/android/src/main/java/com/example/devicelocale/DevicelocalePlugin.java +++ b/android/src/main/java/com/example/devicelocale/DevicelocalePlugin.java @@ -22,10 +22,16 @@ public void onMethodCall(MethodCall call, Result result) { String method = call.method; switch(method){ case "preferredLanguages": result.success(getPreferredLanguages()); break; + case "currentLocale": result.success(getCurrentLocale()); break; default: result.notImplemented(); } } + private String getCurrentLocale() { + String locale = Locale.getDefault().toString(); + return locale; + } + private List getPreferredLanguages() { LocaleList list = Resources.getSystem().getConfiguration().getLocales().getAdjustedDefault(); List result = new ArrayList(); diff --git a/example/lib/main.dart b/example/lib/main.dart index 476f01f..5f3db63 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 = List(); + String _locale; @override void initState() { @@ -23,12 +24,20 @@ class _MyAppState extends State { // Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { List languages; + String currentLocale; // Platform messages may fail, so we use a try/catch PlatformException. try { languages = await Devicelocale.preferredLanguages; + print(languages); } on PlatformException { - print("Error obtaining preferred language"); + print("Error obtaining preferred languages"); + } + try { + currentLocale = await Devicelocale.currentLocale; + print(currentLocale); + } on PlatformException { + print("Error obtaining current locale"); } // If the widget was removed from the tree while the asynchronous platform @@ -38,6 +47,7 @@ class _MyAppState extends State { setState(() { _languages = languages; + _locale = currentLocale; }); } @@ -49,7 +59,15 @@ class _MyAppState extends State { title: const Text('Plugin example app'), ), body: Center( - child: Text('$_languages\n'), + child: Column( + children: [ + Text("Current locale: "), + Text('$_locale'), + Text("Preferred Languages: "), + Text(_languages.toString()), + + ], + ) ), ), ); diff --git a/ios/Classes/DevicelocalePlugin.m b/ios/Classes/DevicelocalePlugin.m index acdc0fc..f3cc420 100644 --- a/ios/Classes/DevicelocalePlugin.m +++ b/ios/Classes/DevicelocalePlugin.m @@ -10,8 +10,14 @@ + (void)registerWithRegistrar:(NSObject*)registrar { } - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { + if ([@"preferredLanguages" isEqualToString:call.method]) { result([NSLocale preferredLanguages]); + } else if([@"currentLocale" isEqualToString:call.method]){ + NSString *locale = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode]; + NSString *language = [[NSLocale currentLocale] objectForKey: NSLocaleLanguageCode]; + NSString *formattedStr = [NSString stringWithFormat:@"%@-%@",language, locale]; + result(formattedStr); } else { result(FlutterMethodNotImplemented); } diff --git a/lib/devicelocale.dart b/lib/devicelocale.dart index dc35685..d555778 100644 --- a/lib/devicelocale.dart +++ b/lib/devicelocale.dart @@ -3,18 +3,23 @@ import 'dart:async'; import 'package:flutter/services.dart'; -/// A Simple plug-in that can be used to interogate a device( iOS or Android) to obtain a list of current set up locales -/// -/// The currentl default locale should be the first in the list +/// A Simple plug-in that can be used to interogate a device( iOS or Android) to obtain a list of current set up locales and languages class Devicelocale { static const MethodChannel _channel = const MethodChannel('uk.spiralarm.flutter/devicelocale'); - /// Returns a 'List' of locales from the device - /// - /// for example **['en-GB', 'es-GB']** + /// Returns a [List] of locales from the device + /// the first in the list should be the current one set on the device + /// for example iOS **['en-GB', 'es-GB'] or for Android **['en_GB, 'es_GB]** static Future get preferredLanguages async { final List version = await _channel.invokeMethod('preferredLanguages'); return version; } + + /// 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 { + final String locale = await _channel.invokeMethod('currentLocale'); + return locale; + } } diff --git a/pubspec.yaml b/pubspec.yaml index 421a3cc..73b6362 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.1.0 +version: 0.1.1 author: Steve Rogers homepage: https://github.com/magnatronus/flutter-devicelocale