aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Accessing resource strings from external assembly always returns en-US strings instead of string based on thread's current culture.

Open MatLeger opened this issue 10 years ago • 7 comments

Title

Accessing resource strings from external assembly returns en-US strings instead of string based on thread's current culture.

Functional impact

When processing each request, all resource strings are returned as English even though the current thread will have a different culture set. For example the code below will always return "Hello World" even though the culture may be fr-FR.

CultureInfo culture = Thread.CurrentThread.CurrentCulture;
_logger.LogTrace($"Current Culture: {culture.Name}");

string message = SharedClassLibrary.Resources.Resource.HelloWorld;
_logger.LogTrace($"Localized message: {message}");

Minimal repro steps

I've attached a zipped solution that contains an ASP.NET Core web app with MVC, a console application, and a class library that contains the shared resources for both the console application and the web app.

  1. Build the solution
  2. Run the Console Application and verify that it will iterate through the supported cultures and print out "Hello World" in that culture.
  3. Run the Web App and land on the home page. You should see "Hello World" in the culture specified in the request by the browser or the culture specified in the query string. Instead you will always see "Hello World". There are links on the home page that allow the user to reload the page in one of the supported languages by adding it to the query string. They will all show "Hello World" even though it is clear that the current culture info has changed on the executing thread.
  4. Bonus bug - note that intellisense is not working for the shared class library in the web app, but it does work in the Console Application.

I'm using ASP.NET Core 1.0 with Visual Studio 2015 Update 3 on Windows Server 2012 R2 Standard. LocalizationWebApp.zip

Expected result

The Web App should get the correct localized resource string just like the console app does.

Actual result

The Web App always gets the English version regardless of the current thread's culture info.

MatLeger avatar Aug 09 '16 17:08 MatLeger

Dup of https://github.com/aspnet/Mvc/issues/4692. See the workaround here - https://github.com/aspnet/Mvc/issues/4692#issuecomment-223671462. Additionally, this only works if you're targeting net46 or higher so you'd also have to change the framework your app currently targets.

pranavkm avatar Aug 09 '16 17:08 pranavkm

I changed the sample to target net461 and I get the same problem. LocalizationWebApp (2).zip

In my case the view is getting the localized string from ViewData["Message"] which is set in the controller. That's the code that is not getting the correct localized string resource even though the thread's culture is set correctly. Here's the controller code:

public class HomeController : Controller
{
    private ILogger<HomeController> _logger = null;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

    public IActionResult Index()
    {
        CultureInfo culture = Thread.CurrentThread.CurrentCulture;
        _logger.LogTrace($"Current Culture: {culture.Name}");

        // The culture var may be fr-FR or es-ES but message is always set to "Hello World"
        string message = SharedClassLibrary.Resources.Resource.HelloWorld;
        _logger.LogTrace($"Localized message: {message}");

        ViewData["Message"] = message;
        ViewBag.SupportedCultures = SharedClassLibrary.SharedExample.SupportedCultures;

        return View();
    }
}

Here's the view:

@using System.Threading
@using System.Globalization
@{
    ViewData["Title"] = "Home Page";
    CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
    CultureInfo[] supportedCultures = (CultureInfo[])ViewBag.SupportedCultures;
}

<h1>@ViewData["Message"]</h1>
<hr />
<strong>Current Culture Info</strong>
<ul>
    <li>@currentCulture.NativeName</li>
    <li>@currentCulture.Name</li>
</ul>
<br/>
<strong>Supported Cultures</strong>
<ul>
    @{
        foreach (CultureInfo culture in supportedCultures)
        {
            <li><a asp-area="" asp-controller="Home" asp-action="Index" asp-route-culture="@culture.Name">Home (@culture.Name)</a></li>
        }
    }
</ul>

The workaround you point to doesn't seem to apply in this case because the thread's culture is already correctly set in the middleware by the QueryStringRequestCultureProvider or the AcceptLanguageHeaderRequestCultureProvider. And yes I've configured Startup.cs to use localization:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddLocalization();
    services.AddMvc();
}
...
private void ConfigureLocalization(IApplicationBuilder app)
{
    var supportedCultures = SharedClassLibrary.SharedExample.SupportedCultures;

    RequestLocalizationOptions options = new RequestLocalizationOptions();
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
    options.DefaultRequestCulture = new RequestCulture("en-US");

    app.UseRequestLocalization(options);
}

MatLeger avatar Aug 09 '16 18:08 MatLeger

@pranavkm - I don't believe this is a duplicate. After further investigation I have figured out what the problem is but I'm not sure how best to solve it.

When I build the LocalizationWebApp (2).zip sample I attached to the ticket (somebody did build it and run it right?) and look at the output directories for each project in the solution I can see that the SharedClassLibrary output folder contains sub folders for each resource language and each of these subfolders contains a "SharedClassLibrary.resources.dll".

If I look at the output directory for the ConsoleApplication which references the SharedClassLibrary, it contains the SharedClassLibrary.dll plus all the language folders that contain the SharedClassLibrary.resources.dll. When I run the console application localization works as expected.

When I look at the output folder for the ASP.NET Core web application I see that it contains the SharedClassLibrary.dll but it does not contain any of the language folders. If I run the ASP.NET Core web app localization does not work as expected. However, if I manually copy the language folders to the web app's output folder and then rerun the application, localization now works as expected.

This was working with RC1 where the DNX utilities were used so I'm guessing DNX was copying the language folders just like the ConsoleApplication does in my sample solution. This appears to have been broken when ASP.NET Core 1.0 was released with the dotnet CLI.

So now my question is what is the best way to get these language folders copied to the ASP.NET Core web app's output directory. If this were a normal project I'd probably add a post build step but I'm not sure what the equivalent is in ASP.NET Core land. There is a /wrap/SharedClassLibrary/project.json file for the ASP.NET Core web app to reference. Is there a way to tell the web app to include the language folders in that project.json file?

MatLeger avatar Sep 06 '16 22:09 MatLeger

@MatLeger thanks for investigating, it was very helpful. I've looked at this and talked with @pranavkm and we think this is a bug in the cli. I've filed dotnet/cli#4153 on them detailing the problem. If you think I've missed anything important there please let me know.

If you would like to use a script to work around this in the mean time the starting point would be a postpublish step in your project.json.

ryanbrandenburg avatar Sep 08 '16 17:09 ryanbrandenburg

@MatLeger Hi Mat. Can you help me? your code is working but, in my case, am getting my culture info from my custom middleware only, so i cannot hardcode it in startup.cs file. How should i modify the code to use options.DefaultRequestCulture = new RequestCulture("en-US"); from my custom middleware?

rajeshj18 avatar Sep 06 '17 05:09 rajeshj18

[error]: listen tcp 外网ip:9090: bind: cannot Rab, 6 Sep 2017 pukul 12.57 rajeshj18 [email protected] menulis:

@MatLeger https://github.com/matleger Hi Mat. Can you help me? your code is working but, in my case, am getting my culture info from my custom middleware only, so i cannot hardcode it in startup.cs file. How should i modify the code to use options.DefaultRequestCulture = new RequestCulture("en-US"); from my custom middleware?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aspnet/Home/issues/1707#issuecomment-327382856, or mute the thread https://github.com/notifications/unsubscribe-auth/AZCBwd9u_bwSRyCTwmoiCd_qPPr-HtTHks5sfjRigaJpZM4JgWGa .

-- Iphone Yurihiko

usagi70 avatar Sep 06 '17 06:09 usagi70

This issue is being closed because it has not been updated in 3 months.

We apologize if this causes any inconvenience. We ask that if you are still encountering this issue, please log a new issue with updated information and we will investigate.

aspnet-hello avatar Dec 30 '17 23:12 aspnet-hello