How can I give string to translate?

317 views Asked by At

I am using key - value translation-S.of(context).translation_key- but I need something like "Translation string".translate() or translate("Translation string")

How can I do this ?

I am using localizely plugin. Flutter version is lastest.

Attention: I am not asking for translate("key") I am asking translate("Translate String") I want to give a value and get current translation value.

1

There are 1 answers

2
iDecode On

I'd suggest you to do it the other way:

String _localeCode = 'en';

Your method:

String translate(String key) {
  if (_localeCode == 'en') {
    if (key == 'key1') return 'English translation for key1';
    else if (key == 'key2') return 'English translation for key2';
  } else if (_localeCode == 'es') {
    if (key == 'key1') return 'Spanish translation for key1';
    else if (key == 'key2') return 'Spanish translation for key2';
  }
}

And you'd use it:

void main() {
  var translatedString = translate('key1');
}

For simplicity I hardcoded the things, but a better solution is to load the keys through a json file, here's a better approach