[英]How to localize Error Messages for a Custom Action in wix and DTF?
I have written a custom action in DTF and would like to generate localized error messages for it. 我在DTF中编写了一个自定义操作,并希望为它生成本地化的错误消息。 Until now, I have gone about this the following way: The custom action includes a method which displays a message that has been defined in the string table like this:
到目前为止,我已经通过以下方式解决了这个问题:自定义操作包含一个方法,该方法显示已在字符串表中定义的消息,如下所示:
public static string getError(Session session, short errorNo)
{
Database db = session.Database;
string errorMessage = (string)db.ExecuteScalar("SELECT `Message` FROM `Error` WHERE `Error` = {0}", errorNo);
db.Close();
return errorMessage;
}
To make use of this error message, an Error
element has to be created under the UI
element like this: 要使用此错误消息,必须在
UI
元素下创建一个Error
元素,如下所示:
<UI>
<Error Id="42042">!(loc.Error42042)</Error>
</UI>
The actual error message can then be stored in the localization variable Error42042
like this: 然后,实际的错误消息可以存储在本地化变量
Error42042
如下所示:
<String Id="Error42042">My localized Error Message.</String>
This approach works quite well although it has one serious limitation: It won't replace properties in the strings. 虽然它有一个严重的限制,但这种方法效果很好:它不会替换字符串中的属性。 So the string
所以字符串
<String Id="Error42043">[MY_PROPERTY] is broken</String>
will be displayed verbatim as [MY_PROPERTY] is broken
. 当
[MY_PROPERTY] is broken
将逐字显示。
Am I required to extend my getError
method to substitute properties manually (ie with the help of regular expressions or some other text substitution technique)? 我是否需要扩展我的
getError
方法来手动替换属性(即在正则表达式或其他一些文本替换技术的帮助下)?
Is there a predefined function in the wix source code I could use for property substitution? 我可以使用wix源代码中的预定义函数进行属性替换吗?
Is this the wrong approach altogether? 这完全是错误的做法吗?
I have seen strings in the form of 我已经看到了形式的字符串
<String Id="Error2227">Param: [2]. Invalid param "[3]" in action: [4].</String>
in other installation packages. 在其他安装包中。 Being able to pass parameters to the String table like this would also be a feasible approach to tackle this problem, but I have not figured out how to pass parameters to the string table.
能够像这样将参数传递给String表也是解决这个问题的可行方法,但我还没想出如何将参数传递给字符串表。 Do you know how to do that?
你知道怎么做吗?
Thanks to the comment by PhilDW I have found the DTF solution and it is amazingly simple. 感谢PhilDW的评论,我找到了DTF解决方案,这非常简单。 Using the
Session.Format
method, which is a DTF wrapper around MsiFormatRecord , one can simply replace properties in localized strings. 使用
Session.Format
方法,它是围绕MsiFormatRecord的DTF包装器,可以简单地替换本地化字符串中的属性。 Building on the example above, the getError method has to be extended into the following: 基于上面的示例,getError方法必须扩展为以下内容:
public static string getError(Session session, short errorNo)
{
Database db = session.Database;
string errorMessage = (string)db.ExecuteScalar("SELECT `Message` FROM `Error` WHERE `Error` = {0}", errorNo);
db.Close();
errorMessage = session.Format(errorMessage);
return errorMessage;
}
Now, properties in the localized strings can be substituted by using the Formatted syntax like this: 现在,可以使用Formatted语法替换本地化字符串中的属性,如下所示:
<String Id="Error42043">{[MY_PROPERTY]} is broken</String>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.