java多语言

在eclipse plugin编程中,可以直接用继承NLS这个类,MLR exnteds NLS,然后设定多个static Field, 其中Field的名字和.properties中的key值相等,用户就可以直接访问子MLR中的Field就可以访问多语言了,不过MLR中要调用NLS.initializeMessages(resourceBunlde, MLR.class) 注意resourceBoundle文件要和MLR在相同包下。多语言文件中也可以用占位符,调用NLS.bind为各个占位符里填值。

ChoiceFormat: 选择Format, 两个构造参数:limits & formats, limits和formats个数相等,一一对应 其中limits中的double数据要按升序排列,这些double数据构成N+1个半开区间,如limits为 [1, 2, 3],那么{Double.NEGATIVE_INFINITY,1}, [1,2} [2,3}, [3, Double.POSITIVE_INFINITY} 在ChoiceFomat.format(double)格式化时根据传入进来的double,落在哪个半开区间则调用相对应的formats。
下面是jdk API中的两个例子:
double[] filelimits = {0,1,2};
		 String[] filepart = {"are no files","is one file","are {2} files"};
		 ChoiceFormat fileform = new ChoiceFormat(filelimits, filepart);
		 MessageFormat pattform = new MessageFormat("There {0} on {1}");
		 Format[] testFormats = {fileform, null, NumberFormat.getInstance()};
		 pattform.setFormats(testFormats);
		 Object[] testArgs = {null, "ADisk", null};
		 for (int i = 0; i < 14; ++i) {
		     testArgs[0] = new Integer(i);
		     testArgs[2] = testArgs[0];
		     System.out.println(pattform.format(testArgs));
		 }

pattform.setFormats(testFormats);在格式化时对应的占位符调用对应的testFormats,其中第一个占位符的Format是ChoiceFomat,因此传入的参数直接影响使用filepart中的哪个值,由于testArgs中的第二个值是ADisk固定值,那么Format为null, 在filepart有用到{2},因此定义一个NumberFormat。

ChoiceFormat fmt = new ChoiceFormat(
			      "-1#is negative| 0#is zero or fraction | 1#is one |1.0<is 1+ |2#is two |2<is more than 2.");
			 System.out.println("Formatter Pattern : " + fmt.toPattern());

			 System.out.println("Format with -INF : " + fmt.format(Double.NEGATIVE_INFINITY));
			 System.out.println("Format with -1.0 : " + fmt.format(-1.0));
			 System.out.println("Format with 0 : " + fmt.format(0));
			 System.out.println("Format with 0.9 : " + fmt.format(0.9));
			 System.out.println("Format with 1.0 : " + fmt.format(1));
			 System.out.println("Format with 1.5 : " + fmt.format(1.5));
			 System.out.println("Format with 2 : " + fmt.format(2));
			 System.out.println("Format with 2.1 : " + fmt.format(2.1));
			 System.out.println("Format with NaN : " + fmt.format(Double.NaN));
			 System.out.println("Format with +INF : " + fmt.format(Double.POSITIVE_INFINITY));

各个limit&format之间用|分隔,每个limit&format内部用#或<分隔

你可能感兴趣的:(java)