Skip to main content

Grammatical Gender with Grammatical Inflection API

warning

Grammatical Inflection API is only available in API level 34 (Android 14) or higher.

Android 14 introduces Grammatical Inflection API that enables you to customize strings based on user's grammatical gender preference. To do so, we need to use GrammaticalInflectionManager class. Alternatively, you can also use the backward-compatible version: GrammaticalInflectionManagerCompat.

Purpose

Grammatical Inflection API is meant for strings that address the user directly. For other situations, consider using MessageFormat API instead.

Basic Usage

Let's use the example taken from the official documentation. The English phrase "You are subscribed to..." can be phrased differently in French based on the gender of the user:

Grammatical GenderPhrase
Masculine-inflected form"Vous êtes abonné à..."
Feminine-inflected form"Vous êtes abonnée à..."
Neutral phrasing that avoids inflection"Abonnement à...activé"

Setting Up String Resources

To accommodate these differences, we need to create additional strings.xml files for these 3 possibilities:

res/values-fr-feminine/strings.xml (Feminine)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="you_are_subscribed">Vous êtes abonnée à...</string>
</resources>
res/values-fr-masculine/strings.xml (Masculine)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="you_are_subscribed">Vous êtes abonné à...</string>
</resources>
res/values-fr-neuter/strings.xml (Neuter)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="you_are_subscribed">Abonnement à...activé</string>
</resources>

Getting Current Grammatical Gender Preference

Then you can utilize GrammaticalInflectionManagerCompat class to customize user's grammatical gender preference within your app.

First, you can retrieve current grammatical gender preference of the user by using getApplicationGrammaticalGender() method:

import android.content.Context
import android.util.Log
import androidx.core.app.GrammaticalInflectionManagerCompat

// val context: Context = ...
val grammaticalGender = GrammaticalInflectionManagerCompat.getApplicationGrammaticalGender(context)
val grammaticalGenderStr = when (grammaticalGender) {
GrammaticalInflectionManagerCompat.GRAMMATICAL_GENDER_NOT_SPECIFIED -> "Not specified"
GrammaticalInflectionManagerCompat.GRAMMATICAL_GENDER_FEMININE -> "Feminine"
GrammaticalInflectionManagerCompat.GRAMMATICAL_GENDER_MASCULINE -> "Masculine"
GrammaticalInflectionManagerCompat.GRAMMATICAL_GENDER_NEUTRAL -> "Neutral"
else -> "Unknown"
}
Log.i("TAG", "Current Grammatical Gender: $grammaticalGenderStr")

For device running below Android 14, the value will always be GRAMMATICAL_GENDER_NOT_SPECIFIED.

Updating Grammatical Gender Preference

Then, you can update user's grammatical gender preference by using setRequestedApplicationGrammaticalGender:

import android.content.Context
import androidx.core.app.GrammaticalInflectionManagerCompat

// val context: Context = ...
GrammaticalInflectionManagerCompat.setRequestedApplicationGrammaticalGender(
context,
GrammaticalInflectionManagerCompat.GRAMMATICAL_GENDER_FEMININE,
)

For device running below Android 14, this method will do nothing.

Take note that calling this method, by default will recreate your Activity. If you want to disable Activity recreation, you need to add grammaticalGender to android:configChanges property inside AndroidManifest.xml file:

AndroidManifest.xml
<activity android:name=".MainActivity"
android:configChanges="grammaticalGender">
</activity>

Here's a simple example showcasing the Grammatical Inflection API in action:

References