Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Deprecate LocalizationResourceManager.SetCulture #766

Merged
merged 10 commits into from
Jan 29, 2021
2 changes: 1 addition & 1 deletion samples/XCT.Sample/ViewModels/SettingViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Language SelectedLanguage

public ICommand ChangeLanguageCommand => changeLanguageCommand ??= new Command(() =>
{
LocalizationResourceManager.Current.SetCulture(CultureInfo.GetCultureInfo(SelectedLanguage.CI));
LocalizationResourceManager.Current.CurrentCulture = CultureInfo.GetCultureInfo(SelectedLanguage.CI);
LoadLanguages();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void LocalizationResourceManager_GetCulture_Equal_Indexer()
var responceIndexerCulture1 = localizationManager[testString];
var responceGetValueCulture1 = localizationManager.GetValue(testString);
var responceResourceManagerCulture1 = resourceManager.GetString(testString, initialCulture);
localizationManager.SetCulture(culture2);
localizationManager.CurrentCulture = culture2;
var responceIndexerCulture2 = localizationManager[testString];
var responceGetValueCulture2 = localizationManager.GetValue(testString);
var responceResourceManagerCulture2 = resourceManager.GetString(testString, culture2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
using System.Globalization;
using System.Resources;
using System.Threading;
using Xamarin.CommunityToolkit.ObjectModel;

namespace Xamarin.CommunityToolkit.Helpers
{
#if !NETSTANDARD1_0
public class LocalizationResourceManager : INotifyPropertyChanged
public class LocalizationResourceManager : ObservableObject
{
public static LocalizationResourceManager Current { get; } = new LocalizationResourceManager();

Expand All @@ -29,18 +30,15 @@ public string GetValue(string text) =>
public string this[string text] =>
GetValue(text);

public void SetCulture(CultureInfo language)
[Obsolete("Use " + nameof(CurrentCulture) + " to set culture")]
[EditorBrowsable(EditorBrowsableState.Never)]
public void SetCulture(CultureInfo language) => CurrentCulture = language;

public CultureInfo CurrentCulture
{
currentCulture = language;
Invalidate();
get => currentCulture;
set => SetProperty(ref currentCulture, value, null);
}

public CultureInfo CurrentCulture => currentCulture;

public event PropertyChangedEventHandler PropertyChanged;

public void Invalidate() =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(null));
Comment on lines -50 to -51
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfversluis can we just remove it? Or we should mark it as obsolete and editorbrowsablestate.never

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method doesn't make sense anymore. Culture is stored in the private field, and when we change it, we always call PropertyChanged ourselves. So public Invalidate method will just needlessly send an event.

It was needed earlier since culture was stored in Thread.CurrentThread.CurrentUICulture that could be changed outside of LocalizationResourceManager

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you're saying, I think it would make sense to first release a version where it's deprecated though. That way people will get a heads up that it is going away and gives them the opportunity to change their code at their pace instead of just dropping a breaking change over the fence that they have to deal with without any notice upfront.

Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, returned the method. When are we planning to remove everything that is marked as Obsolete?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say 1 version obsolete, next version it's gone :)

}
#endif
}