这是对可插入组件的国产化好的解决办法? [英] Is this a good solution for localisation of pluggable components?

查看:194
本文介绍了这是对可插入组件的国产化好的解决办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一个<一个href="http://stackoverflow.com/questions/1504363/how-should-i-localise-pluggable-components">question previously其中只有一个答案。我有一些时间来玩弄这个现在有一个计划,但希望一些反馈,如果它是一个好主意。

I asked a question previously which had only a single answer. I've had some time to play around with this now and have a plan, but want some feedback on if it is a good idea.

问题:

欲其中有一个名称(不变,用于识别组件)将其名字定位于正消耗它的应用程序,不污染的组件的模型与一个DisplayName特性的成分。所述组分可以存在于单独的DLL,并在运行时动态加载。

I want a component which has a name (invariant, used to identify the component) to have its name localised in the application that is consuming it, without polluting the model of the component with a DisplayName attribute. The component may exist in a separate dll and be dynamically loaded at runtime.

我的感觉是,组件DLL应负责提供本地化的名称(这似乎是很好的封装),但消耗组件的应用程序应该负责获取/使用本地化的名称(该组件有一个事实不同的名称显示目的不是组件的一个问题,但使用该组件的查看)的

my feeling is that the component dll should be responsible for providing the localised name (this seems like good encapsulation), but the application consuming the component should be responsible for getting/using the localised name (the fact that the component has a different name for display purposes is not a concern of the component, but of the 'view' using that component)

解决方法:

添加资源的组件DLL具有相同名称的组件类是在文件中添加一个字符串,用钥匙也就是组件的名称的资源。

Add a resource to the components dll with the same name as the file the component class is in. Add a string to the resource with a key which is the name of the component.

在应用程序中获取的本地化名称,如下所示:

In the application get the localised name like so:

ExternalObject obj = GetExternalObject ();            
ResourceManager manager = new ResourceManager (obj.GetType ());
string localisedName= manager.GetString (obj.Name);

这code可能会被封装在一个航向类,但传达的地步。这似乎工作,但它是一个好主意,或者是有这样做的更好/更标准的方式?

This code will probably be encapsulated in a Localiser class, but conveys the point. This seems to work, but is it a good idea, or is there a better/more standard way of doing this?

编辑:我要指出的是一件事,我不知道这个解决方案是资源必须在具有相同名称的类在文件中的.resx文件这使得它的工作,作为资源文件可以从类型名称来识别。这是一样的本地化形式似乎工作,使视觉工作室放的.resx为cs文件,而这一切看上去不错的子组件。但是,Visual Studio中,然后抛出一个警告(有关编辑的资源,是另一个项目的项目的一部分),如果我尝试编辑这个文件,这让我觉得,也许还有就是我应该做这方面的一些其他的方式。

I should point out that one thing that I'm not sure about with this solution is that the resources have to be in a .resx file that has the same name as the file that the class is in. This makes it work, as the resource file can be identified from the type name. This is the same as localisation for forms seems to work, and makes visual studio put the .resx as a 'sub-component' of the .cs file, which all seems nice. But visual studio then throws a warning (about editing a resource that is part of another project item) if I try and edit this file, which makes me think that perhaps there is some other way that I'm supposed to be doing this.

推荐答案

我觉得你有正确的想法,但有更好的方法来做到这一点。

I think you have the right idea, but there's better way to accomplish this.

presumably,你有一个接口,可插入组件实现。再说了,IPluggable:

Presumably, you have an interface that the pluggable component implements. Say, IPluggable:

interface IPluggable {
    ...
    string LocalizedName {get;}
    ...
}

这是你的主要二进制文件,加载可插入组装和使用反射创建IPluggable实例(我认为这正是 GetExternalObject()方法,你有一样),然后访问使用 LocalizedName 属性本地化名称。 的的IPluggable实施,创建一个的ResourceManager 并访问 LocalizedName 从的RESX可插拔的装配。

From your main binary, load the pluggable assembly and create the IPluggable instance using reflection (I assume that's what the GetExternalObject() method you have does) and then access the localized name using the LocalizedName property. Inside the IPluggable implementation, create a ResourceManager and access the LocalizedName from the resx of that pluggable assembly.

你所说的做得到的是行为的可插入组装好的封装 - 它负责提供您的本地化名称,但它选择这样做,没有你的男人程序假定一个的ResourceManager 可以创建访问本地化的名称。

What you get by doing is good encapsulation of behavior in the pluggable assembly - it is responsible for providing you the localized name, however it chooses to do it, without your man program assuming that a ResourceManager can be created to access a localized name.

这篇关于这是对可插入组件的国产化好的解决办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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