用剃刀本地化意见 [英] Localized views with Razor

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

问题描述

我想成为机智和使用的VirtualPathProvider找到本地化的意见。该文件后检查时,它以请求的视图路径和修改它。如果发现它返回一个本地化的虚拟文件:

I was trying to be witty and use a VirtualPathProvider to find localized views. It takes the requested view path and modifies it when checking after the file. It returns a localized virtual file if found:

public pseudoclass MyFileProvider : VirtualPathProvider
{

    bool FileExists(string requestedPath)
    {
        if (IsLocalizedView(requestedPath))
          return true;
       return base.FileExists(requestedPath);
    }

    bool IsLocalizedView(string requestedPath)
    {
        var uri = requestedUri.AddLocaleByMagic("sv");
        if (FileExistsInternal(uri))
          return true;
    }

    //some more stuff to return the actual file
}

问题是,我得到了以下异常:

The problem is that I get the following exception:

在返回的VirtualPathProvider用VirtualPath一个VirtualFile对象设置为/Views/Shared/_Layout.sv.cshtml而不是预期的'/Views/Shared/_Layout.cshtml'的。

The VirtualPathProvider returned a VirtualFile object with VirtualPath set to '/Views/Shared/_Layout.sv.cshtml' instead of the expected '/Views/Shared/_Layout.cshtml'.

当然,我可以伪造的文件路径,但这样会产生与缓存和不同的本地化问题。对吧?

Sure, I could fake the file path, but that would produce problems with caching and different localizations. Right?

任何人有一个更好的方法能够创建本地化的看法?我不想用了同样的观点,但有资源字符串代替。这些观点是如此可怕,他们几乎让我哭,因为他们是如此难以阅读。

Anyone got a better way to be able to create localized views? I do not want to use the same view but with resource strings instead. Such views are so horrible that they almost makes me cry because they are so hard to read.

如果你还没有明白我在寻找:

If you still haven't understood what I'm looking for:

/Views/User/Details.sv.cshtml

/Views/User/Details.sv.cshtml

Hejsan @Model.FirstName

Detta är en lite rolig text på svenska.

/Views/User/Details.en.cshtml

/Views/User/Details.en.cshtml

Hello @Model.FirstName

This is a test on english.

控制器

Controller

public ActionResult Details()
{
  return View(new User()); //should automagically use a swedish or english view
}

我希望能够无需在每个请求手工做任何切换视图(使用的CurrentCulture局部的)。

I want to be able to switch views (to a localized one using CurrentCulture) without having to do anything manually at each request.

推荐答案

您可以写视的CurrentCulture返回意见的自定义视图引擎。
这方面的一个很好的例子可以在斯科特Hanselman的博客帖子,这确实返回移动浏览发现如果请求已经由移动设备制造

You can write a custom ViewEngine which returns views depending on the CurrentCulture. A nice example of this can be found at Scott Hanselman's blog post, which does return mobile Views if request has been made by a mobile device

这篇关于用剃刀本地化意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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