Ser*_* S. 7 get internationalization flutter
示例 https://pub.dev/packages/get/example 仅显示手动从字符串设置语言环境:
void main() {
runApp(GetMaterialApp(
...
locale: Locale('pt', 'BR'),
...
Run Code Online (Sandbox Code Playgroud)
如果我不使用“locale: Locale ('pt', 'BR')”,那么像 Text("title".tr) 这样的代码就不起作用。所以,我需要将当前语言环境设置为“语言环境”属性,但如何获取当前语言环境?
小智 12
要获取设备当前区域设置:
import 'package:get/get.dart';
return GetMaterialApp(
locale: Get.deviceLocale, //returns Locale('<language code>', '<country code>')
);
Run Code Online (Sandbox Code Playgroud)
要获取应用程序当前区域设置:
import 'package:get/get.dart';
return GetMaterialApp(
locale: Get.locale, //returns Locale('<language code>', '<country code>')
);
Run Code Online (Sandbox Code Playgroud)
如果 BuildContext 可用,我们可以通过这种方式获取当前语言环境
Locale myLocale = Localizations.localeOf(context);
Run Code Online (Sandbox Code Playgroud)
根据文档https://flutter.dev/docs/development/accessibility-and-localization/internationalization
另一种选择是使用官方国际图书馆
import 'package:intl/intl.dart';
String locale = Intl.getCurrentLocale(); // returns language tag, such as en_US
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您需要设备区域设置,您可以使用
return GetMaterialApp(
locale: Get.deviceLocale,
);
Run Code Online (Sandbox Code Playgroud)
但你需要当前的应用程序区域设置,你必须使用
final locale = Get.locale;
Run Code Online (Sandbox Code Playgroud)
该包的作者在自述文件中添加了相关信息(https://pub.dev/packages/get#internationalization):
要读取系统区域设置,您可以使用 window.locale。
import 'dart:ui' as ui;
return GetMaterialApp(
locale: ui.window.locale,
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3655 次 |
| 最近记录: |