The example https://pub.dev/packages/get/example only shows setting the locale from a string manually:
void main() {
runApp(GetMaterialApp(
...
locale: Locale('pt', 'BR'),
...
If I don't use "locale: Locale ('pt', 'BR')", then code like Text("title".tr) don't work. So, I need setup current locale to "locale" property, but how get current locale?
Obviously, both are used for state management. However, experienced Flutter devs do not recommend GetX. Do not use GetX.
An identifier used to select a user's language and formatting preferences. This represents a Unicode Language Identifier (i.e. without Locale extensions), except variants are not supported. Locales are canonicalized according to the "preferred value" entries in the IANA Language Subtag Registry.
if BuildContext is available, we can get current locale this way
Locale myLocale = Localizations.localeOf(context);
according to the documentation https://flutter.dev/docs/development/accessibility-and-localization/internationalization
another option is to use the official intl library
import 'package:intl/intl.dart';
String locale = Intl.getCurrentLocale(); // returns language tag, such as en_US
To get Device Current Locale:
import 'package:get/get.dart';
return GetMaterialApp(
locale: Get.deviceLocale, //returns Locale('<language code>', '<country code>')
);
To get the App Current Locale:
import 'package:get/get.dart';
return GetMaterialApp(
locale: Get.locale, //returns Locale('<language code>', '<country code>')
);
GetX | Flutter
Sample Code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With