消息属性文件

发布于 2024-11-18 06:05:33 字数 582 浏览 0 评论 0原文

java中是否有相当于org.springframework.context.support.ResourceBundleMessageSource包的VB?

我这个问题的重点不是Spring。我想知道是否有一种方法可以拥有一个消息属性文件,我可以像使用 hat java 包一样传递变量。

这是我想做的一个例子。

在属性文件中具有以下行:

success.message = Successfully created document with Trans No. {0}

在源代码中具有以下行:

ResourceBundleMessageSource.getMessage("success.message",new String[] {transObject.getTransId()}, null));

此代码使用属性文件查找 success.message 并从 getTransId() 传递变量。

我想这样做以集中所有我的错误消息。并且在我的代码中没有硬编码消息。

VB中有某种等价的东西吗?

Is there a VB equivalent to org.springframework.context.support.ResourceBundleMessageSource package in java?

I am not focusing on Spring with this question. I want to know if there is a way to have a message properties file that I can pass variables like you can with hat java package.

Here is an example of what I would like to do.

In a properties file have the following line:

success.message = Successfully created document with Trans No. {0}

In source code have this line:

ResourceBundleMessageSource.getMessage("success.message",new String[] {transObject.getTransId()}, null));

This code uses the properties file finds success.message and passes the variable from getTransId().

I want to do this to centralize all my error messages. and not have hard coded messages throughout my code.

Is there some kind of equivalent in VB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走过海棠暮 2024-11-25 06:05:33

一种解决方案是使用资源文件。通过右键单击项目并选择项目属性来添加资源。然后单击资源并添加新资源。

我们从问题中的例子开始工作:
对于资源,我们不能遵循如上所示的相同命名约定。我们需要替换“.”带“_”即: success.message ->成功消息
资源文件不允许使用“.”在键名称中。

接下来我们需要将消息写入资源文件中。
“{0}已成功提交文件。”
我们使用 {0}....{x} 作为变量的占位符。

资源选项卡中的第一行应如下所示

success_message | {0} successfully submitted the file.

替换占位符的函数应如下所示:

Public Shared Function messageRetriver(ByVal message As String, ByVal variables As String()) As String
    Dim i As Integer
    Dim pattern As String
    For i = 0 To variables.Length - 1
        pattern = "\x7B" & i & "\x7D"
        Dim myRegex As New Regex(pattern)
        message = myRegex.Replace(message, variables(i))
    Next
    Return message
End Function

现在,在代码中,您所要做的就是调用此函数并传递资源和变量的字符串数组。

Utility.messageRetriver(My.Resources.success_message, {"My Program"})

那应该可以解决问题。
我使用作为编译此信息的资源。

One solution is to use resource files. Add a resource by right clicking on the project and selecting project properties. Then click on resources, and add a new resource.

We work from the example in the question :
With resources we cannot follow the sam naming convention as you see above. We need to replace the '.' with a '_' ie: success.message -> success_message
Resource files do not allow the '.' in the key name.

Next we need to the message into the resource file.
"{0} successfully submitted the file."
We use {0}....{x} as place holders for variables.

The first line in the resource tab should look like this

success_message | {0} successfully submitted the file.

The function to do the replacing of the place holders should look like this:

Public Shared Function messageRetriver(ByVal message As String, ByVal variables As String()) As String
    Dim i As Integer
    Dim pattern As String
    For i = 0 To variables.Length - 1
        pattern = "\x7B" & i & "\x7D"
        Dim myRegex As New Regex(pattern)
        message = myRegex.Replace(message, variables(i))
    Next
    Return message
End Function

Now in your code all you have to do is call this function passing the resource, and the string array of variables.

Utility.messageRetriver(My.Resources.success_message, {"My Program"})

That should do the trick.
I used this as a resource to compile this information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文