unoplatform / uno

Build Mobile, Desktop and WebAssembly apps with C# and XAML. Today. Open source and professionally supported.
https://platform.uno
Apache License 2.0
8.5k stars 690 forks source link

Implement `ResourceLoader`/`ResourceManager`/`ResourceMap` to align with WinAppSdk #11899

Open jeromelaban opened 1 year ago

jeromelaban commented 1 year ago

Discussed in https://github.com/unoplatform/uno/discussions/11867

Originally posted by **nickrandolph** March 31, 2023 With the new class library structure it's somewhat difficult to load resources in code. Uno.Extensions provides a [wrapper](https://github.com/unoplatform/uno.extensions/blob/main/src/Uno.Extensions.Localization.UI/ResourceLoaderStringLocalizer.cs) for this but without this accessing resources from the class library requires two branches of code, one for WinAppSdk that uses ResourceMap and one that uses and overloaded ResourceLoader constructor that takes the name of the class library. The idea would be to provide an implementation of ResourceManager/ResourceMap in Uno that aligns with the WinAppSdk Source issue: https://github.com/unoplatform/uno/issues/11786

Workaround

When using Uno.Extensions, using ResourceLoaderStringLocalizer abstracts the difference.

When not using Uno.Extensions, Windows.ApplicationModel.Resources.ResourceLoader cannot be used under WinAppSDK, Microsoft.Windows.ApplicationModel.Resources.ResourceLoader must be used instead:

#if HAS_UNO
        var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
#else
        var resourceLoader = new Microsoft.Windows.ApplicationModel.Resources.ResourceLoader();
#endif

var myResource = resourceLoader.GetString("ApplicationName");
MartinZikmund commented 1 year ago

I intend to do this in https://github.com/unoplatform/uno/pull/10948