easy_localization icon indicating copy to clipboard operation
easy_localization copied to clipboard

Static typing for AssetLoader

Open alexeyinkin opened this issue 1 year ago • 8 comments

Currently EasyLocalization.assetLoader is dynamic. It defaults to RootBundleAssetLoader that extends a local AssetLoader class.

easy_localization_loader has its own abstract AssetLoader, and its loaders extend it.

The following change will improve type safety:

  1. Limit EasyLocalization.assetLoader to the local AssetLoader class.
  2. Remove AssetLoader duplicate from easy_localization_loader.
  3. In easy_localization_loader, depend on easy_localization and make all loaders extend its AssetLoader.

alexeyinkin avatar Sep 13 '22 03:09 alexeyinkin

Thanks for submitting this and explaining a solution! Maybe you want to submit a PR which implements that?

bw-flagship avatar May 13 '23 06:05 bw-flagship

@bw-flagship done.

The plan is:

  1. Merge https://github.com/aissat/easy_localization_loader/pull/50 and publish v2.0.0.
  2. Merge https://github.com/aissat/easy_localization/pull/598

Note that for both packages this is a breaking change, so the bump to 4.0.0 is required. You may want to hold back publishing until you do more breaking changes and cleanup that were coming.

alexeyinkin avatar Jun 26 '23 10:06 alexeyinkin

Why did you change the file names? It is not necessary for deduplicating the AssetLoader, isn't it?

bw-flagship avatar Jun 26 '23 16:06 bw-flagship

I did not change them but sorted alphabetically. There is a lint for this: https://dart-lang.github.io/linter/lints/directives_ordering.html Same goes for removing the library directive: https://dart.dev/effective-dart/style#dont-explicitly-name-libraries

I just did a quick scan of what I can easily fix. I can undo that if you want.

alexeyinkin avatar Jun 27 '23 07:06 alexeyinkin

I meant this breaking change:

- **BREAKING**: `JsonAssetLoader`, `XmlAssetLoader`, and `YamlAssetLoader` now use `_` instead of `-` when converting a locale to a file name.

Why is this change necessary?

bw-flagship avatar Jun 27 '23 08:06 bw-flagship

These loaders were using localeToString function to convert from a Locale to the file name component: https://github.com/aissat/easy_localization_loader/blob/0469058b11f65b92c4fdc7a2cbbf1900401df0d6/lib/src/yaml_asset_loader.dart#L12

This function was defined in both easy_localization_loaders and easy_localization, and is deprecated in the latter. The reason for deprecation is not mentioned, but my guess was that the author wanted to encourage more consistent naming of files, like en_US.yaml instead of en-US.yaml, since _ is the standard in Dart.

The straightforward way to keep the old behavior was to use the deprecated function from easy_localization, which I did not like. Following my guess of the purpose of the deprecation, I renamed the files.

When you asked, I discovered that Locale.toStringWithSeparator was introduced instead. So if we want to preserve the behavior, I can switch to using it.

Still the _ is more straightforward separator, and we can switch to defaulting to it at some point later.

alexeyinkin avatar Jun 27 '23 08:06 alexeyinkin

Thanks for explanation. I would prefer to preserve the old behavior to reduce breaking changes to a minimum :)

bw-flagship avatar Jun 27 '23 12:06 bw-flagship

Done.

alexeyinkin avatar Jun 27 '23 12:06 alexeyinkin