-
Notifications
You must be signed in to change notification settings - Fork 9.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional culture param to indexer of IStringLocalizer #28047
Comments
@vaclavholusa-LTD thanks for contacting us. @DamianEdwards do you have any thoughts? |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
@vaclavholusa-LTD can you transmit a gist of your ResourceManagerStringLocalizer implementation ? Currently the remove of WithCulture if the only one thing, that is blocking us to migrate to 5.0 thanks in advance |
@odinnou basically this |
We used to have this and it was removed: #3324 The reasons cited were basically that it didn't belong on the interface as it was confusing to imply that all implementations had to support a specific culture or that any instance was culture-specific. Given that history I'm inclined to suggest we investigate a different approach to solving this, perhaps with a new interface that itself derives from |
I would assume the expectation of any programmer is, that you can specify a specific culture when retrieving a resource. Fallback to thread culture is fine. But having to change thread culture to achieve this effect is quite sad. |
Hi, we made a radical choice. |
That is radical :). For me the framework works in most cases. (But as well i am not too happy with the organisation of resx files). There are just a handful of cases where i need to specify an explicit culture. I like the suggestion of @vaclavholusa-LTD from a user perspective.
The user object specifies the desired culture, others may prefer to pass the culture info. |
I personally would say something like this would be really useful to be able to explicitly state a culture for certain applications, but the suggested overloads with the LocalizedString this[CultureInfo culture, string name] { get; }
LocalizedString this[CultureInfo culture, string name, params object[] arguments] { get; } |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Just following up on this. |
I would honestly like to see concrete metrics regarding that. I have an easier time believing in confusion over why a piece of code has to temporarily switch thread culture to fetch resources from a different culture. E.g. to fit translated links for other languages, as has been cited as a real-world usecase in this issue. There was nothing wrong with Actual (bad)
Expected (good)
And possibly rename the method to |
wouldn't it make more sense with the api to actually use keyed services with IStringLocalizer to get a specific locale? like:
|
@schmitch Moreover, you might not even know all the languages to be supported statically upfront. |
I might disagree with bringing this API back, fortunately, you remind me of Please let me know if you have any further questions or if we need to close this |
The only thing that does is paper over things. ... it still doesn't resolve the cognitive disconnect of having to switch cultures in the first place. How would you resolve something that needs to combine multiple resources from multiple languages in a single expression? Compute out everything beforehand? Any idea how ugly that becomes? string en, de, fr, es;
using (CultureScope.Create("en"))
{
en = localizer["some.resource.name"];
}
using (CultureScope.Create("de"))
{
de = localizer["some.resource.name"];
}
using (CultureScope.Create("fr"))
{
fr = localizer["some.resource.name"];
}
using (CultureScope.Create("es"))
{
es = localizer["some.resource.name"];
}
return FormatCombinedSomehow(en, de, fr, es); Compare that to: return FormatCombinedSomehow(
localizer.WithCulture("en")["some.resource.name"],
localizer.WithCulture("de")["some.resource.name"],
localizer.WithCulture("fr")["some.resource.name"],
localizer.WithCulture("es")["some.resource.name"]
); Humorously, the proposed alternative that uses a return FormatCombinedSomehow(
localizer[CultureInfo.GetCultureInfo("en"), "some.resource.name"],
localizer[CultureInfo.GetCultureInfo("de"), "some.resource.name"],
localizer[CultureInfo.GetCultureInfo("fr"), "some.resource.name"],
localizer[CultureInfo.GetCultureInfo("es"), "some.resource.name"],
); |
@rjgotten could you please elaborate on what are you trying to do with the above example? |
@hishamco It papers over the details of the overhead and lifts it to a higher level of abstraction, but doesn't resolve it anywhere near as elegantly as the removed |
I didn't mention this above as a solution in your case, that is why I told you in my last comment what are you trying to do, so I can help |
Summary
Now when
WithCulture()
is gone there is no simple method to get localized resource from different culture without changing the application globalCurrentUICulture
.Motivation and goals
In my ASP.NET Core App i I'm using translated URLs (stored in resource files). On every page I need to create "Alternate Links" tags that lead to the similar page with a translation. For this I'm iterating through supported cultures and getting translated link for every alternate lang page.
In scope
Since
ResourceManagerStringLocalizer
internally uses ResourceManger'sGetString()
method that already supports specifying culture, we should add the option to specify such culture instead of relying on global CurrentUICulture.Out of scope
none
Risks / unknowns
ResourceManagerStringLocalizer.GetStringSafely()
already accepts specific culture so this amendment should be safe to use.Examples
I implemented this feature by creating my own inherited
ResourceManagerStringLocalizer
that adds such method.Extra methods should be added to
IStringLocalizer
interfaceand example implementation of the first one in
ResourceManagerStringLocalizer
(similar approach would apply to the second method)I can submit PR if you are happy with the suggestions.
The text was updated successfully, but these errors were encountered: