使用语法性别对应用界面进行个性化设置

有 30 亿人在使用区分性别的语言,此类语言的语法类别(例如名词、动词、形容词和介词)会根据您交谈所涉及的人或物的性别而变化。传统上,许多区分性别的语言使用阳性语法性别作为默认或通用性别。

以错误的语法性别来称呼用户,例如以阳性语法性别来称呼女性,可能会对她们的表现和态度产生负面影响。相比之下,界面语言如果能正确反映用户的语法性别,就可以提高用户互动度,并提供更个性化、更自然的用户体验。

To help you build a user-centric UI for gendered languages, Android 14 introduces the Grammatical Inflection API, which lets you add support for grammatical gender without refactoring your app.

Example of inflection for grammatical gender

In gendered languages, grammatical gender can't be worked around the same way as it can in English. For example, in English to write a message telling the user that they are subscribed to your app's service, you could use a single phrase: "You are subscribed to...".

To provide a similar phrase in French, there are a few options:

  • Masculine-inflected form: "Vous êtes abonné à..." (English: "You are subscribed to...")
  • Feminine-inflected form: "Vous êtes abonnée à..." (English: "You are subscribed to...")
  • Neutral phrasing that avoids inflection: "Abonnement à...activé" (English: "Subscription to ... enabled")

Similar to English, the first two options address the user directly. However, without any mechanism to accommodate this grammatical feature of French, you would only have the third option, which changes the tone of the message and might not be what you want to display in your user interface.

In these cases, the Grammatical Inflection API lowers the effort to display strings relative to the viewer's grammatical gender—that is, the person who's viewing the UI, not who's being talked about. To show users personalized translations in your app, add translations that are inflected for each grammatical gender for affected languages and then use the GrammaticalInflectionManager API to adjust which translations are shown to each user.

In many languages, grammatical gender also applies to regular nouns in addition to people. For example, in French the word chaise (chair) is feminine, whereas oiseau (bird) is masculine. For situations other than addressing the user, you can use the existing ICU SelectFormat API.

Implement the API

After the user has indicated their grammatical gender (for example, either through a settings section of your app or a user setup workflow), you can use the setRequestedApplicationGrammaticalGender(int) method to store the value in your app's resources configuration.

For example, if you want to set a user's preferred grammatical gender to feminine, you would ask the user to select which grammatical gender they prefer and then call the API:

Kotlin

// Set app's grammatical gender to feminine
val gIM = mContext.getSystemService(GrammaticalInflectionManager::class.java)
gIM.setRequestedApplicationGrammaticalGender(
    Configuration.GRAMMATICAL_GENDER_FEMININE)

Java

// Set app's grammatical gender to feminine
GrammaticalInflectionManager gIM =
    mContext.getSystemService(GrammaticalInflectionManager.class);
gIM.setRequestedApplicationGrammaticalGender(
    Configuration.GRAMMATICAL_GENDER_FEMININE);

Here is example of how to declare configuration changes in your app's manifest file if you want to handle them yourself:

<activity android:name=".TestActivity"
              android:configChanges="grammaticalGender"
              android:exported="true">
</activity>

If your app needs to check the grammatical gender in the current resource configuration, you can use the getApplicationGrammaticalGender() method to retrieve it:

Kotlin

val gIM = mContext.getSystemService(GrammaticalInflectionManager::class.java)
val grammaticalGender = gIM.getApplicationGrammaticalGender()

Java

GrammaticalInflectionManager gIM =
    mContext.getSystemService(GrammaticalInflectionManager.class);
int grammaticalGender = gIM.getApplicationGrammaticalGender();

Add translations for languages with grammatical gender

To provide localized text for languages with grammatical gender, create an alternative resources file and append the grammatical gender qualifier immediately after the locale name for those languages. The following table outlines the possible values:

Qualifier String value Example (French fr)
Feminine feminine res/values-fr-feminine/strings.xml
Masculine masculine res/values-fr-masculine/strings.xml
Neuter neuter res/values-fr-neuter/strings.xml

You should only include strings that support grammatical gender inflections in these resources files. All strings must have a value in the default resource file that contains other localized strings. This default translation is shown whenever a gender-inflected translation is not available.

In the example provided for French earlier, the neutral phrasing would be the value of the string in the default resources res/values-fr/strings.xml file. The following code snippets show how each resource file would be formatted to accommodate all the grammatical variations from the example in French:

Feminine

Include the feminine-inflected string in the res/values-fr-feminine/strings.xml resources file:

<resources>
    ...
    <string name="example_string">Vous êtes abonnée à...</string>
</resources>

Masculine

Include the masculine-inflected string in the res/values-fr-masculine/strings.xml resources file:

<resources>
    ...
    <string name="example_string">Vous êtes abonné à...</string>
</resources>

Neuter

Include the default string in the res/values-fr/strings.xml resources file:

<resources>
    ...
    <string name="example_string">Abonnement à...activé</string>
</resources>