Android 使用xliff 格式化字符串

Android资源字符串/res/values/string.xml中可以包含xliff的节点,Xliff是XML Localization Interchange File Format 的缩写,中文名为XML本地化数据交换格式。

 

quote from wikipedia (http://en.wikipedia.org/wiki/XLIFF) :

 

"XLIFF (XML Localisation Interchange File Format) is an XML-based format created to standardize localization. XLIFF was standardized by OASIS in 2002. Its current specification is v1.2[1] released on Feb-1-2008.

The specification is aimed at the localization industry. It specifies elements and attributes to aid in localization.

XLIFF forms part of the Open Architecture for XML Authoring and Localization (OAXAL) reference architecture."

 

<xliff:g>标签介绍: 

属性id可以随便命名 
属性值举例说明
%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格 
%n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0 
%n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00 

 

也可简单写成:

 

%d   (表示整数)

%f    (表示浮点数)

%s   (表示字符串)

 

 

使用步骤举例:

 

1.

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

 

2.

 

 <string name="test_xliff">小红今年<xliff:g id="xxx">%d</xliff:g>岁了,上<xliff:g id="yyy">%s</xliff:g>年级!</string>

 

3. 

 

String test = String.format(getResources().getString(R.string.test_xliff), 7, "小学二");

 

输出:

 

小红今年7岁了,上小学二年级!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值