简体   繁体   English

ASP.NET本地化:在不重启应用程序的情况下更改资源?

[英]ASP.NET localization: Changing resources without restarting the application?

I have the requirement that the end-user can change localized resources and the changes should be visible in the application without the need to restart the application. 我要求最终用户可以更改本地化资源,并且应用程序中应该可以看到更改,而无需重新启动应用程序。

Update to clarify the scenario: 更新以澄清方案:
I am talking about changing the localized resources at runtime. 我说的是在运行时更改本地化资源。 Lets say I have a typo in the german translation of a page. 可以说我在页面的德语翻译中有一个拼写错误。 Then some admin-user should have the possibility to change that typo at runtime. 然后一些管理员用户应该有可能在运行时更改该错字。 There should be no need for a redeployment or restart in order for this change to be reflected in the UI. 应该不需要重新部署或重新启动,以便在UI中反映此更改。

I am using ASP.NET MVC3. 我正在使用ASP.NET MVC3。

What options do I have? 我有什么选择?

I have been looking into writing a custom ResourceProvider that loads resources from the database. 我一直在研究编写一个从数据库加载资源的自定义ResourceProvider
This seems not too much effort, however so far I pointed out two drawbacks: 这似乎没有太大的努力,但到目前为止我指出了两个缺点:

  • It is not working with the DataAnnotations that are used for convenient validation in MVC3 (DataAnnotations work with a ErrorMessageResourceType parameter, which only works with compiled resources) 它不能用于在MVC3中方便验证的DataAnnotations(DataAnnotations使用ErrorMessageResourceType参数,该参数仅适用于已编译的资源)
  • We basically have to provide our own tooling around managing resources (like translating etc.) which is a pity, since there are a lot of tools for this that work with resx-files. 我们基本上必须提供我们自己的管理资源(如翻译等)的工具,这是一个遗憾,因为有很多工具可以使用resx文件。

What are the other options? 还有什么其他选择? Would manipulation of the deployed resx-files at runtime be an option? 是否可以在运行时操作已部署的resx文件?
But I suspect that the application is automatically "restarted" when it detects those changes: I suspect ASP.NET realizes that the resx-files have changed, it then recycles the application-pool and compiles the new resx-files on the fly. 但我怀疑应用程序在检测到这些更改时会自动“重新启动”:我怀疑ASP.NET意识到resx文件已经更改,然后它会回收应用程序池并动态编译新的resx文件。
Is this correct? 它是否正确? Is there any way around this? 有没有办法解决?

I have not yet looked into compiling the resources into satellite assemblies before deployment. 我还没有考虑在部署之前将资源编译成附属程序集。 Is this even a recommended scenario for web applications? 这甚至是Web应用程序的推荐方案吗?
But even with compiled satellite assemblies I suspect that ASP.NET restarts the application, when those assemblies are changed on the fly. 但即使使用已编译的附属程序集,我怀疑ASP.NET会在动态更改这些程序集时重新启动应用程序。 Is this correct? 它是否正确?

I would be interested in any experience in how the original requirement can be satisfied? 我对如何满足原始要求的经验感兴趣? And I would be interested in any comments about the options I have mentioned above. 我会对我上面提到的选项的任何评论感兴趣。

DataAnnotations accept a ErrorMessageResourceType which tells the ValidationAttrributes where to access resources. DataAnnotations接受一个ErrorMessageResourceType ,它告诉ValidationAttrributes访问资源的位置。 You can pass this as follows: 您可以按如下方式传递:

[Required(
    ErrorMessageResourceType = typeof(DynamicResources), 
    ErrorMessageResourceName = "ResourceKey")]
public string Username { get; set; }

By creating a type for this parameter with static properties for each key you can create an implementation that loads resources from a database or other implementation. 通过为每个键创建具有静态属性的此参数的类型,您可以创建一个从数据库或其他实现加载资源的实现。 You could then combine this with a dynamic object for DRY and move the implementation into TryGetMember . 然后,您可以将其与DRY的动态对象组合,并将实现移至TryGetMember Potentially then use T4 templates to generate the statics from your database at compile time, ending up with this: 然后可能在编译时使用T4模板从数据库生成静态,最后得到:

public class DynamicResources : DynamicObject
{
    // move these into partial and generate using T4
    public static string MyResource
    {
        get { return Singleton.MyResource; }
    }

    public static string MyOtherResource
    {
        get { return Singleton.MyOtherResource; }
    }

    // base implementation to retrieve resources
    private static dynamic singleton;

    private static dynamic Singleton
    {
        get { return singleton ?? (singleton = new DynamicResources()); }
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        // some logic here to look up resources
        result = GetResourceKeyFromDatabase(binder.Name);
        return true;
    }
}

Of course it would be perfect if resources weren't static properties. 当然,如果资源不是静态属性,那将是完美的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net中的应用程序本地化 - Application Localization in asp.net asp.net mvc应用程序无故每秒重新启动 - asp.net mvc application restarting every second without reason 尽管更改了CurrentThread.CurrentCulture,ASP.NET MVC 5本地化资源仍会冻结并且不会更改语言 - ASP.NET MVC 5 localization resources freeze and do not change language despite changing CurrentThread.CurrentCulture Asp.net 多语言网络应用程序 - 本地化 - Asp.net multilingual web application - localization 用于ASP.NET Web应用程序的TraceSource的本地化 - Localization of the TraceSource for ASP.NET web application Asp.Net应用程序中的jQuery DataTable本地化 - Jquery DataTable localization in Asp.Net application 在不重新启动应用程序的情况下向asp.net mvc应用程序添加dll引用 - Adding dll reference to asp.net mvc application without restarting application asp.net本地化资源显式本地化未加载本地化文件 - asp.net Localized Resources Explicit Localization not loading localized files ASP.NET Web表单资源和本地化的棘手问题 - Tricky problem with ASP.NET web forms resources and localization 在香草ASP.NET中进行本地化的最佳实践或资源? - Best practices or resources for Localization in vanilla ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM