Reputation: 1
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,
),
],
);
},
);
Upvotes: 0
Views: 145
Reputation: 1597
Try a function that switches the title strings to translated string.
const myFav = 'myFav';
ProfileItems(iconName = AppImage.heart, title = myFav)
String translateTitle(BuildContext context, String title) {
AppLocalizations appLocalizations = AppLocalizations.of(context)!;
switch (title) {
case myFav:
return appLocalizations.myFav;
// ...
// handle rest of cases
// ...
default:
return 'Default Title';
}
}
Text
widget like sotranslateTitle(context, items[index].title)
Upvotes: 0
Reputation: 41
strings.json file content
{
"Hello, world!" : "こんにちは、世界!"
}
code.dart file content
import 'package:flutter_localizations/flutter_localizations.dart';
class MyApp extends StatelessWidget{
@override
Widget build(BuildContext context) {
final loc = AppLocalizations.of(context);
List<String> strings = ["Hello, world!", "This is a localized string."];
// Get the localized strings.
List<String> localizedStrings = [];
for (String string in strings) {
localizedStrings.add(loc.translate(string));
}
// Display the localized strings.
return Text(localizedStrings.join("\n"));
}
}
This code will display the following text:
こんにちは、世界!
This is a localized string.
Upvotes: 0
Reputation: 836
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
late String appLang = "";
You will have to store selected language variable in above string defined.
List<ProfileItems> items = [
ProfileItems(iconName: AppImage.heart, title: appLang == 'en' ? 'MyFav' : 'माझे आवडते'),
ProfileItems(iconName: AppImage.save_collection, title: appLang == 'en' ? 'MyCollection' : 'माय कलेक्शन'),
];
Note: This solutions helps if your app have language support upto 2 or 3 languages.
Upvotes: 0