如何基于某些参数加载不同的RESX文件 [英] How to load different RESX files based on some parameter

查看:262
本文介绍了如何基于某些参数加载不同的RESX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET3.5(C#)ASPX是国际化在10种不同语言的页面。

I have an ASP.NET3.5 (C#) ASPX page that is internationalized in 10 different languages.

该网页是非常复杂的结构与数十家由状态机模式驱动嵌套的看法。

The page is very complex in its structured with dozens of nested views driven by a state machine pattern.

编辑:我使用的是元:的ResourceKey 语法每个ASP的控制,它允许使用声明的语法隐性资源EX pressions

I am using the meta:resourcekey syntax in every asp control, which allows to use declarative syntax for Implicit Resource expressions.

我一直在问到品牌的基础上的一些查询字符串参数的页面。品牌意味着不只是加载不同的CSS文件,也有不同的文字导语(所有语言)。

I have been asked to "brand" the page based on some query string parameter. Branding will mean not just loading different CSS files, but also having different text blurbs (in all languages).

有没有一种简单的方法来交换的resx文件,而无需手动获取资源为每个数以百计文字和图像,我有这个页面?

Is there an easy way to "swap" resx files without having to get resources manually for each of the hundreds of literals and images that I have on this page?

在换句话说,假设我有以下RESX文件:

In other words, let's say I have the following RESX files:

brand1_myPage.aspx.en-US.resx
brand1_myPage.aspx.de-DE.resx
brand1_myPage.aspx.fr-FR.resx

brand2_myPage.aspx.en-US.resx
brand2_myPage.aspx.de-DE.resx
brand2_myPage.aspx.fr-FR.resx

myPage.aspx将寻找RESX命名文件的 myPage.xx-XX.resx 的。

有没有办法来加载,而不是任的 brand1xxx.resx 的文件或 brand2xxx.resx 的基础上一定的价值?

Is there a way to load instead either the brand1xxx.resx files or the brand2xxx.resx based on some value?

在此先感谢。

推荐答案

您可以使用定制文化实现这样的效果。

You can use custom cultures to achieve this effect.

首先,创建并在系统中注册自定义的文化,例如:

First, create and register custom cultures in the system, for example:

CultureAndRegionInfoBuilder builder = new CultureAndRegionInfoBuilder("en-US-brand1", CultureAndRegionModifiers.None);
CultureInfo parentCI = new CultureInfo("en-US");
RegionInfo parentRI = new RegionInfo("en-US");
builder.LoadDataFromCultureInfo(parentCI);
builder.LoadDataFromRegionInfo(parentRI);
builder.Parent = parentCI;
// set other properties of the custom culture (CultureEnglishName, CultureNativeName, possibly other ones)
// ...
builder.Register();

请注意,你可能想建立一个简单的工具来自动化这一点,因为这些文化需要将每个系统上安装在您的aplication会被编译或执行。需要管理权限,以便能够登记培养

Note that you might want to build a simple tool to automate this, since those cultures need to be installed on every system where your aplication will be compiled or executed. Administrative rights are needed to be able to register the cultures.

一旦你安装的文化,创造RESX文件,就像你通常会做的,但使用自定义的区域性名称(myPage.aspx.en-US-brand1.resx等)。

Once you have the cultures installed, create resx files like you would normally do, but use the custom culture names (myPage.aspx.en-US-brand1.resx etc).

现在所有剩下要做的就是基于一些参数(越早越好,的BeginRequest是个好地方,设置System.Threading.Thread.CurrentThread.CurrentUICulture;或PAGE_ preINIT如果你想这只是对一些页面):

Now all that's left to do is to set System.Threading.Thread.CurrentThread.CurrentUICulture based on some parameter (the sooner the better, BeginRequest is a good place; or Page_PreInit if you want this only for some pages):

CultureInfo ci = new CultureInfo(Request.QueryString["paramname"]);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;

(设置的CurrentCulture是不是真的有必要,如在的CurrentUICulture方面资源的工作,但同时设置允许您进一步自定义页面的各个品牌,如使用不同的日期/时间格式设置为每个自定义的文化/品牌)

(setting CurrentCulture is not really necessary, as resources work in terms of CurrentUICulture, but setting both allows you to further customize the page for each brand, e.g. use different date/time format settings for each custom culture/brand)

一些注意事项:

  • 在该解决方案为您提供了很大的灵活性,因为正常的区域性后备实际上是 - 如果没有找到一个条目为EN-US-brandX,运行时将回落为en-US等;这样可以大大减少重复的resx项,如果品牌大多是类似的,因为你可以把一些条目只有在父(EN-US)RESX文件,
  • 您可以创建多层次继承的文化,如EN-US-brandX-variantY,
  • 的预期访问资源工作的所有方法,
  • 改变工作线程的文化意味着如果你设置了文化,比方说,去-DE-brandX和你去DE安装在操作系统的本地化,你会得到本地化异常消息,
  • 由于上述原因,您可能希望为当前(UI)文化重置CultureInfo.InvariantCulture中的Application_Error,甚至还更好,只要你抓住,你知道会导致的Application_Error异常;这将$死亡的标准黄页的对$ pvent本地化和至少一部分的异常堆栈的,
  • 您可以考虑构建一个服务工具,这将注册/注销/更新的文化,特别是如果你预期变动频繁,
  • 在这种解决方案可能如果使用基于客户端的培养检测是有问题的。

这篇关于如何基于某些参数加载不同的RESX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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