本地化:如何让用户无需编译即可定义自定义资源?
在我们的应用程序中,我们有一个数据项集合,每个数据项都有一个 DisplayedName
属性。该属性应该本地化,即它应该以用户选择的语言显示。因此,另一个属性 DisplayedNameResourceKey
指定 DisplayedName
属性应返回哪个资源。
在简化的代码中,这意味着如下:
public string DisplayedName
{
get
{
return MyResources.ResourceManager.GetObject(this.DisplayedNameResourceKey);
}
}
public string DisplayedNameResourceKey
{
get;
set;
}
现在,问题是:用户应该能够编辑这些项目,包括 DisplayedName
,或更准确地说是 DisplayedNameResourceKey
。不仅如此,用户还应该能够以某种方式定义他可以引用的新资源。也就是说,他可以从一组预定义的资源(一些常用名称)中进行选择,也可以定义一个自定义资源,然后用户也需要对其进行本地化。
但是,用户无法在运行时且未经编译的情况下将自定义资源添加到 MyResources
。因此,需要另一种方法。它不必是一种极其用户友好的方式(例如不需要 UI),因为这通常由我们的服务工程师完成。
我正在考虑使用包含资源键对和相应翻译的 txt 或 csv 文件。每种语言都会在预定义位置存在一个单独的文件。但我对这个想法并不是很满意,因为它需要大量的工作来解决资源问题。
有谁知道针对这种情况的好方法?
In our application, we have a collection of data items, each with a DisplayedName
property. This property should be localized, i.e. it should be displayed in the language selected by the user. Therefore, another property, DisplayedNameResourceKey
, specifies which resource should be returned by the DisplayedName
property.
In simplified code this means something like this:
public string DisplayedName
{
get
{
return MyResources.ResourceManager.GetObject(this.DisplayedNameResourceKey);
}
}
public string DisplayedNameResourceKey
{
get;
set;
}
Now, the problem is: The user should be able to edit these items including the DisplayedName
, or more precisely the DisplayedNameResourceKey
. And not only this, but the user should also be able to somehow define new resources which he can then reference. That is, he can either choose from a predefined set of resources (some commonly used names), or define a custom resource which then needs to be localized by the user as well.
However, the user cannot add custom resources to MyResources
at runtime and without compiling. Therefore, another approach is needed. It does not have to be an extremely user-friendly way (e.g. UI is not required) because this will typically be done by our service engineers.
I was thinking about using a txt or csv file containing pairs of resource keys and the corresponding translations. A separate file would exist for every language at a predefined location. But I am not really satisfied with that idea because it involves a lot of work to resolve the resources.
Does anyone know a good approach for such a situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以考虑将资源放入数据库中。通过这种方式,您可以使其保持相当动态和快速。
例如,一个包含 3 列的表:资源键、语言、消息。您可能应该在资源键和语言列上创建复合索引以进行快速查找。
You could consider putting the resources in a database. In this way, you keep it fairly dynamic and fast.
E.g. a table with 3 columns: resource key, language, message. You should probably create a composite index on the resource key and language columns for fast lookup.