Localization: How to allow the user to define custom resources without compiling?
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?
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.
精彩评论