本地化MVC一个页面上的文本 [英] Localize text on one page in MVC

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

问题描述

我学习asp.net MVC的,所以我没有这么多的经验。
我想在一个页面上本地化的文本,但我不想把标签浏览模式。
太多的文字必须本地化,它看起来不正确摆在视图模型属性的所有文本。

I am learning asp.net MVC so I do not have so much experience. I want to localize text on one page but I don’t want put labels to view model. Too much text must be localized and it does not look right to put all text in view model properties.

我想读直接从资源文件剃刀本地化值。
是这可能吗?
在哪个文件夹我应该创建资源文件?
我应该如何命名的资源文件?
如何在页面从资源文件中读取值?

I would like to read localized value with razor directly from resource file. Is this possible? In what folder should I create resource file? How should I name resource file? How to read value at page from resource file?

推荐答案

这不是从你纯C#做什么不同码。详细说明(即使它不是剃刀一样的概念仍然适用),可在的这篇文章

It's not different from what you would do in plain C# code. A detailed description (even if it's not about Razor same concepts still apply) can be found in this article.

所有你必须决定你想要把你的资源文件第一。选任的资源文件夹中。亚历克斯建议(和它的相当合理)来模拟解决方案结构(视图,模型和控制器),也可把一切井井有条,整洁。在实践中你有这样的结构:

First of all you have to decide where you want to put your resource files. Elective place is Resources folder. Alex suggests (and it's pretty reasonable) to mimic solution structure (views, model and controllers) there to keep things organized and tidy. In practice you'll have a structure like this:


.
   /Resources
       /Models
           LogInModel.resx
       /Controllers
           LogIn.resx
       /Views
           /Home
               LogIn.resx
               Index.resx   

和等。本地化的资源将被简单地使用这样的:

and so on. Localized resources will be simply used like this:

查看

<head>
    <title>@MyProject.Resources.Views.Home.Index.Title</title>
</head>
<body>
    <h1>@MyProject.Resources.Views.Home.Index.Welcome</h1>
</body>



模式

[Required(ErrorMessageResourceName = "Required", 
    ErrorMessageResourceType = typeof(MyProject.Resources.Models.LogIn))]
[ValidatePasswordLength(ErrorMessageResourceName = "PasswordMinLength", 
    ErrorMessageResourceType = typeof(MyProject.Resources.Models.LogIn))]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }



控制器

var user = FindUserByName(userName);
if (user == User.Nobody)
    ModelState.AddModelError("1", MyProject.Resources.Controllers.Search.UserNotFound);

现在,让我们能够本地化(这可能是每个线程不同,那么其请求)。这只是一个例子,请参阅本文章的所有细节:

Now let's enable localization (which may be different for each thread then for its request). This is just an example, please refer to full article for all details:

protected void Application_AcquireRequestState(object sender, EventArgs e)
{
    if (HttpContext.Current.Session != null)
    {
        string name = (CultureInfo)this.Session["Culture"];
        if (!String.IsNullOrEmpty(name))
        {
            CultureInfo ci = new CultureInfo(name );
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture =
                CultureInfo.CreateSpecificCulture(ci.Name);
        }
    }
}



参考



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