Android development / Basic: Android and Compose foundations / Lesson 7
Plural resources and localized counts
Use quantity resources for count messages and verify locale-specific grammar instead of assembling English fragments.
On this page
Lesson goals
- Define and retrieve a plural resource.
- Explain selection quantity and format arguments.
- Test locale-specific output and accessibility.
Model a count message
A count message must follow the grammar of the active language. Concatenating a number with an English noun assumes that all locales use the same plural rules and word order. Android plural resources select a form according to the current locale and quantity. Provide complete translations reviewed for each supported language, including the categories that language needs.
Define a resource
This default English resource defines one and other forms. It is a small example, not an Arabic translation; a real localized project needs appropriate resources and review for each supported locale. Each form includes a numeric format placeholder. Place it in the application resources and build with the Android toolchain so the generated resource identifier exists.
<resources>
<plurals name="task_count">
<item quantity="one">%d task</item>
<item quantity="other">%d tasks</item>
</plurals>
</resources>
The default resource includes one and other forms.
The resource supplies complete English forms for one and other quantities.
Retrieve and format the message
The helper passes count once to choose the plural form and again as the argument for the numeric placeholder. It requires an Android Context and generated R class, so the fragment is not a standalone Kotlin program. If a form does not contain a placeholder, formatting requirements differ; keep the resource and call contract aligned.
import android.content.Context
fun taskCountLabel(context: Context, count: Int): String =
context.resources.getQuantityString(R.plurals.task_count, count, count)
With the default English resource, 1 gives 1 task and 2 gives 2 tasks.
The first count selects the grammatical form and the second fills the placeholder.
Review locale and meaning
Plural categories are grammatical categories, not universal numeric ranges. Arabic and other languages can require forms beyond English one and other, and translators may change word order. Avoid using a plural resource for a sentence whose meaning depends on unrelated gender or context without an appropriate localization design. Keep accessibility labels consistent with the visible message.
Verify on the target
Test representative quantities including zero, one and several values for every supported locale, using reviewed expected strings. Change the device or app language and confirm the correct resources are selected. Inspect text scaling, right-to-left layout and screen-reader output. A unit test of an English helper alone cannot establish that the installed Android resources or translations are correct.
Common errors and fixes
The count appears without a number.
- Why it happens
- The selected resource form or formatting arguments do not match.
- How to fix it
- Align the placeholder and format arguments.
- Prevent it next time
- Test each relevant resource form.
A translated count is grammatically wrong.
- Why it happens
- English plural assumptions were copied to another language.
- How to fix it
- Provide reviewed locale-specific plural forms.
- Prevent it next time
- Test representative quantities with native-language review.
The resource identifier is unresolved.
- Why it happens
- The fragment is outside a configured Android resource build.
- How to fix it
- Place the resource in the host project and rebuild.
- Prevent it next time
- Verify with the actual Android toolchain.
2 practice exercises
1. Why is count passed twice to getQuantityString in this example?
Show worked solution
The first value selects a plural form; the second fills the %d placeholder. Both are needed because the selected English strings include that placeholder.
2. Can the English one/other example serve as the complete Arabic translation?
Show worked solution
No. Arabic has its own plural grammar and may need additional forms. Provide reviewed Arabic resources and test representative quantities in the actual locale.
Key takeaways
- Use complete locale-specific quantity messages.
- Separate form selection from formatting arguments.
- Verify resources and accessibility on Android targets.