dotnet
dotnet copied to clipboard
[Proposal] LocalizationResourceManager
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;
Closing for the following reasons:
- Duplication of RESX
- Required use of
ObservableObject
-
ObservableObject
is not implemented inCommunityToolkit.Maui
-
ObservableObject
is implemented inCommunityToolkit.MVVM
-
Reopening based on our discussion during the June Standup: https://www.youtube.com/watch?v=t3g_NrQfE8g
Why I suggest to keep it in MCT?
- 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.
- Based on the first point. Splitting Localization classes on different packages maybe not a great customer experience
- Minor but it will be easier for XCT users migrate to MCT
- 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.
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
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
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
- 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.
- 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.
- 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.
- 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
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
?
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 🙂
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.
Any updates on this? This has been marked as in-progress champion
for three months now, I think
Those labels were just leftovers from when the issue was moved to this repo, but they have no meaning here. I've removed them 🙂
Hi @Sergio0694 , any updates about this issue?
Would love to see this soon.
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
Until a final solution is made for this, have I created a replacement library for .NET MAUI. :smiley: https://github.com/SirJohnK/LocalizationResourceManager.Maui