Easy_localization: is there a way to generate the json string key in a dart class so as not to make typing errors?

Created on 31 Mar 2020  路  6Comments  路  Source: aissat/easy_localization

Do you know any way to do this?

For example
en.json

{
    "title": "Hello",
    "home.msg": "Hello {} in the {} world ",
    ...
}

l10n_keys.dart

class LocalizationKeys{
   static const String titile = 'title';
   static const String home_msg = 'home.msg';
}
enhancement

Most helpful comment

Hi @basketball-ico and @aissat ,
I'm using easy_localization in some Flutter apps, and I would like to have that useful abstract class.
So I'm started to working on a build_runner task to do exactly this job, and I release the first version on yesterday.
The package is translations_keychain.

With that tool you could use the translations label with:

Text(TranslationsKeychain.title.tr());

I hope it could helps. :)

--Luca

All 6 comments

@basketball-ico currently no

Hi @basketball-ico and @aissat ,
I'm using easy_localization in some Flutter apps, and I would like to have that useful abstract class.
So I'm started to working on a build_runner task to do exactly this job, and I release the first version on yesterday.
The package is translations_keychain.

With that tool you could use the translations label with:

Text(TranslationsKeychain.title.tr());

I hope it could helps. :)

--Luca

@lcaprini Thanks! we already release code generation for json/csv, please look develop branch. If you have any suggestions, this will help us.

@Overman775 Cool! I saw the code generation, but I think it is a different thing.

My generator is only for the keys, it doesn't consider the translations: it generates a simple abstract class with all the keys of the translation files as property, so you could use it in order to have IDE suggestions instead to write manually.

In example, instead to write Text('HOME'.tr()) you could use Text(TranslationsKeychain.HOME.tr());.

I used your json generator, and I think is useful, but I don't find a way that helps the IDE to suggest me the key. Maybe I missed something?

@lcaprini Now I understand, thanks for the good idea 馃憤

@basketball-ico @lcaprini

dependencies:
  easy_localization: ^2.2.0-dev

example:

{
  "msg_named": "{} 賲賰鬲賵亘丞 亘丕賱賱睾丞 {lang}",
}
flutter pub run easy_localization:generate  -f keys -o locale_keys.g.dart

```
abstract class LocaleKeys {
static const msg_named = 'msg_named';
}

```dart
 Text(LocaleKeys.msg_named).tr(namedArgs: {'lang': 'Dart'}, args: ['Easy localization']),
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gjrjf picture gjrjf  路  4Comments

Xgamefactory picture Xgamefactory  路  4Comments

Wilson13 picture Wilson13  路  7Comments

necipk picture necipk  路  5Comments

Overman775 picture Overman775  路  6Comments