Message formatting Method

Source: Internet
Author: User

When I learned the C language, I did not adapt to its print method to output information. Later I found it interesting. Because it places the parameter in the desired position according to the appropriate position.

Later, when writing a message, I found that many messages are similar, and then I wanted to use a template. Then I only needed the changed information to pass it as a parameter. Later, I checked that Java has good support.

Next, we will summarize some of the frequently used message formatting methods that can be used in project development.

1. Java implementation

 
/** String java. Text. messageformat. Format (string pattern, object... arguments) * format information and enter the following parameters in the specified position in sequence. * Note: The placeholder count starts from "0" */Public void formatmessage () {string pattern = "{0}-{1}-{2}"; string result = messageformat. format (pattern, 1, "two", '3'); system. out. println (result );}/*~! Output: 1-two-3 */

/** String Java. lang. string. format (string format, object... ARGs) * Outputs a six-digit numeric string. For the rule of filling * format with "0" for the missing digits, see formatter class in API-Format String syntax */Public void formatstring () {string format = "% 06d"; string result = string. format (format, 30); system. out. println (result );}/*~! Output: 000030 */

2. Struts 2 Application

A. Configure the International File
Method 1: struts. xml configuration
<Constant name = "struts. Custom. i18n. Resources" value = "message"> </constant>
Method 2: struts. properties configuration
Struts. Custom. i18n. Resources = message
B. message_zh_cn.properties Configuration
Temp = {0} data {1}
C. Use it on the page
<S: Text name = "Temp">
<S: Param value = "% {'kkkk '}"/>
<S: param> kllll </S: param>
</S: Text>

! Use of the <s: param> label
The S: Param tag of struts2 mainly has two attributes: Name and value. If you want to enter the direct quantity in the value attribute, you can write as follows: <s: param name = "some" value = "% {'user'}"/>, you can also write: <s: Param name = "some"> User </s: param>. However, if the value is directly assigned, this value is not dynamically generated by action, but a character string specified by itself. Only the latter can be used.

3. Javascript implementation

String. format = function (SRC) {If (arguments. length = 0) return NULL; var ARGs = array. prototype. slice. call (arguments, 1); Return SRC. replace (/\ {(\ D +) \}/g, function (M, I) {return ARGs [I] = undefined? Null: ARGs [I] ;}) ;};

Message formatting is implemented.

Recently, I saw jquery. i18n. properties on the Internet to achieve internationalization. I learned about it yesterday.

// This will initialize the plugin // and show two dialog boxes: one with the text "ol áworld" // and other with the text "Good Morning John! "Jquery. i18n. properties ({Name: 'message', // use the resource file name path: 'js/i18n/', // path of the resource file: 'both ', // call method. The value 'vars' (default) can be used as a variable. The 'map' map method or 'both' both support language: 'en _ us ', // language callback: function () {// We specified mode: 'Both 'So translated values will be // available as JS vars/functions and as a map // accessing a simple value through the map // call jquery through map. i18n. prop ('msg _ hello'); // accessing a value with placeholders through the map // call with the map parameter. John replaces the corresponding parameter and supports multiple parameters of jquery. i18n. prop ('msg _ complex ', ['john']); // accessing a simple value through a JS variable // Call Alert (msg_hello + ''+ msg_world) through variables );}});

You can use $. i18n. Prop ('key'); to obtain information and display it. Obviously, this is a public method. When we want to further encapsulate it, we encounter a problem: when the variable length parameter is passed to the i18n plug-in, the reason is that the number of parameters cannot be determined. Of course, in JS, parameters for each function are actually an array of arguments. Through this, I made some modifications to them and manually processed the parameter list.

 
$. I18n. Prop = function (key/* Add parameters as function arguments as necessary */) {// For details, refer to the source file ............ // Here is what I addedCodeIf the parameter is an array, It is merged into the parameter array if (arguments. length = 2) {var Params = arguments [1]; if ($. isarray (Params) {arguments. length = 1; $. merge (arguments, Params );}}............}

The preceding section describes how to format parameters ~

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.