0

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?

Dabblernl
  • 15,831
  • 18
  • 96
  • 148

2 Answers2

2

I solved it thus:

Resource files

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: enter image description here

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,
                _ => "?"
            };
        }
    }
Dabblernl
  • 15,831
  • 18
  • 96
  • 148
1

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

enter image description here

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 ;)

LazZiya
  • 5,286
  • 2
  • 24
  • 37
  • If I am to inject the 'IStringLocalizer' I need to come up with an implementation for it as in several pieces of code I instantiate a 'Month ' myself. – Dabblernl Jun 01 '20 at 15:11
  • I tried to create an old school resources.resx file in the Properties folder of the project with Public access, but I get an error that no public resource generator can be found. – Dabblernl Jun 01 '20 at 15:13
  • Do you get the same error when you create a resource under any custom folder (other than Properties folder)? – LazZiya Jun 01 '20 at 15:36
  • you pointed me in the right direction. Below my solution. Many thanks, – Dabblernl Jun 01 '20 at 18:28