Please translate my list of strings by using applocalizations on flutter. I am facing problem with lists but it is working correctly for my single strings by using this line of code "AppLocalizations.of(context)!.myFav". How to handle the list of strings using applocalizations?
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,
),
],
);
},
);
2
Answers
You can use ternary operator where you are setting up the static list view text
If you have taken a variable which stores the language code ex – en for English or mr for Marathi, then your code will look like below
You will have to store selected language variable in above string defined.
Note: This solutions helps if your app have language support upto 2 or 3 languages.
strings.json file content
code.dart file content
This code will display the following text: