简体   繁体   English

C#类库本地化

[英]C# Class Library Localization

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... 我在类库中的函数已经可以从db中的用户配置文件中提取语言环境代码...现在我想根据语言环境包含使用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. 是的 - 我已经用谷歌搜索了一些研究,但我能找到的所有教程都太复杂了,无法满足我的需求。

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. 在程序集中创建一个Resources目录。

  2. Start with English and add a "Resources File" (.resx) to that directory. 从英语开始,在该目录中添加“资源文件”(.resx)。 Name it something like "text.resx". 将其命名为“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. 将此名称命名为“text.es.resx”。注意文件名的“es”部分。 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. 添加您的西班牙语资源。

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. 如果您需要更改用户控件或直接在aspx页面中执行某些操作,事情会变得复杂一些。 Update your question if you need more info. 如果您需要更多信息,请更新您的问题。

Note that you could have resource files named like "text.es-mx.resx" That would be specific to mexican spanish. 请注意,您可以拥有名为“text.es-mx.resx”的资源文件,这将特定于墨西哥西班牙语。 However, that's not always necessary because "es-mx" will fall back to "es" before it falls back to the default. 但是,这并不总是必要的,因为“es-mx”会在它回退到默认值之前回退到“es”。 Only you will know how specific your resources need to be. 只有您将了解您的资源需要具体的具体情况。

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

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。

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

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