Subscribed unsubscribe Subscribe Subscribe

Silver Light and Blue Sky

C# and Infrastructure. Code is everything.

resource file naming in ASP.NET Core localization

While I'm reading the document, I found difficulty in resource file naming.

Globalization and localization | Microsoft Docs

To summarize first:

  • "name" property of the project.json uses as the assembly name of the .NET Core project. The top level folder name is used if this property is not specified.
  • We must abbreviate the assembly name from the resource class FQDN if the FQDN starts with the assembly name.
  • We must use full FQDN if the FQDN doesn't start with the assembly name.

When we localize messages in ASP.NET Core project, we use DI with IStringLocalizer<T> and IHtmlLocalizer<T>. We specify the class T for resources. It's same as IViewLocalizer in the View and ViewModel. We may have to prepare resource file (resx) for each class T and obey the file naming constitution. The constitution can be categorized in two cases. The one is the assembly name is the same as the FQDN of class T. The other case is the assembly name is different from FQDN. If we use Visual Studio for creating ASP.NET Core projet, the assembly name is usually same as project name and default namespace. In this case, the FQDN always starts with this assembly name as far as you don't create the class whose namespace is not a default namespace. However, for example, if you create ASP.NET Core project with dotnet new -t web your default namespace is always "WebApplication". And the assembly name is the same as the top folder name as name property is not specified in generated project.json.

case 1: FQDN starts with assembly name

The resource file name must be abbreviated assembly name from FQDN.

assembly name: AspNetCore.StarterWeb
class name: AspNetCore.StarterWeb.Controllers.HomeController (used by IStringLocalizer<HomeController> )
resource file: Controllers.HomeController.ja.resx (in the case of Japanese resouce)

AspNetCore.StarterWeb.Controllers.HomeController.ja.resx is not working.

case 2: FQDN doesn't start with assembly name

The resource file name must be the FQDN.

assembly name: AspNetCore.StarterWeb
class name: WebApplication.Controllers.HomeController (used by IStringLocalizer<HomeController> )
resource file: WebApplication.Controllers.HomeController.ja.resx (in the case of Japanese resouce)

This blog entry is based on my posted issue.

github.com