更改 ResourceManager(使其可更新)

发布于 2024-12-10 09:20:35 字数 654 浏览 0 评论 0原文

我有一个 MVC 3 (Razor) 项目,为了本地化,我们使用强类型资源。 我们希望能够更新“在线”已有的翻译。这意味着应该可以在网站上编辑翻译。 (例如,如果网址中存在“translateLanguage=on”等参数)基本上,使用当前解决方案不可能做到这一点,因为如果资源已更改,则必须重新编译。

当然,我们可以编写自己的资源管理器来使用数据库,但是这样我们就必须将所有翻译重写到数据库中,这将非常耗时。这也意味着我们必须更改所有代码以反映这个“新”资源管理器。

很难在所有事情上实施它。现在,我们可以在属性中使用它 例如

[Required(ErrorMessageResourceType = typeof(_SomeResource), ErrorMessageResourceName = "SomeResouceElement") 
  SomeProperty

以及代码:

 string translatedResource = _SomeResource.SomeResourceElement;

您能给我提供一些如何在 mvc 3 中执行此操作的信息吗?

I have a project in MVC 3 (Razor) For localization we are using Strongly typed resources.
We want to have possibility to update translation that already exist "on-line". It means, that it should be possible to edit translation on the website. (e.g. If in the url there is parameter like "translateLanguage=on") Basically, it is not possible to do that with current solution, because if resource has been changed, then it must be recompiled.

Of course we can write our own Resource Manager that will be using a database, but then we would have to rewrite all of our translations to the database and that would be time consuming. It would also mean that we would have to change all of our code to reflect this "new" resource manager.

It would be hard to implement it in all things. Now, we can use it in attributes
e.g.

[Required(ErrorMessageResourceType = typeof(_SomeResource), ErrorMessageResourceName = "SomeResouceElement") 
  SomeProperty

As well as in code:

 string translatedResource = _SomeResource.SomeResourceElement;

Could you provide me with some information how to do this in mvc 3?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神仙妹妹 2024-12-17 09:20:35

一般来说,资源文件由两部分组成:xml + 自动生成的cs代码。如果您打开资源设计器文件,您将看到

 /// <summary>
        ///   Looks up a localized string similar to About project.
        /// </summary>
        public static string about_project {
            get {
                return ResourceManager.GetString("about_project", resourceCulture);
            }
        }

所以您可以做什么,您可以使用 ResourceManager.GetString("Key")

Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
            var t = Resources.ResourceManager.GetResourceSet(new CultureInfo(cultureName), true, true);

为了使其更智能,您可以重写 BaseView

public abstract class ViewBase<TModel> : System.Web.Mvc.WebViewPage<TModel>
{
    public string GetTranslation(string key)
    {
        return _rManager.GetString(key);
    }

    private ResourceManager _rManager;
    protected ViewBase()
    {
        _rManager = Resources.ResourceManager.GetResourceSet(new CultureInfo(cultureName), true, true);
    }


}

然后您将能够在您的剃刀视图中使用 GetTranslation (要运行这个基本视图需要从 Views 文件夹修改 web.config)

,然后编辑 xml 后就可以访问资源数据。

Generally resource file consists of two parts xml + autogenerated cs code. If you open resource designer file you will see

 /// <summary>
        ///   Looks up a localized string similar to About project.
        /// </summary>
        public static string about_project {
            get {
                return ResourceManager.GetString("about_project", resourceCulture);
            }
        }

So what you can do you can use ResourceManager.GetString("Key")

Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
            var t = Resources.ResourceManager.GetResourceSet(new CultureInfo(cultureName), true, true);

To make it more smart you can rewrite BaseView

public abstract class ViewBase<TModel> : System.Web.Mvc.WebViewPage<TModel>
{
    public string GetTranslation(string key)
    {
        return _rManager.GetString(key);
    }

    private ResourceManager _rManager;
    protected ViewBase()
    {
        _rManager = Resources.ResourceManager.GetResourceSet(new CultureInfo(cultureName), true, true);
    }


}

And then you will be able to use GetTranslation in your razor view (To run this base view you need to modify web.config from Views folder)

And then you will be able after editing xml access to resource data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文