[英]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: 这似乎没有太大的努力,但到目前为止我指出了两个缺点:
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.