动态添加文本MessageFormat

MessageFormat动态文件格式化.

MessageForamt可以对一个模板中的信息进行动态赋值.

1.MessageFormat使用

            MessageForamt.format(String pattern,Object... params);

2.说明一下关于动态文本中的占位符?

            例如:{0} is required 

            1.注意占位符只能使用{0}---{9}之间的数值.

            2.关于占们符的格式

            {argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号

            {argumentIndex,formatType}: 参数的格式化类型

            {argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。

扫描二维码关注公众号,回复: 1744682 查看本文章

            formatType可以取的值有:number date time

            formatStyle可以取的值有

                    number类型可以取:integer currency  percent 

                    date类型可以取的:short medium  full long

                    time类型可以取的:short medium  full long


首先修改Message_en_US.properties的配置文件的内容如下,加了一个占位符{0}.

title=LOGIN WINDOM
username=USERNAME
password=PASSWORD
submit=LOGIN
login.error={0} is required

再创建一个MessageFormatTest的类.

package cn.itcast.i18n;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Test;

public class MessageFormatTest {

	public static void main(String[] args) {
		ResourceBundle bundle = ResourceBundle.getBundle("message", Locale.US);

		String pattern = bundle.getString("login.error");
		//String value = MessageFormat.format(pattern, "username", "ok");

		System.out.println(pattern);
	}
}
将会打印出.

若释放掉注释的那句话,将会用 username 代替{0},并且打印 System.out.println(value); 


下面是对字符串的动态添加.

package cn.itcast.i18n;

import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;

import org.junit.Test;
public class MessageFormatTest {

	public static void main(String[] args) {
		ResourceBundle bundle = ResourceBundle.getBundle("message", Locale.US);

		String pattern = bundle.getString("login.error");
		String value = MessageFormat.format(pattern, "username", "ok");

		System.out.println(value);
	}

	@Test
	public void fun1() {

		// At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and
		// caused $1000000 of damage

		String msg = "At {0,time,medium} on {0,date,long}, a hurricance destroyed {1,number,integer} houses and caused {2,number,currency} of damage";

		Date date = new Date();

		// String value=MessageFormat.format(msg, date,99,1000000);

		// System.out.println(value);

		MessageFormat mf = new MessageFormat(msg, Locale.US);

		String value = mf.format(new Object[] { date, 99, 1000000 });

		System.out.println(value);
	}

	@Test
	public void fun2() {

		// At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and
		// caused $1000000 of damage

		String msg = "At {0,time,short} on {0,date,long}, a hurricance destroyed {1,number,integer} houses and caused {2,number,currency} of damage";

		Calendar c = Calendar.getInstance();
		c.set(1998, 6, 3, 12, 30,0);

		Date date = c.getTime();

		MessageFormat mf = new MessageFormat(msg, Locale.US);

		String value = mf.format(new Object[] { date, 99, 1000000 });

		System.out.println(value);
	}
}

第一个是打印实时时间.

第二个是打印自定义事件.

猜你喜欢

转载自blog.csdn.net/Superman___007/article/details/80792656