MessageFormat(动态问文本)
一.如果一个字符串中包含了多个与国际化相关的数据,可以使用MessageFormat类对这些数据进行批量处理。
例如:At 12:30 pm on jul 3,1998, a hurricance destroyed 99 houses and caused $1000000 of damage
• 以上字符串中包含了时间、数字、货币等多个与国际化相关的数据,对于这种字符串,可以使用MessageFormat类对其国际化相关的数据进行批量处理。
MessageFormat 类如何进行批量处理呢?
• MessageFormat类允许开发人员用占位符替换掉字符串中的敏感数据(即国际化相关的数据)。
• MessageFormat类在格式化输出包含占位符的文本时,messageFormat类可以接收一个参数数组,以替换文本中的每一个占位符。
二. 模式字符串:
• On {0}, a hurricance destroyed {1} houses and caused {2} of damage.
MessageFormat类
• MessageFormat(String pattern)
l 实例化MessageFormat对象,并装载相应的模式字符串。
• format(object obj[])
l 格式化输出模式字符串,参数数组中指定占位符相应的替换对象。
l format(new Object[ ]{date, new Integer(99), new Double(1E7) })
例题讲解:package com.hbsi.test;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
public class Demo3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//模式字符串
String pattern="At{0,time,full} on{0,date,long}, a hurricance destroyed {1,number} houses and caused {2,number,currency} of damage.";
MessageFormat mf=new MessageFormat(pattern,Locale.US);
//准备参数数组
Object [] objs={new Date(),new Integer(99),new Double(1e7)};
//执行格式化
String result=mf.format(objs);
System.out.println(result);
//加入到资源文件中
ResourceBundle bundle=ResourceBundle.getBundle("com.hbsi.resource.MyResource",Locale.ENGLISH);
result=bundle.getString("title");
//System.out.println(result);
MessageFormat mf1=new MessageFormat(result,Locale.ENGLISH);
System.out.println(mf.format(objs));
}
}
三.模式字符串与占位符
l 占位符有三种方式书写方式:
• {argumentIndex}: 0-9 之间的数字,表示要格式化对象数据在参数数组中的索引号
• {argumentIndex,formatType}: 参数的格式化类型
• {argumentIndex,formatType,FormatStyle}: 格式化的样式,它的值必须是与格式化类型相匹配的合法模式、或表示合法模式的字符串。
代码分析:
String pattern = "At {0, time, short} on {0, date}, a destroyed'\n'"
+ "{1} houses and caused {2, number, currency} of damage.";
MessageFormat msgFmt = new MessageFormat(pattern,Locale.US);
String datetimeString = "Jul 3, 1998 12:30 PM";
Date date = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.SHORT,Locale.US).parse(datetimeString);
String event = "a hurricance";
Object []msgArgs = {date, event, new Integer(99), new Double(1E7)};
String result = msgFmt.format(msgArgs);
System.out.println(result);
分享到:
相关推荐
java中MessageFormat格式化字符串的例子,包括NumberFormat,DateFormat,ChoiceFormat等
前端项目-messageformat,multipleformat和selectformat message和i18n工具-一个实现ICU标准的javascript。
讯息格式程序文本的经验和精妙性很重要。 Messageformat是一种用于处理应用程序中的复数形式和性别的机制。 由于它旨在支持包含的,因此它也可以带来更好的翻译。 此monorepo由构成我们的ICU MessageFormat的JS实现...
messageformat.net, .NET的ICU MessageFormat实现 用于. NET 的- 更好的用户界面字符串。 这是. NET 中ICU消息格式的实现。 有关该格式的官方信息,请转到: http://userguide.icu-project.org/formatparse/messages...
主要介绍了Java中的MessageFormat.format用法实例,本文先是讲解了MessageFormat的语法,然后给出了多个操作实例,需要的朋友可以参考下
主要介绍了Java利用MessageFormat实现短信模板的匹配,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这个插件依赖于 Alex Sexton 的库以及文本! 插入。 MessageFormat 库必须位于您的源代码树中的路径下,您将使用该路径作为别名,如下面的 RequireJS 配置示例所示。 RequireJS 配置 以下是您的 RequireJS 配置应...
消息格式包使用 messageformat.js 编译消息树。 输出中没有全局变量。安装 > npm install messageformat-bundle用法 var messageformatBundle = require ( 'messageformat-bundle' ) ;// Work in progress…应用程序...
messageformat.js 的代码镜像模式 此存储库提供用于编辑 MessageFormat 模板字符串的代码镜像模式。 语法高亮代码折叠和 linter 与编辑器集成。 如果您想看到它的实际效果,请尝试。 虽然有很多不同的 ...
官方版本,亲测可用
ngx-translate-messageformat-compiler ngx-translate编译器,它使用messageformat.js来使用ICU语法编译翻译以处理复数形式和性别 (StackBlitz) 目录 安装 假设您已经安装了 。 使用npm : npm install ngx-...
利用MessageFormat格式化字符串实例资料.pdf
前端项目-angular-translate-interpolation-messageformat,使用messageformat.js根据某些值插入字符串。
前端项目-intl-messageformat,将ICU消息字符串格式化为数字、日期、复数,然后选择占位符以创建本地化消息。
Intl Messageformat Parser 是发送给 AST 的解析消息字符串,它可以格式化一个人的语言环境消息。
gettext-to-messageformat 使用将gettext输入(po / pot / mo文件)转换为兼容的JSON。 安装 npm install --save gettext-to-messageformat 或者 yarn add gettext-to-messageformat 如果在本机不支持ES6功能...
MessageFormat 编译对象编译 messageformat.js 翻译字符串对象。安装 npm install messageformat-compile-object --save用法 var compile = require('messageformat-compile-object');var obj = {locale: 'es'};obj....
支持MessageFormat i18n,采用流星方式。 轻松React性地使用复杂的字符串(性别,复数形式),并且非常容易地将其翻译成其他语言(通过Web UI)。 特征 通过行业黄金标准MessageFormat使用超强大的语言 通过自动...
命令行界面CLI messageformat (可通过 npm 脚本使用)将 json 中的消息散列作为输入并生成一个 js 文件,该文件将编译的消息工厂导出为 UMD 模块。 和助手 CLI 工具(可通过 npm 脚本使用),基于 CLDR 数据: 复数...
适用于Webpack的YAML i18n加载程序...安装npm install messageformat-yaml-loader 或者yarn add messageformat-yaml-loader用法有关以下内容的有效演示, npm install在目录中运行npm install ,然后在浏览器中打开e