aissat / easy_localization

Easy and Fast internationalizing your Flutter Apps
https://pub.dev/packages/easy_localization
MIT License
908 stars 325 forks source link

Use difference source for fallback translations when implement custom assetLoader #643

Open Chaloemphisit opened 8 months ago

Chaloemphisit commented 8 months ago

Use difference source for fallback translations.

Issue

Question

black-rusuz commented 7 months ago

That would be very useful. My white-label app needs to override some stings on server-side, but others can be used with the defaults from the RootBundle.

@Chaloemphisit Here's my draft, you may use it:

class CombinedAssetLoader extends SmartNetworkAssetLoader {
  final AssetLoader parent;

  CombinedAssetLoader({
    required this.parent,
    required super.localeUrl,
    super.timeout = const Duration(seconds: 30),
    required super.assetsPath,
    super.localCacheDuration = const Duration(days: 1),
  });

  @override
  Future<Map<String, dynamic>> load(String localePath, Locale locale) async {
    final smartData = await super.load(localePath, locale);
    final parentData = await parent.load(localePath, locale);
    parentData?.addAll(smartData);
    return parentData ?? {};
  }
}

I could rewrite this and open a Pull Request on the easy_localization_loader repository. Maybe create kinda class CombinedAssetLoader extending AssetLoader, that requires arguments with base loader and override loader, and creates merged Map with localized strings.

Chaloemphisit commented 7 months ago

Sorry, but i think this logic will not work for nested map.

image
black-rusuz commented 7 months ago

Hmmm, that's true. I did this for my project and didn't need it :) You need to add something like "deep merge": https://gist.github.com/niusounds/30ba030c084ccd11faaab0bf2ecd81cf