Little secret of string in Android

Source: Internet
Author: User

Usually we all know that when using string, to avoid hard coding in the code, you should use the String.xml file to save the string we use in the project, the advantage is to avoid the reuse of code, unified management and loading different languages according to different regions.
But we can usually ignore some of its usage:

1.string can also use parameters
<stringname="four">4</string><stringname="family">我们家有%s个人</string>

Use:

getString(R.string.family, getString(R.string.four))

What if there are multiple parameters?
Like what:

<stringname="four">4</string><stringname="one">1</string><stringname="family">我们家有%1$s个人,我有%2$s个弟弟</string>

Use:

getString(R.string.family, getString(R.string.four), getString(R.string.one))

There are multiple arguments to the second analogy.

2 Internationalization of String

If you want to create a string resource that corresponds to English:

This time you can write the language you want to translate

<stringname="samsung">我一部sumsung手机</string><stringname="family">I have a sumsung phone</string>

At this time we can find that Sumsung does not need to be translated into English, for unnecessary translation of words, we can use <xliff:g> labels to mark up
Before using we will point out the namespace: <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
Like what:

<string name="samsung">我一部<xliff:g>sumsung</xliff:g>手机</string>

Ps:google Play console provides translation services for app translation service
There is another situation, such as the singular and plural:

<string name="eat_one_apple"one apple</string><string name="eat_ohter_apples"three apples</string>

Android provides a plurals method for this scenario.

<plurals name="eat_apple">       <item quantity="one">I eat one apple</item>        <item quantity="other">I eat three apples</item></plurals>

To obtain the Plurals method as follows, the second parameter passed into the quantity, the system will select the corresponding display according to quantity, the method can also add parameters:

getResources().getQuantityString(R.plurals.eat2)

And the Chinese part we can change this:

<plurals name="Eat_apple">        <item Quantity="One">I ate one.<xliff:g id="Kindle">Apple</xliff:g></Item>        <item Quantity="Other">I ate a lot.<xliff:g id="Kindle">Apples</xliff:g></Item></plurals>
3 adding special characters to a string

I think this people use more, some special characters are no way directly in writing string.xml, such as "<", ">" but can use its corresponding ASCII code to replace the display, such as to express: @ xiaoming, you can write:

<stringname="call_xiaoming">&#064;小明</string>

The ASCII code for @ & #064

Little secret of string in Android

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.