Faq_17 % in Android string. xml

Source: Internet
Author: User

There is such a situation that you have never met.

We want to internationalize Android resources, but sometimes we need to dynamically change the content of the string. You cannot write all the situations to string. xml.

What should I do?

Suppose we have made an app, but some devices do not support some features of our app. In this case, you need to prompt the user:

Your mobile phone does not have ××. You cannot use this function for the moment!


To solve this problem, Android provides us with a convenient method: You can use xliff or not use xliff.


Xliff is the abbreviation of XML localization interchange file format. The Chinese name is the XML localized data exchange format.


Let's take a few examples to illustrate the problem.


1. Do not use xliff


In Res/values/string. xml:


<String name = "info"> I am % 1 $ D, and myname is % 2 $ S </string>


In the Code:


Context mcontext;

String info = mcontext. getresources (). getstring (r.string.info, 10, "Mark ");


Output Information: I am is 10, and myname is Mark


It can be seen that % 1 $ D is replaced by 10, and % 2 $ S is replaced by Mark. At this point, you will ask, why do we need to mark 1 and 2? What if we write 1?

The test shows that the output information of write 1 is: I am is 10, and myname is 10.


For more information, see the getstring source code.


Public String getstring (int id, object...
Formatargs) throws notfoundexception {
String raw = getstring (ID );
Return string. Format (mconfiguration. locale, raw, formatargs );
}


Object... formatargs indicates a data type. For details, refer:

Http://blog.csdn.net/androidbluetooth/article/details/6593632


2. Use xliff


In Res/values/string. xml:


<? XML version = "1.0" encoding = "UTF-8"?>
<Resources xmlns: xliff = "urn: Oasis: names: TC: xliff: Document: 1.2">

<String name = "info"> I am is <xliff: G> % 1 $ d </xliff: G>, and myname is <xliff: g> % 2 $ S </xliff: G> </string>

</Resources>


Note: xmlns: xliff = "urn: Oasis: names: TC: xliff: Document: 1.2" to declare.


Then in the code, you can use it as above. The results are the same.


If you want to be more user-friendly, you can write as follows:


<String name = "info"> I am is <xliff: G id = "Age" example = "20"> % 1 $ d </xliff: G>,
And myname is <xliff: G id = "name", example = "Jhon"> % 2 $ S </xliff: G> </string>


ID and example are only descriptions.


% Is a formatted string by default. What if you want to use % in string. xml?


Solution


1. Use % to indicate 1%. Do not use the conversion symbol \. It does not work!


<String name = "value_perc"> 30% % </string>


2. Add attributes: formatted = "false"


<String name = "value_perc"
Formatted = "false"> 30% </string>




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.