flutter_gen icon indicating copy to clipboard operation
flutter_gen copied to clipboard

i18n/l10n for assets? Or, allow one class to implement another class?

Open fzyzcjy opened this issue 3 years ago • 0 comments

Hi thanks for this wonderful library! I have different files for different languages, for example, zh/one.png, en/one.png, zh/two.png, en/two.png, .... Then I hope to access the correct variant based on the current language. For example, if current language is en, I hope to get en/one.png when reading assets.one.

This may be possible by letting class $AssetsEn implements $AssetsZh (probably by manual configuration in yaml). Full example:

// auto generated

class $AssetsZh {
  String get one => 'zh/one.png';  String get two => 'zh/two.png';
}

class $AssetsEn implements $AssetsZh /* THIS implements is the ONLY thing that flutter_gen needs to add*/ {
  String get one => 'en/one.png';  String get two => 'en/two.png';
}

// manually written

$AssetsZh getAssetsForCurrentLanguage(String language) {
 switch(language){
  case 'zh': return $AssetsZh();
  case 'en': return $AssetsEn();
 }
}

void main() {
 var assets = getAssetsForCurrentLangauge('zh');
 make_use_of(assets.one); // it is zh/one.png now
}

fzyzcjy avatar Jul 04 '22 11:07 fzyzcjy