Joe Lu
Joe Lu

Reputation: 2370

.NET MVC localized resource file is not read

I was trying to add some satellite resource files to display different language I followed these two posts in general

stackoverflow post

Localization of a Site in MVC5 Using Resource File

here's what I have done in nutshell (using visual studio 2017, .net framework 4.5)

I created a folder named it "Resources"

I right clicked this folder and created a file named it "Lang.resx", added a string pair (name: Welcome, value: Hello), then switch it to public and save

I right clicked this folder and created another file named it "Lang.es.resx", added a string pair (name: Welcome, value: Hola), then switch it to public and save

I opened up web.config and added following line to System.Web

<globalization enableClientBasedCulture="true" culture="auto" uiCulture="auto" />

I opened up a blank view, write the following to that view

@{ 
  System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.GetCultureInfo("es");
  System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo("es");
}

<div>@MyProject.Resources.Lang.Welcome</div>
<div>@Thread.CurrentThread.CurrentCulture.DisplayName</div>
<div>@Thread.CurrentThread.CurrentUICulture.DisplayName</div>

The output is as following:

Hello Spanish Spanish

As you can see, the culture is changed but somehow the Lang.es.resx is not read, I have created multiple new project in different hosting VPS, the result is always the same. So there must be something I've done incorrectly, or some steps missing

Upvotes: 0

Views: 1797

Answers (2)

StP
StP

Reputation: 51

In the file properties in Visual Studio, set:

Copy to output directory: Copy always

If you want for multiple files within a folder, you can always edit your .csproj file

<Content Include="parent directory\*.*">
  <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>

Upvotes: 0

Joe Lu
Joe Lu

Reputation: 2370

ok, after a whole day debugging, I've found out the cause

  1. When using resource file, it creates additional folder to the current bin folder, say Language.es.resx, then it creates a folder called es
  2. When I upload the project to the ftp server, this language folder didn't get created and the es recource.dll file was not uploaded

I think this is it, hopefully this can help anyone facing the same problem

Upvotes: 1

Related Questions