Add optional culture param to indexer of IStringLocalizer
See original GitHub issueSummary
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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:17
- Comments:10 (6 by maintainers)
@odinnou basically this https://gist.github.com/vaclavholusa-LTD/2a27d0bb0af5c07589cffbf1c2fff4f4 just replace “Ltd” with your custom name 😃 Had to inherit some built-in classes/interfaces since it doesn’t exist in the original IStringLocalizer… and naturally you need to pass ILtdStringLocalizer<T> as your dependency
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.