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

查看:33
本文介绍了在控制台 .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

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

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