A method to solve the problem of the failure of spring internationalization copywriting placeholder

Source: Internet
Author: User

Written in front: The next long period of time the article will mainly record some of the actual problems encountered in the project and corresponding solutions, in the corresponding code analysis will point to the problem, will not be irrelevant process code, interested readers can self-tracking. At the same time, we hope that we can share the experience in the comment area, so that everyone progress together!

Environment or version: Spring 3.2.3

Phenomenon: Using spring's own messagesource to handle the internationalization of the copy, the US state of the copy has some placeholder has not been replaced, the CN state of normal. The copy is as follows:

Tms.pallet.order.box.qty=the Total palletized boxes Quantity {0} doesn ' t match with the received boxes Quantity Beckham, please Double check!
tms.pallet.order.box.qty= total box number of pieces {0}, and the order receipt of the total number of cases of small shellfish inconsistencies. Please check!

Intuition: Is not the English copy is too long, spring processing when the length of the limit, think carefully think spring should not design such a hole.

Troubleshooting: Breakpoint Tracking Spring Source (entry: Messagesource's GetMessage method), and finally found a messageformat in such a section of the processing method:

  1. Indices for segments
  2. private static final int seg_raw = 0;
  3. private static final int seg_index = 1;
  4. private static final int seg_type = 2;
  5. private static final int seg_modifier = 3; Modifier or Subformat
  6. /**
  7. * Sets the pattern used by this message format.
  8. * The method parses the pattern and creates a list of subformats
  9. * For the format elements contained in it.
  10. * Patterns and their interpretation is specified in the
  11. * <a href= "#patterns" rel= "external nofollow" >class description</a>.
  12. *
  13. * @param pattern The pattern for this message format
  14. * @exception IllegalArgumentException If the pattern is invalid
  15. */
  16. @SuppressWarnings ("Fallthrough")//Fallthrough in switch is expected, suppress it
  17. public void Applypattern (String pattern) {
  18. Stringbuilder[] segments = new STRINGBUILDER[4];
  19. Allocate only Segments[seg_raw] here. The rest is
  20. Allocated on demand.
  21. Segments[seg_raw] = new StringBuilder ();
  22. int part = Seg_raw;
  23. int formatnumber = 0;
  24. Boolean inquote = false;
  25. int bracestack = 0;
  26. Maxoffset =-1;
  27. for (int i = 0; i < pattern.length (); ++i) {
  28. char ch = pattern.charat (i);
  29. if (part = = Seg_raw) {
  30. if (ch = = ' \ ') {
  31. if (i + 1 < pattern.length ()
  32. && Pattern.charat (i+1) = = ' \ ') {
  33. Segments[part].append (CH); Handle Doubles
  34. ++i;
  35. } else {
  36. Inquote =!inquote;
  37. }
  38. } else if (ch = = ' {' &&!inquote) {
  39. part = Seg_index;
  40. if (segments[seg_index] = = null) {
  41. Segments[seg_index] = new StringBuilder ();
  42. }
  43. } else {
  44. Segments[part].append (CH);
  45. }
  46. } else {
  47. if (inquote) {//Just copy quotes in parts
  48. Segments[part].append (CH);
  49. if (ch = = ' \ ') {
  50. Inquote = false;
  51. }
  52. } else {
  53. Switch (CH) {
  54. Case ', ':
  55. if (Part < Seg_modifier) {
  56. if (segments[++part] = = null) {
  57. Segments[part] = new StringBuilder ();
  58. }
  59. } else {
  60. Segments[part].append (CH);
  61. }
  62. Break
  63. Case ' {':
  64. ++bracestack;
  65. Segments[part].append (CH);
  66. Break
  67. Case '} ':
  68. if (Bracestack = = 0) {
  69. part = Seg_raw;
  70. Makeformat (i, formatnumber, segments);
  71. formatnumber++;
  72. Throw away other segments
  73. Segments[seg_index] = null;
  74. Segments[seg_type] = null;
  75. Segments[seg_modifier] = null;
  76. } else {
  77. --bracestack;
  78. Segments[part].append (CH);
  79. }
  80. Break
  81. Case ':
  82. Skip any leading space chars for Seg_type.
  83. if (part! = Seg_type | | segments[seg_type].length () > 0) {
  84. Segments[part].append (CH);
  85. }
  86. Break
  87. Case ' \ ':
  88. Inquote = true;
  89. Fall through, so we keep quotes in other parts
  90. Default
  91. Segments[part].append (CH);
  92. Break
  93. }
  94. }
  95. }
  96. }
  97. if (Bracestack = = 0 && part! = 0) {
  98. Maxoffset =-1;
  99. throw new IllegalArgumentException ("unmatched braces in the pattern.");
  100. }
  101. This.pattern = Segments[0].tostring ();
  102. }
Copy Code

The above code is a bit confusing, slightly peculiar, we mainly look at the first logical branch: Each of the internationalized copy template string to be processed to traverse the character, when the character is "'", the next character is judged whether "'", if it is "" " In a StringBuilder that has been processed, it is not inquote to true if the character does not ' {' and Inquote is false to reset the part to 0 and segments[seg_index]= If NULL, re-create the StringBuilder object, or continue stitching.

Cause Analysis:

    1. In conjunction with our configured English copy (where a total of two placeholders, before these two placeholders have a single quotation mark), according to the above spring's processing source, the actual processing will be: the string character-by-byte processing, one by one to the processed StringBuilder, when processing to ' {' , where part will be set to 1, and the StringBuilder type object will be referenced on the segments 1th storage bit, the program continues to process the following pending characters and continues stitching (see yourself part! = Seg_raw Logical Branch), until processing to '} ', the part is re-assigned to the other bits of the 0,sefgments are emptied, so continue to process the following string continue stitching, processing into single quotes, Inquote is set to True, the next stitch, no longer on the back of the "{" Do placeholder processing.
    2. There is no single quotation mark between the two placeholders in the Chinese copy, so the 2nd in the problem phenomenon is resolved, and the Chinese copy shows normal.

Solution:

From the source to see only one solution, {} The single quotation marks need to appear in pairs, our processing method is to modify the copy in order to:

Tms.pallet.order.box.qty=the Total palletized boxes Quantity {0} doesn "t match with the received boxes Quantity Beckham, please Double check!

Modifying a copy directly is not a good solution, it is best to rewrite the method before spring calls the Applypattern method to replace the single quotation mark with double quotation marks. Helpless Spring 3.2.3 version of the corresponding internationalization of the processing method all the way to the private, do not give you the opportunity to rewrite.

Check the relevant information that, in the Spring4.3.2 version of the Resourcebundlemessagesource class can be overridden by the Getstringornull method to achieve.

Long-term scenario: Upgrade the spring version of the project while using more new features.

http://www.ljhseo.com/
http://www.xyrjkf.net/
http://www.xyrjkf.cn/
http://www.xyrjkf.com.cn/
http://www.zjdygsi.cn/
http://www.zjdaiyun.cn/
http://www.jsdygsi.cn/
http://www.xyrjkf.top/
http://www.xyrjkf.com/
http://www.daiyunzj.cn/
http://ljhseo.com/
http://xyrjkf.net/
http://xyrjkf.cn/
http://xyrjkf.com.cn/
http://zjdygsi.cn/
http://zjdaiyun.cn/
http://jsdygsi.cn/
http://xyrjkf.top/
http://xyrjkf.com/
http://daiyunzj.cn/

A method to solve the problem of the failure of spring internationalization copywriting placeholder

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.