Search code examples
.net-coreconsole-applicationasp.net-core-localization

Using .resx files in Console .Net Core 1.1 application


I was trying to check some cool .Net Core 1.1 features, but in process I met an issue.

I tried to create a simple .NetCore Console application

enter image description here

I also created two a simple .resx resourse files in root directory with a string resourse.

I let console load the resourse and It was loaded successfully.

When I changed the CultureInfo and loaded the string again.

I expected, that the localized string will be loaded, but console showed the same (English) default resourse

Do I doing something wrong? I expected, that localization should work like in previous versions of .NET, but It didn't.

My sample code below:

        //default string loaded
        Console.WriteLine(AppResource.resourseText);
        Console.ReadLine();

        //changing CultureInfo
        CultureInfo.CurrentCulture = new CultureInfo("cs");
        CultureInfo.CurrentUICulture = new CultureInfo("cs");
        CultureInfo cul = new CultureInfo("cs");

        //loading the localized string 3 different ways
        Console.WriteLine(AppResource.resourseText);
        Console.WriteLine(AppResource.ResourceManager.GetString("resourseText"));
        Console.WriteLine(AppResource.ResourceManager.GetString("resourseText", cul));

        Console.ReadLine();

Solution

  • You must install this package "Microsoft.Extensions.Localization", then Your code above will work as You want. It has verified uisng .net core 2.0 and console app