开发者

Message Properties File

开发者 https://www.devze.com 2023-03-17 05:22 出处:网络
Is there a VB equivalent to org.springframework.context.support.ResourceBundleMessageSource package in开发者_开发问答 java?

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消