基于当前区域性调用不同(本地化)查看 [英] Call different (localized) view based on current culture

查看:159
本文介绍了基于当前区域性调用不同(本地化)查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 LocalizationAttribute 它实现了 ActionFilterAttribute 本地化意见。我只是把 [本地化] 控制器。我用LocalizeStrings.resx文件,在此基础上的语言是对当前线程应用。一切工作为这个简单的例子(与本地化的字符串)。现在我想本地化整个页面(不只是字符串)。



要acomplish这个你用哪种方法?



我需要确定哪些线程控制器的电流和基于该值调用视图:

 公众的ActionResult公司简介( )
{
开关(Thread.CurrentThread.CurrentUICulture.Name)
{
案EN-US:
返回视图(EnglishUSView);
案去-DE:
返回视图(德语);
默认:
返回查看();
}
返回查看();
}

或你推荐别的?


< DIV CLASS =h2_lin>解决方案

我会建议简单地扩展 RazorViewEngine 并重写 FindPartialView FindView ,以便通知视图引擎寻找与内螺纹当前文化有看法。如果无法找到该网页进行比为usuall。




  1. 扩展 RazorViewEngine

  2. 内部视图文件夹应该有(假定,英语是默认郎。) Index.cshtml Index.de-DE.cshtml

  3. 修改的Global.asax.cs 文件,里面的应用程序启动你应该叫本地化的视图引擎(实现RazorViewEngine一)



控制器

 无需进一步修改需要

查看

  /Views/Home/Index.cshtml 
/Views/Home/Index.de.DE.cshtml

助手

 公共类LocalizedRazorViewEngine:RazorViewEngine 
{
公众覆盖ViewEngineResult FindPartialView ...
公众覆盖ViewEngineResult FindView ...
}

ApplicationStart

  ViewEngines.Engines.Clear(); 
ViewEngines.Engines.Add(新LocalizedRazorViewEngine());


I'm using LocalizationAttribute which implements ActionFilterAttribute to localize views. I simply put [Localize] on controller. I was using LocalizeStrings.resx files to apply based on which language is on current thread. Everything works for this simple case (with localized strings). Now I want to localize complete pages (not just strings).

To acomplish this which approach do you use?

Do I need to identify which thread is current on controller and based on that value to call view:

public ActionResult AboutUs()
{
    switch (Thread.CurrentThread.CurrentUICulture.Name)
    {
        case "en-US":
            return View("EnglishUSView");               
        case "de-DE":
            return View("GermanView");              
        default:
            return View(); 
    }
    return View();
}

or do you recommend something else ?

解决方案

I would recommend to simply extended RazorViewEngine and override FindPartialView and FindView in order to inform ViewEngine to look is there view with current culture inside thread. If that page cannot be found than proceed as usuall.

  1. Extend RazorViewEngine
  2. Inside View folder there should be (assuming that english is default lang.) Index.cshtml and Index.de-DE.cshtml
  3. Modify Global.asax.cs file, inside application start you should call localized view engine (the one which implements RazorViewEngine)

Controller

no further modification needed

Views

/Views/Home/Index.cshtml
/Views/Home/Index.de.DE.cshtml

Helper

public class LocalizedRazorViewEngine : RazorViewEngine
{
   public override ViewEngineResult FindPartialView ...
   public override ViewEngineResult FindView...
}

ApplicationStart

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new LocalizedRazorViewEngine());

这篇关于基于当前区域性调用不同(本地化)查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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