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

Avoid users to create new instances of the LocalizationResourceManager #770

Merged
merged 7 commits into from
Jan 20, 2021

Conversation

pictos
Copy link
Contributor

@pictos pictos commented Jan 16, 2021

Description of Change

Just marked the public constructor from LocalizationResourceManager (LRM) as obsolete and ask the users to access the instance using the Current property.

Bugs Fixed

API Changes

Added:

  • [Obsolete] public LocalizationResourceManager();

Behavioral Changes

Users can create new instances of LRM but this is marked as obsolete

PR Checklist

  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

@pictos pictos added bug Something isn't working. Breaky break. a/i18n 🌐 labels Jan 16, 2021
@pictos pictos self-assigned this Jan 16, 2021
@XamarinCommunityToolkitBot

A new build for this PR has been completed. As a result a NuGet has been created which allows you to verify this fix or try out the new feature! Download here

@pictos
Copy link
Contributor Author

pictos commented Jan 17, 2021

Also, we can minimize the breaks by adding this workaround

               public static LocalizationResourceManager Current { get; } = new LocalizationResourceManager(null);
		
               LocalizationResourceManager(object obj)
		{
		}
		
                [Obsolete("Please use the Current property instead of create a new instance of this class")]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public LocalizationResourceManager()
		{
		}

The good of the break is that we will force all devs to use it in the right way... The bad part is the break.
Thoughts?

@AndreiMisiukevich
Copy link
Contributor

AndreiMisiukevich commented Jan 17, 2021

Or just

#pragma warning disable
public static LocalizationResourceManager Current { get; } = new LocalizationResourceManager();	
#pragma warning restore

[Obsolete("Please use the Current property instead of create a new instance of this class")]
[EditorBrowsable(EditorBrowsableState.Never)]
public LocalizationResourceManager()
{
}

@AndreiMisiukevich
Copy link
Contributor

@pictos but basically do you think that many people already use this class?
Maybe we can just message all of them )

@pictos
Copy link
Contributor Author

pictos commented Jan 17, 2021

but basically do you think that many people already use this class?

@AndreiMisiukevich I have no idea, but we have more than 4k users, so if they need i18n on their apps, they can rely on our implementation and miss use it. If you look at this PR's issue, you can see that devs can miss understand it and create new instances per resx file. Or even wrap up this in a DI and not use the singleton for it. Here I'm trying to fix the API and protect the devs from themselves.

Yeah, your suggestion is better than mine, I'll push it.

@XamarinCommunityToolkitBot

A new build for this PR has been completed. As a result a NuGet has been created which allows you to verify this fix or try out the new feature! Download here

@jfversluis
Copy link
Member

jfversluis commented Jan 19, 2021

So just to be clear, this doesn't really break the users, right? This just obsoletes it and directs them to use something else?

Then can you make the actual breaking change and PR that into develop @pictos ? can't do that yet, because when we merge main to develop then we're back with this change again... Let's at least make an issue so we won't forget. while we figure out how to go about this the best way

jfversluis
jfversluis previously approved these changes Jan 19, 2021
…tionResourceManager.shared.cs

Co-authored-by: Gerald Versluis <[email protected]>
@jfversluis
Copy link
Member

@pictos could you update the description in the first comment to reflect this change? You're now talking about the private constructor which is not accurate anymore.

Then merge away :)

@jfversluis jfversluis added this to the v1.0.2 milestone Jan 19, 2021
@jfversluis jfversluis added the ready-to-merge Review completed, Ready for API review and merge label Jan 19, 2021
@maxkoshevoi
Copy link
Contributor

maxkoshevoi commented Jan 19, 2021

@pictos, @AndreiMisiukevich, @jfversluis Just to be clear, what should we do when user has multiple resource files? Like described in the issue linked to this PR. Do we just not support that scenario?

Currently Translate extension won't work with multiple LocalizationResourceManager instances (we can add another parameter to it though), but LocalizedString will (thanks to second constructor that accepts LocalizationResourceManager instance).

@jfversluis
Copy link
Member

@maxkoshevoi do you know how that works in "normal" .NET/C# projects?

@maxkoshevoi
Copy link
Contributor

maxkoshevoi commented Jan 19, 2021

@jfversluis in normal project if you have several resource files, you can reference each of them by name, and get localized resource strings from them:
image

So normal projects allow for multiple resource files no problem. By restricting creation of LocalizationResourceManager instances we are saying that user mast have no more than one resource file (with localized variations of it) to use our on-the-fly culture change.

@AmrAlSayed0
Copy link
Contributor

@jfversluis There was a discussion here #769 (comment) you can see what I mean. BTW, I'm generating my AppResource.Designer.cs files with the PublicResXFileCodeGenerator instead of ResXFileCodeGenerator so that it is public and can be used from other assemblies/projects that reference it.

@pictos
Copy link
Contributor Author

pictos commented Jan 19, 2021

@AmrAlSayed0 are you able to test your project with the nugget generated from this PR?

@pictos pictos requested a review from jfversluis January 19, 2021 22:42
@AmrAlSayed0
Copy link
Contributor

@pictos No, I didn't see a way how I would be able to unless TranslateExtension is modified to take an instance of LocalizationResourceManager as a parameter or something (Maybe, optionally, in which case use .Current)

And by the way, I should mention that I'm bringing this up because that is MY use case. I'm in no way blocking this PR. If you feel that the LocalizationResourceManager and TranslateExtenstion are ok as they are now and you got approved, go ahead and merge it. I just won't be able to use it. I already wrote something similar in my own app and I use XCT in my app so I thought I could just give up my poorly written implementation and use XCT's but this PR won't affect me very much, so feel free to do whatever.

@pictos
Copy link
Contributor Author

pictos commented Jan 20, 2021

@AmrAlSayed0 if you have time to test it will be great because I can try to make it work on your scenario. I don't want to break any current user. If you are not able to test there is no problem I can try to implement something similar to test it.
Even if this PR gets merged you will be able to create a new instance, I just marked that constructor as obsolete.

@maxkoshevoi
Copy link
Contributor

@pictos If TranslateExtension had optional LocalizationResourceManager parameter, we would be able to fully support multiple resource files. What do you think about adding it?

@pictos
Copy link
Contributor Author

pictos commented Jan 20, 2021

@maxkoshevoi that could be a thing, I need to try out that first. The downside is that will add more complexity to the lib.

@maxkoshevoi
Copy link
Contributor

@pictos One optional parameter for markup extension that only has two parameters shouldn't complicate things much, but it will allow people with more than one resource file to use our culture changing flow.
Also LocalizationResourceManager constructor shouldn't be obsolete in this case.

@jfversluis jfversluis merged commit ba74e67 into main Jan 20, 2021
@jfversluis jfversluis deleted the pj/fixed-localization-manager branch January 20, 2021 19:51
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/i18n 🌐 api-change 🚨 bug Something isn't working. Breaky break. ready-to-merge Review completed, Ready for API review and merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug] We can create new instances of the LocalizationManager
7 participants