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.
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:
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:
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 plugin:
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 package.
Top comments (0)