简体   繁体   中英

How to reference a resource file from a Model in ASP.Net Core (3.1)?

So, I read all about the new localization system in ASP.Net Core (3.1) and successfully made use of the IStringLocalizer<MyController> and the IViewLocalizer<MyView> . I also could use the localization for the [DisplayName("Property description") in Models.

Below what I seem unable to do: In good old.Net Framework I could do this:

 public class Month
{
    public int MonthNumber { get; set; }

    public string  Name
    {
        get
        {
            switch(MonthNumber)
            {
                case 1:
                    return Properties.Resources.Jan;
                case 2:
                    return Properties.Resources.Feb;
                default:
                    return "?";
            }
        }
    }

But how can I do this in a Model in ASP.Net Core 3.1?

I solved it thus:

资源文件

Just add the resource files following the structure you have chosen. All docs and tutorials suggest you take a folder named "Resources" as your base folder, so that is what you see here. The link that is most probable to survive over time that explains how to use resources in an ASP.Net Core project: Microsoft docs on Localization for ASP.Net Core

Make sure that you mark all three "Month" resx files as Public: 在此处输入图像描述

Visual Studio will at first complain with the message:

Custom tool PublicResXFileCodeGenerator failed to produce an output for input file 'Month.en.resx' but did not log a specific error.

Simply get rid of this error by restarting Visual Studio!

Now you can use the resources as follows:

  public string Name
    {
        get
        {
            return MonthNumber switch
            {
                1 => Resources.Models.Month.Jan,
                2 => Resources.Models.Month.Feb,
                _ => "?"
            };
        }
    }

You need to inject IStringLocalizer to the class:

public class Month
{
    public int MonthNumber { get; set; }

    private readonly IStringLocalizer Localizer;

    public Month(IStringLocalizer localizer)
    {
        Localizer = localizer;
    }

    public string  Name
    {
       get
       {
           switch(MonthNumber)
           {
               case 1:
                  return Localizer["Jan"];
               case 2:
                   return Localizer["Feb"]
               default:
                   return "?";
           }
       }
    }
}

Another approach can be done by adding localized month names for the numbers in the resource file, so:

var monthName = Localizer["4"]; 
// result: April for English culture
// or Nisan for Turkish culture

Just for clarification;

The resource key can have three types of access modifiers:

  • Internal
  • Public
  • No code generation

在此处输入图像描述

If the key is marked with Internal or Public you can access it as you mentioned, because the compiler will auto generate a static class .cs linked to the relevant resource file with the key names as accessible properties.

But the common approach with Asp.Net Core is to work with Shared resources , and shared resource has the access modifier as No code generation ; so thats mean no peoperty keys will be generated at the backend ( .cs will not be generated). And in this case you have to inject the IStringLocalizer or whatever locaizer in use to the class.

So changing the key access modifier to Internal or Public can work as well, but it is not a best practice;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM