Flutter PRO tip #1: Get the language, country and currency of your user
Tags: flutter, android, ios, mobile
Published: 2022-06-08 14:48:17

When you decide to publish your application beyond your territory or region, possibly to the entire world, you may find that in order to execute certain functions is necessary to obtain the language in which the user has configured the device, or maybe what country they are in, or even what is the official currency of their country.

> 📽 Video version available on [YouTube](https://youtu.be/nIFxG4qpKEU) and [Odysee](https://odysee.com/@svprdga:d/flutter-pro-tip-1-get-the-language-country-and-currency-of-your-user)

{% embed https://youtu.be/nIFxG4qpKEU %}

Flutter has a `Localizations.localeOf(context)` method to get a `Locale` object, which holds localization information about the user. However, this information is 'restricted' to the _locales_ that your application supports.

For example, let's imagine that your application supports English for the United States and for the United Kingdom; your main widget would look something like this:

```dart
class App extends StatelessWidget {
  const App({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      title: 'Flutter get locale',
      supportedLocales: [
        Locale('en', 'US'),
        Locale('en', 'GB'),
      ],
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      home: MainScreen(),
    );
  }
}
```
Now you can obtain this data with:

```dart
final locale = Localizations.localeOf(context);
final language = locale.languageCode;
final country = locale.countryCode;
final format = NumberFormat.simpleCurrency(locale: locale.toString());
final currencyName = format.currencyName;
final currencySymbol = format.currencySymbol;
```

If your phone is set to US English and you run the above code you will get the following information:

> Language: en

> Country: US

> Currency: USD

If the phone is configured in UK English you will get:

> Language: en

> Country: GB

> Currency: GBP

But if the phone has configured French of France you will get this:

> Language: en

> Country: US

> Currency: USD

The same goes if you try to get this data from any other localization settings other than US English and UK English.

As you can see, in some cases obtaining this data can be useful if what we want is to completely restrict ourselves to the locales that we have configured; but many other times you will need to get the language, country and currency of your users regardless of whether your app officially 'supports' it. For this we are going to use the [devicelocale](https://pub.dev/packages/devicelocale) plugin:

```dart
final locale = await Devicelocale.currentLocale;

String language = '';
String country = '';

if (locale != null && locale.length >= 2) {
  try {
    language = locale.substring(0, 2);
  } catch (e) {
    debugPrint('Error when fetching user language: $e');
  }
}

if (locale != null && locale.length >= 5) {
  try {
    country = locale.substring(3, 5);
  } catch (e) {
    debugPrint('Error when fetching user country: $e');
  }
}

final format = NumberFormat.simpleCurrency(locale: locale.toString());
final currency = format.currencyName;
final currencySymbol = format.currencySymbol;
```

Continuing with the previous example, if you now try to obtain the data for French of France you will obtain:

> Language: fr

> Country: FR

> Currency: EUR

As a summary, use `Localizations.localeOf(context)` if you want to get location data within the _Locale_ supported by your app; but if you want to get such data even though it is not specified in `supportedLocales` use the [devicelocale](https://pub.dev/packages/devicelocale) package.