Skip to content

Commit

Permalink
added currentLocale call
Browse files Browse the repository at this point in the history
  • Loading branch information
SpiralArm Consulting Ltd committed Feb 15, 2019
1 parent 1a09cf9 commit 7ca1cea
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions android/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin/default"/>
</classpath>
23 changes: 23 additions & 0 deletions android/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>devicelocale</name>
<comment>Project android created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
connection.project.dir=../example/android
eclipse.preferences.version=1
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> getPreferredLanguages() {
LocaleList list = Resources.getSystem().getConfiguration().getLocales().getAdjustedDefault();
List<String> result = new ArrayList<String>();
Expand Down
22 changes: 20 additions & 2 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 = List();
String _locale;

@override
void initState() {
Expand All @@ -23,12 +24,20 @@ class _MyAppState extends State<MyApp> {
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> 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
Expand All @@ -38,6 +47,7 @@ class _MyAppState extends State<MyApp> {

setState(() {
_languages = languages;
_locale = currentLocale;
});
}

Expand All @@ -49,7 +59,15 @@ class _MyAppState extends State<MyApp> {
title: const Text('Plugin example app'),
),
body: Center(
child: Text('$_languages\n'),
child: Column(
children: <Widget>[
Text("Current locale: "),
Text('$_locale'),
Text("Preferred Languages: "),
Text(_languages.toString()),

],
)
),
),
);
Expand Down
6 changes: 6 additions & 0 deletions ios/Classes/DevicelocalePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)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);
}
Expand Down
17 changes: 11 additions & 6 deletions lib/devicelocale.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List> 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<String> get currentLocale async {
final String locale = await _channel.invokeMethod('currentLocale');
return locale;
}
}
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.1.0
version: 0.1.1
author: Steve Rogers <[email protected]>
homepage: https://github.com/magnatronus/flutter-devicelocale

Expand Down

0 comments on commit 7ca1cea

Please sign in to comment.