在Console .Net Core 1.1应用程序中使用.resx文件 [英] Using .resx files in Console .Net Core 1.1 application

查看:54
本文介绍了在Console .Net Core 1.1应用程序中使用.resx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试检查一些很棒的.Net Core 1.1功能,但在过程中遇到了一个问题.

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

我试图创建一个简单的.NetCore控制台应用程序

I tried to create a simple .NetCore Console application

我还在根目录中创建了两个简单的.resx资源文件,其中包含字符串资源.

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.

当我更改 CultureInfo 并再次加载字符串时.

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

我做错什么了吗?我期望本地化应该像.NET的早期版本中一样工作,但事实并非如此.

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

我的示例代码如下:

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

推荐答案

您必须安装此软件包" Microsoft.Extensions.Localization ",那么您上面的代码将以你要.它已验证uisng .net core 2.0和控制台应用程序

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

这篇关于在Console .Net Core 1.1应用程序中使用.resx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆