简体   繁体   English

在Windows应用程序C#中以不同语言打印输出

[英]printout in different languages in windows application c#

My application is not to be localized yet but I have to print a report in different languages. 我的应用程序尚未本地化,但我必须用不同的语言打印报告。 So, i think i will create different resources for report text but how to set the particular resource for selected language. 因此,我认为我将为报告文本创建不同的资源,但如何为所选语言设置特定的资源。

like for Spanish language how PrintResource.es-ES.resx will be set. 像西班牙语一样,将如何设置PrintResource.es-ES.resx。

do i have to use if..else..if 我是否必须使用if..else..if

You should not have to do anything beyond just ensuring you're running with the right culture at the time you produce the report. 除了在生成报告时确保以正确的文化运行之外,您无需做任何其他事情。

Here's an example: 这是一个例子:

  1. Create a new Console project 创建一个新的控制台项目
  2. Add a Resource file to it, call it Strings.resx 向其中添加一个资源文件,将其称为Strings.resx
  3. Rename the "String1" key to "DisplayName", and give it a value, like "English" 将“ String1”键重命名为“ DisplayName”,并为其提供一个值,例如“ English”
  4. Add another resource file, call it Strings.nb-NO.resx 添加另一个资源文件,将其称为Strings.nb-NO.resx
  5. Rename the "String1" key in the norwegian file to "DisplayName" as well, and give it a different value, like "Norwegian" 也将挪威文文件中的“ String1”键也重命名为“ DisplayName”,并给它一个不同的值,例如“ Norwegian”

Then in the main Program.cs, paste in the following code: 然后在主Program.cs中,粘贴以下代码:

using System;
using System.Globalization;
using System.Threading;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main()
        {
            Console.WriteLine(Strings.DisplayName);

            Thread.CurrentThread.CurrentUICulture =
                CultureInfo.GetCultureInfo("nb-NO");

            Console.WriteLine(Strings.DisplayName);
        }
    }
}

Execute this, and you should see "English" followed by "Norwegian" on the console. 执行此操作,您将在控制台上看到“英语”和“挪威语”。

As you can see from this example, as long as the right UI culture is set, the right resource file will be read. 从该示例中可以看到,只要设置了正确的UI文化,就会读取正确的资源文件。

The code that printed the english text and the code that printed the norwegian text is identical, but the underlying culture has changed, so it will use different resource files. 打印英文文本的代码和印刷挪威文本的代码是相同的,但是底层的文化已经改变,因此它将使用不同的资源文件。

If you don't want to "soil" the rest of the application with spanish UI after producing the report, simply spin up a thread for the reporting code, and set the UI culture for that thread only to spanish. 如果您不想在生成报告后使用西班牙语UI对应用程序的其余部分进行“破坏”,只需为报告代码启动一个线程,并将该线程的UI文化设置为西班牙语即可。 The resource subsystem of .NET should handle the rest. .NET的资源子系统应处理其余部分。

Also make sure you're formatting numbers and dates using culture-aware method overloads. 另外,请确保使用了解文化的方法重载来格式化数字和日期。

This: 这个:

Console.WriteLine(string.Format(CultureInfo.CurrentUICulture, "{0}: {1}", Strings.DisplayName, 10.31));

Might produce different text from this: 可能由此产生不同的文本:

Console.WriteLine(string.Format("{0}: {1}", Strings.DisplayName, 10.31));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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