關於訊息格式化的方法

來源:互聯網
上載者:User

學習C語言的時候,很不適應它的print方法來輸出資訊,後來覺得很有意思。因為它把參數按照適當的位置放到了我們期待的位置。

後來寫訊息的時候,發現很多訊息都是相似的,然後就想用一個模板來,然後我只需要變化的資訊當做參數傳遞過去。後來查了一下,java裡有很好的支援。

接下來,總結一下我們經常用到的一些訊息格式化的方法,在項目開發中可以嘗試使用。

1. java實現

/* * String java.text.MessageFormat.format(String pattern, Object... arguments) * 格式化資訊,並將後面的參數依次填入指定位置。 * 注意:預留位置計數從“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) * 實現輸出6位元字字串,不足位用“0”填充 * format的規則參照:API中Formatter類--格式字串文法 */public void formatString() {    String format = "%06d";    String result = String.format(format, 30);    System.out.println(result);}/*~!output:000030*/

2. struts 2應用

a. 配置國際化檔案
   方法一:struts.xml配置
    <constant name="struts.custom.i18n.resources" value="message"></constant>
   方法二:struts.properties配置
    struts.custom.i18n.resources=message
b. message_zh_CN.properties配置
   temp={0}data{1}
c. 頁面中使用
    <s:text name="temp">
        <s:param value="%{'kkkk'}"/>
        <s:param>kllll</s:param>
    </s:text>

!關於<s:param>標籤的使用
struts2的s:param標籤主要有兩個屬性name與value, 若想在value屬性中輸入直接量,則可以這樣寫:<s:param name="some" value="%{'user'}"/>, 也可以這樣寫:<s:param name="some">user</s:param>。但如果直接賦值,這個值不是由Action動態產生的,而是自己指定的一個字串,則只能用後者。

3. javascript實現

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];    });};

實現了格式化訊息。

最近,在網上看到了jQuery.i18n.properties來實現國際化,發現很不錯。昨天學習了一下。

// 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', //使用資源檔名稱    path:'js/i18n/', //資源檔所在路徑    mode:'both',    //調用方式,可用值‘vars’ (default) 變數方式, ‘map’map方式 or ‘both’兩者均支援    language:'en_US', //語言    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         // 通過map調用        jQuery.i18n.prop('msg_hello');        // Accessing a value with placeholders through the map         // 帶參數Map調用,john會替換相應參數,支援多個參數        jQuery.i18n.prop('msg_complex', ['John']);        // Accessing a simple value through a JS variable        // 通過變數調用        alert(msg_hello +' '+ msg_world);    }});

通過$.i18n.prop('key');方式擷取資訊,並將資訊顯示。顯然這是一個公用的方法,當我們想對其進行進一步封裝的時候,遇到了一個問題,就是變長參數傳遞給i18n外掛程式的時候,原因在於無法確定參數的數量。當然,在js中,對於每一個function,參數其實都是arguments一個數組,通過這一點,我對其做了一些修改,手動的處理了一下參數列表。

$.i18n.prop = function(key /* Add parameters as function arguments as necessary  */) {        // 詳細資料參考源檔案        …………        // 這裡是我添加的代碼,如果參數是數組,則將其合并到參數數組中if (arguments.length == 2) {var params = arguments[1];if ($.isArray(params)) {arguments.length = 1;$.merge(arguments, params);}}                …………}

以上,是對於參數格式化的一些學習~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.