Format resource string to another?

What would be the best way to accomplish something like this?

Suppose I have the following pair of Resource lines.

 BadRequestParameter: Potential bad request aborted before execution. RequiredParameterConstraint: {0} parameter requires a value. {1} 

And suppose I want to set {1} to the second, to the value of BadRequestParameter . I could easily do this using string.Format . But now suppose I have many Resource lines, such as the second, all of which include another Resource line in them.
What would be the best way to code this? Does string.Format in every case really repeat everything I can do?

Update

I will try to explain myself better. These are the resource strings that I have:

 BadRequestParameter Potential bad request aborted before execution. EmptyVector Vectorized requests require at least one element. {0} OverflownVector Vectorized requests can take at most one hundred elements. {0} RequiredParamConstraint {0} parameter requires a value. {1} SortMinMaxConstraint {0} parameter value '{1}' does not allow Min or Max parameters in this query. {2} SortRangeTypeConstraint Expected {0} parameter Type '{1}'. Actual: '{2}'. {3} SortValueConstraint {0} parameter does not allow '{1}' as a value in this query. {2} 

I would like to avoid writing a line to BadRequestParameter at the end of each of these lines. So I added the format at the end of these lines. The problem is that I would like to somehow automatically refer {x} to the BadRequestParameter to avoid having to make calls like

 string.Format(Error.EmptyVector, Error.BadRequestParameter); 
+4
source share
3 answers

I have many resource strings, such as the second, all of which include some other resource string.

Instead of storing ready-made formatted strings ready for use, you can store the source materials to create real-format strings and add code to expand them before use. For example, you can store strings as follows:

 BadRequestParameter: Potential bad request aborted before execution. SupportNumber: (123)456-7890 CallTechSupport: You need to call technical support at {SupportNumber}. RequiredParameterConstraint: {{0}} parameter requires a value. {BadRequestParameter} {CallTechSupport} 

Of course, passing these strings to string.Format as-is will not work. You need to parse these lines, for example using RegExp s, and find all instances where you have a word between curly braces, not a number. Then you can replace each word with your own serial number and create an array of parameters based on the names you find between the curly braces. In this case, you will get these two values ​​(pseudo-code):

 formatString = "{{0}} parameter requires a value. {0} {1}"; // You replaced {BadRequestParameter} with {0} and {CallTechSupport} with {1} parameters = { "Potential bad request aborted before execution." , "You need to call technical support at (123)456-7890." }; 

Note: Of course, recursion is required to create this parameters array.

At this point, you can call string.Format to create your last line:

 var res = string.Format(formatString, parameters); 

This returns a string in which resource strings are previously replaced for your subscribers:

 "{0} parameter requires a value. Potential bad request aborted before execution. You need to call technical support at (123)456-7890." 

Callers can now use this string to format without worrying about other resource values.

+1
source

Yes :-) if you do not want to use the helper method, which is shorter, but that would be just for the sake of convenience

 public static string f(string format, params object[] p) { return string.Format(format, p); } 
0
source

IF you treat the argument indicators {#} as wild cards, then why would it be useful for you to fill them inside your resource.

I see absolutely nothing wrong with

 String.Format(RequiredParamterConstraint, "something", BadRequestParameter); 
0
source

Source: https://habr.com/ru/post/1388625/


All Articles