ASP.NET MVC注册自定义的文化和国际化 [英] ASP.NET MVC Registering custom culture and internationalization

查看:186
本文介绍了ASP.NET MVC注册自定义的文化和国际化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET MVC 4项目,我已经注册了自定义的文化,因为我想有一个特定的客户端翻译。

I have an ASP.NET MVC 4 project and I've registered the custom culture in it because I want to have a client-specific translation.

我叫像 RegisterCulture参数下面的辅助方法(EN-GB-CustA,英语(客户A),EN-GB); 此调用在的Application_Start 完成 MvcApplication的事件处理程序:一个HttpApplication

private static void RegisterCulture(string cultureCode, string cultureName, string baseCultureCode)
{
    var ci = new CultureInfo(baseCultureCode);
    var ri = new RegionInfo(ci.Name);
    var builder = new CultureAndRegionInfoBuilder(cultureCode, CultureAndRegionModifiers.None);
    builder.LoadDataFromCultureInfo(ci);
    builder.LoadDataFromRegionInfo(ri);

    builder.CultureEnglishName = cultureName;
    builder.CultureNativeName = cultureName;

    try
    {
        builder.Register();
    }
    catch (InvalidOperationException)
    {

    }
}

该方法是神仙简单,它基本上会基于现有的新的文化和替换它的名字。

The method is fairy simple, it basically creates new culture based on existing one and replaces it's name.

现在在我的Global.asax只是为了测试目的,我把下面的code到 MvcApplication 类切换当前线程自定义之一。

Now in my Global.asax just for the testing purposes I've put the following code to MvcApplication class to switch current thread for the custom one.

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    var ci = new CultureInfo("en-GB-CustA");

    Thread.CurrentThread.CurrentUICulture = ci;
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name);
}

我还包含两种资源文件。一个叫 Test.resx 这是默认文本,第二个是 Test.en-GB-CustA.resx 。我添加了一个简单的字符串资源有一个名为标题有两个不同的值。

I've also included two resources files. One is called Test.resx which is for the default texts, the second one is Test.en-GB-CustA.resx. I've added a simple string resource there called Title with two different values.

Test.resx => "Hello World!"
Test.en-GB-CustA => "Hello from custom culture!"

我也把我的观点之一,code,以显示这个标题(我已经添加了 VIEWRES 为S CustomToolNamespace 为简化两个资源文件)。

I've also put on one of my view the code to display this title (I've added ViewRes as s CustomToolNamespace for both resource files for simplification).

@ViewRes.Test.Title

尽管我已经设置了自定义的文化descibed我得到detault之前的Hello world的价值所有的时间可​​惜的是。缺少什么我在这里?

Unfortunatelly even though I've set the custom culture as descibed before I'm getting the detault "Hello world" value all the time. What am I missing here?

推荐答案

我结束了在重命名code形成我的定制文化所以像 EN-XX 。我还必须同时定义 TwoLetterISOLanguageName ThreeLetterISOLanguageName ,并正确注册。

I ended up renaming the code form my custom culture so something like en-XX. I've also have to define both TwoLetterISOLanguageName and ThreeLetterISOLanguageName and it registered properly.

这篇关于ASP.NET MVC注册自定义的文化和国际化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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