dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

[Proposal] LocalizationResourceManager

Open brminnick opened this issue 3 years ago • 24 comments

LocalizationResourceManager

  • [x] Proposed
  • [x] Prototype: Not Started
  • [ ] Implementation: Not Started
    • [ ] iOS Support
    • [ ] Android Support
    • [ ] macOS Support
    • [ ] Windows Support
  • [ ] Unit Tests: Not Started
  • [ ] Sample: Not Started
  • [ ] Documentation: Not Started

Summary

The LocalizationResourceManager class is a helper class that enables users to respond to culture changes at runtime.

Detailed Design

LocalizationResourceManager.shared.cs

public class LocalizationResourceManager : INotifyPropertyChanged
{
  public static LocalizationResourceManager Current;

  private LocalizationResourceManager();

  public void Init(ResourceManager resource);
  public void Init(ResourceManager resource, CultureInfo initialCulture);
  public object GetValue(string resourceKey);

  public object this[string resourceKey] => GetValue(resourceKey);

  public CultureInfo CurrentCulture { get; set; }
}

Usage Syntax

XAML Usage

N/A

C# Usage

LocalizationResourceManager.Current.PropertyChanged += (_, _) => AppResources.Culture = LocalizationResourceManager.Current.CurrentCulture;
LocalizationResourceManager.Current.Init(AppResources.ResourceManager);

LocalizationResourceManager.Current.CurrentCulture = newCulture;

brminnick avatar Sep 26 '21 23:09 brminnick

Closing for the following reasons:

  • Duplication of RESX
  • Required use of ObservableObject
    • ObservableObject is not implemented in CommunityToolkit.Maui
    • ObservableObject is implemented in CommunityToolkit.MVVM

brminnick avatar Sep 26 '21 23:09 brminnick

Reopening based on our discussion during the June Standup: https://www.youtube.com/watch?v=t3g_NrQfE8g

brminnick avatar Jun 02 '22 20:06 brminnick

Why I suggest to keep it in MCT?

  1. It works great with TranslateExtension, which depends on MAUI. So if we decide to add TranslateExtension we will need to depend on Community Toolkit dotnet.
  2. Based on the first point. Splitting Localization classes on different packages maybe not a great customer experience
  3. Minor but it will be easier for XCT users migrate to MCT
  4. As for me localization resources are mostly related to UI (like button text or user error messages - not logs), so keep it somewhere in Core library may not give a lot of benefit. User may store Resx files wherever he wants. LocalizationResourceManager only needs ResourceManager reference.

VladislavAntonyuk avatar Jun 06 '22 20:06 VladislavAntonyuk

I agree that we should probably offer all or nothing rather than take on a dependency. I am trying to think on whether there could be a compromise but nothing is coming to mind

bijington avatar Jun 06 '22 20:06 bijington

It can work as a standalone feature but as I described in the article LocalizationResourceManager and TranslateExtension work better together: https://vladislavantonyuk.azurewebsites.net/articles/Localize-.NET-MAUI-application

VladislavAntonyuk avatar Jun 06 '22 21:06 VladislavAntonyuk

For me, we should implement this in a more Core lib. in other to add value across other .NET UI frameworks (and maybe in non UI frameworks as well). I'll express that by commenting on the points that @VladislavAntonyuk mentioned.

So from 1 and 2

  1. It works great with TranslateExtension, which depends on MAUI. So if we decide to add TranslateExtension we will need to depend on Community Toolkit dotnet.
  2. Based on the first point. Splitting Localization classes on different packages maybe not a great customer experience

Now that CommunityToolkits are all in the same org, we should start thinking about ways to add features to all of them. Like the CommunityToolkit.Maui.Core will be used by other Toolkits that you will target other MAUI flavors, we can have a dependency on the CommunityToolkit.Common if that makes sense. We can ship the CommunityToolkit.Common with our NuGet, so devs don't need to install it by themself.

  1. Minor but it will be easier for XCT users migrate to MCT

Can't see why will be hard to migrate from XCT to MCT if this feature is on CommunityToolkit.Common. Repeating my self, we can ship that package with our NuGet.

  1. As for me localization resources are mostly related to UI (like button text or user error messages - not logs), so keep it somewhere in Core library may not give a lot of benefit. User may store Resx files wherever he wants. LocalizationResourceManager only needs ResourceManager reference.

You can use resx files in ConsoleApps (maybe you're developing a dotnet tool and want to support multiple languages), and also can be used in other .NET areas, that include UI, like ASP.NET, WPF, UWP, etc. I mean, MAUI isn't the only UI framework for .NET so having this feature in a Core library will help other toolkits, applications and libs to create and manage their localization resources like we want to do here

pictos avatar Jun 06 '22 22:06 pictos

Because this Proposal is not specific to .NET MAUI, my vote is to implement this feature in the CommunityToolkit/dotnet repo so that additional devs can benefit from it.

Hey @sergio0694! Do agree that this feature is best scoped for CommunityToolkit.Common?

brminnick avatar Jun 07 '22 15:06 brminnick

Leaving an update here as @brminnick asked - right now the 8.0 release is feature complete and I'm just working on stabilizing stuff and fixing last minute bugs. I'm happy to look into this once that is out though, for sure 🙂

Sergio0694 avatar Jun 13 '22 20:06 Sergio0694

if this library takes this forward, I hope we can not have the public static LocalizationResourceManager Current; property. With the new structure of Maui and dependency injection, there should be less of a need to add static properties like this.

powerdude avatar Jun 13 '22 23:06 powerdude

Any updates on this? This has been marked as in-progress champion for three months now, I think

maxkoshevoi avatar Sep 17 '22 21:09 maxkoshevoi

Those labels were just leftovers from when the issue was moved to this repo, but they have no meaning here. I've removed them 🙂

Sergio0694 avatar Sep 17 '22 21:09 Sergio0694

Hi @Sergio0694 , any updates about this issue?

VladislavAntonyuk avatar Oct 28 '22 07:10 VladislavAntonyuk

Would love to see this soon.

jrahma avatar Jul 29 '23 21:07 jrahma

Hi, i tried to migrate from XF to MAUI and i have 111 references to the LocalizedString class im my project. Also 71 references to the Translate extensions in xaml. What is now the prefered way to migrate my code to MAUI? It is not better to keep this functionality in the MAUI Toolkit and remove it later once a better place is found?!

Thanks

AlleSchonWeg avatar Oct 16 '23 10:10 AlleSchonWeg

Until a final solution is made for this, have I created a replacement library for .NET MAUI. :smiley: https://github.com/SirJohnK/LocalizationResourceManager.Maui

SirJohnK avatar Nov 02 '23 13:11 SirJohnK