Android带参字串的写法注意事项

问题引入

1.项目中会常常遇到一些带参数的字串。例如,Android原生系统中,当电量低于15%时,会提示“Battery is low”等信息,如下图所示。


2. 由于项目组自己有针对源码中的所有字串进行汇总,方便进行字串翻译库统一管理更新。但是期间出现一个问题,就是效果看起来少了个%。

[原始字串id]

S:SystemUI:battery_low_percent_format

[源码字串翻译]

<string name="battery_low_percent_format" msgid="2900940511201380775">"<xliff:g id="PERCENTAGE">%s</xliff:g> remaining"</string>

项目Java代码中访问方式如下:

    private void showWarningNotification() {
        final int textRes = mSaver ? R.string.battery_low_percent_format_saver_started
                : R.string.battery_low_percent_format;
        //final String percentage = NumberFormat.getPercentInstance().format((double) mBatteryLevel / 100.0);//原生做法
        final Notification.Builder nb = new Notification.Builder(mContext)
                .setSmallIcon(R.drawable.ic_power_low)
                // Bump the notification when the bucket dropped.
                .setWhen(mBucketDroppedNegativeTimeMs)
                .setShowWhen(false)
                .setContentTitle(mContext.getString(R.string.battery_low_title))
                .setContentText(mContext.getString(textRes, mBatteryLevel/*percentage*/))//modify by xxx for systemUI FC when battery is low at 2015 1116
                .setOnlyAlertOnce(true)
                .setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_WARNING))
                .setPriority(Notification.PRIORITY_MAX)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                .setColor(mContext.getColor(
                        com.android.internal.R.color.battery_saver_mode_color));

3.从以上代码可知,是由于项目上对此作出了修改,原生使用percentage作为参数传给%s,而经过NumberFormat的处理,返回的直接时14%,所以原生机器Nexus不会存在这个问题。而我们直接传送的时一个整型数值,因而问题出现了。


问题处理

如果在不再修改Java代码的情况下,该如何调整字串翻译,才能使得这个%显现呢,这就涉及到%的添加和转义的问题。

经过自己写的一个小应用测试,得出如下结论:
1.首先我测试了4中情况的字串,定义如下:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="get_paired_bluetooth_device2">GET BLUETOOTH 100%</string>   ---->显示为GET BLUETOOTH 100%
<string name="get_paired_bluetooth_device3">GET BLUETOOTH 100%%</string>   ---->显示为GET BLUETOOTH 100%%
<string name="xdayi1"><xliff:g id="percentage">%s</xliff:g> %% remaining</string>  ---->显示为23 % remaining
<!-- <string name="xdayi2"><xliff:g id="percentage">%s</xliff:g> % remaining</string> -->  ---->发生force close
</resources>

2.
[结论] 
1.不包含<xliff:g>标签的字串,无论1个还是2个%,都不会发生force close,如果想表达34%,这只需一个%即可(这与之前验结果一致);
2.如果字串中包含<xliff:g>标签,不论是在<xliff:g>标签里面还是外面,如果想表达35%,必须包含两个%。因为只要字串中包含了<xliff:g>,该字串解析需要用到以下红色字体的命名空间。另外,无论<xliff:g>标签里面还是外面,使用一个%,都会发生force close。
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

[补充]
当然,后两个字串的%放置在标签里面,结果也是一样的。
<string name="xda1"><xliff:g id="percentage">%s%%</xliff:g> remaining 2</string>  OK
<string name="xda2"><xliff:g id="percentage">%s%</xliff:g>remaining 2</string>  NOK(force close)

原始文档

本文个人原创原始文档获取地址如下,
http://download.csdn.net/detail/u013398960/9804976

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值