About Android strings.xml - Several principles you should know

About Android strings.xml - Several principles you should know

[[187865]]

Don't try to reuse

Do not reuse a string in different pages.

1. Suppose your login and registration pages both have a loading dialog box. Because both have loading interfaces, you decide to use the same string - R.string.loading.

But you never know when you will use a different string, then you will need to create two new strings and modify the Java code. If you use two strings from the beginning, you only need to modify the strings.xml file.

2. You never know which languages ​​your app will support. In one language - maybe you can use the same word in different contexts, but in another language different contexts may require different words.

Note that in the English version of strings.xml, both R.string.download_file_yes and R.string.terms_of_use_yes are the same word - "Yes".

But in Ukrainian version R.string.download_file_yes uses "Гаразд" and R.string.terms_of_use_yes uses "Так".

Separation

Strings on the same page are separated by prefixes and comments

Add a page prefix to each string, helping you immediately identify which page a string belongs to.

A clean strings.xml file helps you easily maintain and translate into different languages ​​- page by page.

Create a separate strings.xml file for each page

If you want to create a strings.xml file for each page, you can name it like this: settings-strings.xml, profile-strings.xml. But an app usually has 10 to 20 pages. So there will be 10 to 20 strings.xml files in each language directory, which I think brings confusion.

Format

Use Resources#getString(int id, Object… formatArgs) to format strings

Never use the + sign to concatenate strings, because different languages ​​have different word orders.

The correct way is to use Resources#getString(int id, Object… formatArgs).

plural

Use Resources#getQuantityString (int id, int quantity) to handle plural numbers

Never resolve pluralization issues in Java code, because the grammatical rules for pluralization are different in different languages.

The correct way is to use Resources#getQuantityString(int id, int quantity).

Note: For more information about the getQuantityString method, see: Android Quantity Strings getQuantityString method value problem

Word highlighting

Use html text to highlight static words

If you want to change color of some words in TextView - ForegroundColorSpan is not always the best choice, because its highlighting is done by index and it is not safe in multi-language. Use html's font color tag in strings.xml file.

Suppose you have a text that says “Discover and play games.” You want the words “Discover” and “play” to appear in blue.

<<:  The fourth episode of the Aiti tribe consultation: Application scenarios and functions of Java message queues

>>:  The fourth session of the Aiti Tribe Technical Clinic

Recommend

7 Tips to Reduce Customer Acquisition Costs for Facebook Ads

Customer acquisition requires a solid understandi...

Why Are Your Facebook Ads Burning Money So Fast?

When you run Facebook ads , one of the most impor...

How to write a hot-selling product introduction? Just look at these points!

Product introduction and product details are task...

For these dragons, having a tail that's too big is also a problem...

When it comes to the largest animal on land today...

The top 10 keywords for breaking through brand traffic in 2021!

As the consumption environment evolves, how can n...

Changsha tea tasting high-end peripheral contact information sharing

Content: 90 minutes unlimited studio in Changsha....

How to attract users through short video promotion?

On June 24, the "China Mobile Internet Devel...

How to create an internet celebrity?

This article takes Dyson as an example and analyz...

3 key points for optimizing Google advertising!

The quality score of your ads plays a very import...

The oracle bone script is so cute that even I am scared!

▲The picture shows Zao Wou-Ki's "Landsca...