Add optional culture param to indexer of IStringLocalizer
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
## Summary
Now when `WithCulture()` is gone there is no simple method to get localized resource from different culture without changing the application global `CurrentUICulture`.
## 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's `GetString()` 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` interface
```
LocalizedString this[string name, CultureInfo culture] { get; }
LocalizedString this[string name, CultureInfo culture, params object[] arguments] { get; }
```
and example implementation of the first one in `ResourceManagerStringLocalizer` (similar approach would apply to the second method)
```
public virtual LocalizedString this[string name, CultureInfo culture]
{
get
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
var value = GetStringSafely(name, culture);
return new LocalizedString(name, value ?? name, resourceNotFound: value == null, searchedLocation: _resourceBaseName);
}
}
```
I can submit PR if you are happy with the suggestions.
Contributor guide
Assessment
This issue has not been assessed yet.