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

1

7

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'")
Nivernais answered 19/4, 2021 at 0:12 Comment(1)
I followed the documentation here: localizely.com/i18n-questions/flutter/…Laurentia
B
2

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)
   )
Bagman answered 4/5, 2021 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.