devicelocale 0.2.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

devicelocale #

Gets the device locale data, independent of the app locale settings.

Usage #

import 'package:devicelocale/devicelocale.dart';

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 or just the currently set device locale.

iOS #

When first created this was a Swift based plugin, but due to these issues the Swift code has been removed and it now uses a simple Objective C call.

Updates #

  • Apr 2020 0.2.3 Updated Android code from contributions from @ened

  • Mar 2020 0.2.2 Updated iOS so that if detected locale is null then it is not included in return value

  • Sep 2019 0.2.0 Updated Android following feedback from https://github.com/huzhirento . Fallback to the currentLocale when attempting to get a list of locales fro Android 7 and below as the getLocales() call is a feature of API 24 and failed for Android 7 and below.

Getting Started #

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

0.2.3 - 2 Apr 2020 #

  • updates to Android code

0.2.2 - 14 March 2020 #

  • update iOS so that locale does not show if it is null

0.2.1 - 14 Dec 2019 #

  • enables the plugin to be used in flutter v1.12.13

0.2.0 - 06 Sep 2019 #

  • Updated Android getPreferredLanguages to fallback to the currentLocale if an error occurs when getting getLocales() (below API 24)

0.1.1 - 15 Feb 2019 #

  • Added currentLocale call

0.1.0 #

  • Removed Swift code. Now uses Objective C for iOS implementation

0.0.1 #

  • Initial release.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:devicelocale/devicelocale.dart';

void main() => runApp(MyApp());

/// Demo getting a device locale
class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List _languages = List();
  String _locale;

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  // 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 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
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text("Current locale: "),
              Text('$_locale'),
              Text("Preferred Languages: "),
              Text(_languages.toString()),
              Padding(
                padding: EdgeInsets.all(10),
                child: RaisedButton(
                  onPressed: () {
                    listLanguages();
                  },
                  child: Text("Run Test")
                ),
              ),
            ],
          )
        ),
      ),
    );
  }

  /// testing for issue-12
  void listLanguages() async {
     List languages = await Devicelocale.preferredLanguages;
    String locale = await Devicelocale.currentLocale;
    print('current locale: $locale, preferred device languages:');
    languages.forEach((l) => print(l));
  }


}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  devicelocale: ^0.2.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:devicelocale/devicelocale.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Apr 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8