easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

Use difference source for fallback translations when implement custom assetLoader

Open Chaloemphisit opened this issue 1 year ago • 3 comments

Use difference source for fallback translations.

Issue

  • Currently, if I implement the custom assetLoader to fetch the translation asset from the internet, if the fallback is called in case the localization key is not found, I want it's to use to fallback translation from local asset instead of network.

Question

  • Do you have any suggestions or is it possible to request a new feature to support it?

Chaloemphisit avatar Feb 05 '24 03:02 Chaloemphisit

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.

black-rusuz avatar Feb 13 '24 14:02 black-rusuz

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

image

Chaloemphisit avatar Feb 27 '24 04:02 Chaloemphisit

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

black-rusuz avatar Feb 27 '24 14:02 black-rusuz