Improving Android string resources with XLIFF

2019-04-18 23:54发布

I've seen some Google apps and code samples using XLIFF tags to wrap variables. I see some great advantages in doing this, especially for replacing non-descriptive format arguments such as %1$s.

Unfortunately, XLIFF doesn't seem to integrate well into ADT. Take the following string resource, for instance:

<resources 
    xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2" 
    xmlns:tools="http://schemas.android.com/tools">

    <string name="share_with_application">
        Share your score of <xliff:g id="score" example="1337">%1$s</xliff:g>
        with <xliff:g id="application_name" example="Bluetooth">%2$s</xliff:g>!  
    </string>

</resources>

Graphical Layout preview

In the above example, the string is truncated after the first <xliff> tag. One would also expect the example attribute to be used, resulting in a graphical preview showing:

Share your score of 1337 with Bluetooth!

Is there presently any merit to using XLIFF tags in my strings resources?

3条回答
甜甜的少女心
2楼-- · 2019-04-19 00:21

We've just added support for this in Android Studio, for version 0.3: https://android-review.googlesource.com/#/c/67724/

查看更多
闹够了就滚
3楼-- · 2019-04-19 00:24

Having now gotten my hands dirty with building AOSP from source, CommonsWare is correct: that build process is entirely different and indeed many AOSP apps contain XLIFF tags. It's too bad this doesn't integrate with ADT, but it is as it is.

查看更多
Animai°情兽
4楼-- · 2019-04-19 00:35

Just to be clear, the use of XLIFF tags in Android string resources is fully documented at

https://developer.android.com/distribute/tools/localization-checklist.html#manage-strings

Such strings work fine even in Eclipse builds (the support is in the aapt tool that builds the apps).

However, as illustrated in the question, the graphical layout tools contained in the ADT don't make any allowance for the xliff tag, merely truncating the string after the first such marked up sub-string.

The graphical tools in Android Studio do cope well with such strings, knowing about, and making use of the xliff markup.

I actually find that the full <xliff:g> is rather verbose in my source code, so I adjust the namepsace declaration to allow me to use just <x:g> thus:

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

   <string name="greeting">Hello <x:g id="name">%1$s</x:g>!</string>

</resources>
查看更多
登录 后发表回答