C# 类库本地化 [英] C# Class Library Localization

查看:13
本文介绍了C# 类库本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要快速了解类库中的本地化

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....

我在类库中的函数已经可以从数据库中的用户配置文件中提取语言环境代码...现在我想根据语言环境包含使用 resx...

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...

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

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.

为了让你开始,

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

  1. create a Resources directory in your assembly.

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

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.

添加您的文本资源.

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

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.

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

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;
}

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

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.

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

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.

这篇关于C# 类库本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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