关于消息格式化的方法

学习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);
		}
	}
	        
        …………
	
}

以上,是对于参数格式化的一些学习~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值