C# 类库本地化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1138934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:09:24  来源:igfitidea点击:

C# Class Library Localization

c#localization

提问by JL.

I need a very quick introduction to localization in a class library

我需要快速介绍类库中的本地化

I am not interested in pulling the locale from the user context, rather I have users stored in the db, and their locale is also setup in the db....

我对从用户上下文中提取语言环境不感兴趣,而是我将用户存储在数据库中,并且他们的语言环境也在数据库中设置....

my functions in the class library can already pull the locale code from the user profile in the db... now I want to include use resx depending on locale...

我在类库中的函数已经可以从数据库中的用户配置文件中提取区域设置代码...现在我想根据区域设置使用 resx...

I need a few steps to do this correctly...

我需要几个步骤才能正确执行此操作...

And yeah - I have already googled this, and some research, but all the tutorials I can find are way too complex for my needs.

是的 - 我已经在谷歌上搜索了这个,并进行了一些研究,但是我能找到的所有教程对于我的需求来说都太复杂了。

采纳答案by NotMe

Unfortunately, this subject is way too complicated. ;) I know, I've done the research as well.

不幸的是,这个主题太复杂了。;) 我知道,我也做过研究。

To get you started though,

为了让你开始,

  1. create a Resources directory in your assembly.

  2. Start with English and add a "Resources File" (.resx) to that directory. Name it something like "text.resx". In the event that the localized resource can't be found, the app will default to pulling out of this file.

  3. Add your text resources.

  4. Add another resources file. Name this one something like "text.es.resx" Note the "es" part of the file name. In this case, that defines spanish. Note that each language has it's own character code definition. Look that up.

  5. Add your spanish resources to it.

  1. 在程序集中创建一个 Resources 目录。

  2. 从英语开始,然后将“资源文件”(.resx) 添加到该目录。将其命名为“text.resx”。如果找不到本地化资源,应用程序将默认拉出此文件。

  3. 添加您的文本资源。

  4. 添加另一个资源文件。将此命名为“text.es.resx”,注意文件名的“es”部分。在这种情况下,这定义了西班牙语。请注意,每种语言都有自己的字符代码定义。看那个。

  5. 将您的西班牙语资源添加到其中。

Now that we have resource files to work from, let's try to implement.

现在我们有了可以使用的资源文件,让我们尝试实现。

In order to set the culture, pull that from your database record. Then do the following:

为了设置文化,从您的数据库记录中提取。然后执行以下操作:

String culture = "es-MX"; // defines spanish culture
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

This could happen in the app that has loaded your assembly OR in the assembly initialization itself. You pick.

这可能发生在已加载程序集的应用程序中或程序集初始化本身中。你选。

To utlize the resource, all you have to do is something like the following within your assembly:

要利用资源,您所要做的就是在程序集中执行以下操作:

public string TestMessage() {
  return Resources.Text.SomeTextValue;
}

Ta Da. Resources made easy. Things can get a little more complicated if you need to change usercontrols or do something directly in an aspx page. Update your question if you need more info.

达。资源变得容易。如果您需要更改用户控件或直接在 aspx 页面中执行某些操作,事情可能会变得更加复杂。如果您需要更多信息,请更新您的问题。

Note that you could have resource files named like "text.es-mx.resx" That would be specific to mexican spanish. However, that's not always necessary because "es-mx" will fall back to "es" before it falls back to the default. Only you will know how specific your resources need to be.

请注意,您可以将资源文件命名为“text.es-mx.resx”,这将特定于墨西哥西班牙语。但是,这并不总是必要的,因为“es-mx”会在回退到默认值之前回退到“es”。只有您会知道您的资源需要多么具体。

回答by NikolaiDante

Name your resxes with the culture in them (eg. resource_en-GB.resx) and select which resource to query based on the culture.

使用其中的文化命名您的 resxes(例如,resource_en-GB.resx)并根据文化选择要查询的资源。

回答by user3711989

In order to access the Resource file from code, you need to open the resource file and then change the "Access Modifier" dropdown to "public", mine was "no code generation". After that you can access like: Resources.FileName.ResourceName.

为了从代码访问资源文件,您需要打开资源文件,然后将“访问修饰符”下拉列表更改为“公共”,我的是“不生成代码”。之后,您可以访问:Resources.FileName.ResourceName。