Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter App localization : How to get value with String key?

I can get value of translations with methods like that :

AppLocalizations.of(context).closed

if I have "closed" as String variable like that :

String c = "closed";

how can I get the value with c Variable :

AppLocalizations.of(context).c  ( This Makes Error "The getter 'c' isn't defined for the type 'AppLocalizations'")
like image 796
Mark Sameh Avatar asked Jul 03 '26 18:07

Mark Sameh


1 Answers

I followed the Flutter official doc for Internationalization, but I haved the same problem. So, I have installed on my ide (Visual Studio Code) the plugin Flutter Intl (localizely.flutter-intl), it's available also in Android Studio. To get the dynamic tranlsations, I haved use the ICU format type called: Select (https://support.crowdin.com/icu-message-syntax/#select). For example:

main.dart

localizationsDelegates: [
  S.delegate,
  GlobalMaterialLocalizations.delegate,
  GlobalWidgetsLocalizations.delegate,
  GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,

init_it.arb

"gender": "{gender, select, male{Maschio} female{Femmina} other{entrambi}}",
  "@gender": {
    "description": "Gender type",
    "placeholders": {
      "gender": {}
    }
  }

myWidget.dart

Text(
     S.of(context).gender(gender.name)
   )
like image 87
vcestra Avatar answered Jul 05 '26 08:07

vcestra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!