请使用 flutter 上的 applocalizations 翻译我的字符串列表。我面临列表问题,但通过使用这行代码“AppLocalizations.of(context)!.myFav”,它可以正确地处理我的单个字符串。如何使用应用程序本地化处理字符串列表?
List<ProfileItems> items = [
ProfileItems(iconName: AppImage.heart, title: "myFav"),
ProfileItems(iconName: AppImage.save_collection, title: "myColec"),
ProfileItems(iconName: AppImage.my_purchases, title: "myPurchase"),
ProfileItems(iconName: AppImage.my_gifts, title: "myGift"),
ProfileItems(iconName: AppImage.preferences, title: "myPref"),
];
ListView.builder(
itemCount: items.length,
padding: EdgeInsets.zero,
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, index) {
return Column(
children: [
Container(
height: 40,
margin: EdgeInsets.symmetric(horizontal: 8),
child: Center(
child: ListTile(
minLeadingWidth: 10,
visualDensity: VisualDensity(horizontal: 0, vertical: -4),
leading: SvgPicture.asset(
items[index].iconName,
fit: BoxFit.cover,
color: themeController.theme.cardColor,
),
title: AppText.appText(
items[index].title, // AppLocalizations.of(context)!
fontFamily: true,
fontSize: 14,
fontWeight: FontWeight.normal,
textColor: themeController.theme.cardColor,
),
trailing: Icon(
Icons.navigate_next_outlined,
color: themeController.theme.cardColor,
),
),
),
),
const Divider(
indent: 8,
endIndent: 8,
thickness: 1,
),
],
);
},
);
您可以在设置静态列表视图文本时使用三元运算符 如果您使用了一个变量来存储语言代码 ex - en(英语)或 mr(马拉地语),那么您的代码将如下所示
late String appLang = "";
您必须将选定的语言变量存储在上面定义的字符串中。
List<ProfileItems> items = [
ProfileItems(iconName: AppImage.heart, title: appLang == 'en' ? 'MyFav' : 'माझे आवडते'),
ProfileItems(iconName: AppImage.save_collection, title: appLang == 'en' ? 'MyCollection' : 'माय कलेक्शन'),
];