文化没有在多语种的网站应用 [英] Culture not being applied in multilingual site

查看:213
本文介绍了文化没有在多语种的网站应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了我彻底难倒一个非常不寻常的问题。我们有一个多语种网站,所以我们使用资源文件。然而,每一段文字,我们的看法已被烧毁,像< A HREF =#> @ TextResources.my_key< / A> 将被定位于随机的文化。这种情况只有在我的Azure部署,我不能重现本地。

添加到神秘的是,有文字的孤独位总是尊重我的文化变革。该文本通过方法调用检索:

 < A HREF =#> @ ConfigUtils.getTerms()< / A>
 

方法是:

 公共静态字符串getTerms()
{
    字符串键= GetKeyFromDb(CONSTANTS.TERMS);
    如果(!string.IsNullOrEmpty(键))
    {
        返回TextResources.ResourceManager.GetString(键);
 

我还是从我们的资源文件中读取,但在这种情况下,它被本地化为所需!正在应用的培养资源文件被读入后视图,但这种方法被称为前?!

我们所有的控制器从基本控制器,其中我们覆盖 OnActionExecuting()来申请我们的文化传承

 保护覆盖无效OnActionExecuting(ActionExecutingContext filterContext){
    ContextModel CTX =(ContextModel)会议[ContextModel];
    //根据用户设置的值设置正确的定位
    如果(CTX = NULL和放大器;!&安培;!ctx.UserLanguageId = NULL){
        Thread.CurrentThread.CurrentUICulture =新的CultureInfo(ctx.UserLanguageId);
        Thread.CurrentThread.CurrentCulture =新的CultureInfo(ctx.UserLanguageId);
    }
}
 

在我开始移动文化管理code到不同的地方和重新部署,希望能够解决问题到Azure,我希望有人有想法,以通过方法调用,为什么只有文字检索被本地化。

OnActionExecuting()的动作之前执行,所以我想这将是合适的位置把文化管理code。有没有其他地方会更好?

更新

它看起来像这个问题presents本身就是一个部署后,但可以通过重新启动云服务来解决。

更新2 每@ RichardSchneider的要求,自动生成 TextResources code是如下:

 公共静态字符串my_key {
    得到 {
        返回ResourceManager.GetString(my_key,resourceCulture);
    }
}
 

解决方案

在万一出现这种情况给别人,我下定决心的根本原因。我们有一个页面,用户能够通过在一封电子邮件中向他们提供一个URL下载了一份报告。该URL包括用于本地化的语言查询字符串变量:

  ?

/报告/下载ID = 123安培; LANG =德

在控制器动作,原作者设置我们的资源对象的文化,而不是螺纹:

  TextResources.Culture =新的CultureInfo(德);
 

综观元数据:

  [生成code(System.Resources.Tools.StronglyTypedResourceBuilder,4.0.0.0)]
公共类TextResources
{
公共静态的CultureInfo文化{获得;组; }
 

因此​​,出现此属性接受precedence在线程的文化,当谈到次申请本地化我们的意见。此外,由于房地产是静态的,它是全球范围内跨任何Azure的情况下担当了报告申请。所以,当人们下载各种语言的这些报告,效果似乎是随机的。

我承认我仍然不清楚为什么下面始终尊重线程文化:

 公共静态字符串getTerms()
{
    字符串键= GetKeyFromDb(CONSTANTS.TERMS);
    如果(!string.IsNullOrEmpty(键))
    {
         返回TextResources.ResourceManager.GetString(键);
 

I have a very unusual issue that has me completely stumped. We have a multilingual website so we employ Resource files. However, every piece of text on our Views that has been burned in like <a href="#">@TextResources.my_key</a> will be localized to a random culture. This happens only on my Azure deployment, I cannot reproduce locally.

Adding to the mystery is that there is a lone bit of text that ALWAYS respects my change of culture. That text gets retrieved via a method call:

<a href="#">@.ConfigUtils.getTerms()</a>

Method is:

public static string getTerms()
{
    string key = GetKeyFromDb(CONSTANTS.TERMS);
    if (!string.IsNullOrEmpty(key))
    {
        return TextResources.ResourceManager.GetString(key);

I'm still reading from our resource file, but in this context, it is being localized as desired! Is the culture being applied after the resource file is read in the view, but before this method is called?!

All our controllers inherit from a base controller, where we override OnActionExecuting() to apply our culture:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {
    ContextModel ctx = (ContextModel) Session["ContextModel"];
    // Set the correct localization according to the value set by the user
    if (ctx != null && ctx.UserLanguageId != null){
        Thread.CurrentThread.CurrentUICulture = new CultureInfo (ctx.UserLanguageId);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(ctx.UserLanguageId);
    }
}

Before I start moving the culture management code to different spots and re-deploying to Azure in hopes that resolves the issue, I'm hoping that someone has a thought as to why only the text retrieved via the method call gets localized.

OnActionExecuting() is executed before the action, so I had thought this would be the appropriate spot to put culture management code. Is there somewhere else that would be better?

UPDATE

It looks like this issue presents itself after a deployment, but can be resolved by restarting the cloud service.

UPDATE 2 Per @RichardSchneider's request, the auto-generated TextResources code is as follows:

public static string my_key{
    get {
        return ResourceManager.GetString("my_key", resourceCulture);
    }
}

解决方案

In case this happens to someone else, I determined the root cause. We had a page where users were able to download a report via a URL provided to them in an email. The URL included a language querystring variable used for localization:

/report/download?id=123&lang=de

In the controller action, the original author set the culture on our resource object, not the thread:

TextResources.Culture = new CultureInfo("de");

Looking at the metadata:

[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
public class TextResources
{
public static CultureInfo Culture { get; set; }

So, it appears this property takes precedence over the thread culture when it comes times to apply localization in our views. And since the property is static, it was applied globally across whichever Azure instance was serving the report. So as people were downloading these reports in various languages, the effect appeared random.

I admit I am still unclear as to why the following always respected thread culture:

public static string getTerms()
{
    string key = GetKeyFromDb(CONSTANTS.TERMS);
    if (!string.IsNullOrEmpty(key))
    {
         return TextResources.ResourceManager.GetString(key);

这篇关于文化没有在多语种的网站应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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