question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add optional culture param to indexer of IStringLocalizer

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:17
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
vaclavholusa-LTDcommented, Dec 2, 2020

@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

0reactions
msftbot[bot]commented, Oct 21, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Changing the language of an IStringLocalizer
This should do it. You need to set CultureInfo.CurrentUICulture to the culture you want before getting the string value.
Read more >
Globalization and localization in ASP.NET Core
Learn how ASP.NET Core provides services and middleware for localizing content into different languages and cultures.
Read more >
A Deep Dive into ASP.NET Core Localization
Implement a strategy that can be used to select a language or a culture per request. ... First off, you should add localization...
Read more >
ASP.NET Core Localization Deep Dive - Joonas W's blog
Now try adding ?culture=fi-FI to the end of the URL in your browser, and see what happens. ... Return the parameter for the...
Read more >
Globalization and localization - ASP.NET Core Documentation
IStringLocalizer uses the ResourceManager and ResourceReader to provide culture-specific resources at run time. The simple interface has an indexer and an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found