我正在创建一个PopupMenuButton
以在本地更改应用程序的语言,但我不知道如何加载不同的语言。
这是我为更改语言而创建的小部件,我想将应用程序语言设置为 PopupMenu 项的值。
import 'package:flutter/material.dart';
import '../generated/l10n.dart';
class ChangeLanguage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return PopupMenuButton(
offset: const Offset(0, 40),
onSelected: (value) {
//set language to value
print(L.current);
print(Localizations.localeOf(context).toString());
},
itemBuilder: (BuildContext context) => [
PopupMenuItem(
value: "de",
child: Text(
"Deutsch",
style: Theme.of(context).textTheme.bodyText2,
)),
PopupMenuItem(
value: "en",
child: Text(
"English",
style: Theme.of(context).textTheme.bodyText2,
)),
],
);
}
}
我在 main.dart 文件中实现了 flutter_localizations 和 intl,如下所示:
localizationsDelegates: [
L.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: L.delegate.supportedLocales,
支持的语言是使用 intl 包生成的en
。de