Grammatical Inflection API-Android14

Nav Singh
ProAndroidDev
Published in
4 min readMar 13, 2023

Grammatical Inflection API provides a more personalized, natural-sounding user experience for users speaking languages where grammatical gender changes the sentence based on the addressee.

For example: French 🇫🇷

Chère cliente[Feminine], cher client[Masculine] — Dear client [EN]

Grammatical Inflection API introduce in Android14 which helps us to accommodate this.

Let's see it in Action

  • We need to add alternative resource files and append the grammatical gender after the locale name

These resources files should only contain strings that support grammatical gender inflections

  • Put other strings in the default resource files
https://developer.android.com/about/versions/14/features#grammatical-inflection

Code time

Initialize the GrammticalInflectionManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val grammaticalInflectionManager =
getSystemService(GRAMMATICAL_INFLECTION_SERVICE)
as GrammaticalInflectionManager
/.....

Set the Grammatical gender for your app

Here, we can ask the user if they want to use the app with a gender-specific locale.

Set grammatical genders are persistent across application restarts; if Backup & Restore is enabled, they will be backed up.

App grammatical gender changes will result in configuration changes (Activity re-creation)

For example: Using Dialog with possible options.

  • Call setRequestedApplicationGrammaticalGender (int grammaticalGender) selected option
  • Possible values:
grammaticalInflectionManager.
setRequestedApplicationGrammaticalGender(
Configuration.GRAMMATICAL_GENDER_FEMININE)

Main Composable

@RequiresApi(34)
@Composable
fun MainComposable(modifier: Modifier = Modifier) {
val context = LocalContext.current

var showGenderDialog by rememberSaveable {
mutableStateOf(true)
}

val grammaticalInflectionManager =
context.applicationContext?.getSystemService(ComponentActivity.GRAMMATICAL_INFLECTION_SERVICE) as GrammaticalInflectionManager

if (showGenderDialog) {
SelectGrammaticalGenderDialogComposable { selectedValue ->
// set the grammatical gender based on the selected value
grammaticalInflectionManager.setRequestedApplicationGrammaticalGender(selectedValue)
showGenderDialog = !showGenderDialog
}
}

Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {

Image(
painter = painterResource(id = R.drawable.android_14_logo),
contentDescription = "Android 14 logo",
modifier = Modifier.size(300.dp),
)
Text(
text = "Grammatical Inflection API 🇫🇷",
style = TextStyle(fontSize = 20.sp, fontWeight = FontWeight.Bold),
modifier = modifier,
)

Spacer(modifier = Modifier.height(16.dp))

Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
OutlinedButton(onClick = {
// show the current grammatical gender
Toast.makeText(
context,
"${grammaticalInflectionManager.applicationGrammaticalGender}",
Toast.LENGTH_SHORT
).show()
}) {
Text(text = "Check app's Grammatical gender")
}
Spacer(modifier = Modifier.width(8.dp))
OutlinedButton(onClick = {
showGenderDialog = true
}) {
Text(text = "Change grammatical gender")
}
}

Text(text = stringResource(id = R.string.baker_job_title))

Text(text = stringResource(id = R.string.dear_cleint))
}
}

Composable for Dialog

@RequiresApi(34)
@Composable
fun SelectGrammaticalGenderDialogComposable(alertAction: (Int) -> Unit) {
GrammaticalInflectionAPITheme {
AlertDialog(
onDismissRequest = { /*TODO*/ },
confirmButton = { /*TODO*/ },
text = {
Column(verticalArrangement = Arrangement.Center) {
Text(
text = "Masculine",
style = TextStyle(fontWeight = FontWeight.ExtraBold, fontSize = 24.sp),
modifier = Modifier.clickable {
alertAction(Configuration.GRAMMATICAL_GENDER_MASCULINE)
},
)
Spacer(Modifier.height(4.dp))
Text(
text = "Feminine",
style = TextStyle(fontWeight = FontWeight.ExtraBold, fontSize = 24.sp),
modifier = Modifier.clickable {
alertAction(Configuration.GRAMMATICAL_GENDER_FEMININE)
},
)
Spacer(Modifier.height(4.dp))
Text(
text = "Neutral",
style = TextStyle(fontWeight = FontWeight.ExtraBold, fontSize = 24.sp),
modifier = Modifier.clickable {
alertAction(Configuration.GRAMMATICAL_GENDER_NEUTRAL)
},
)
Spacer(Modifier.height(4.dp))
}
},
title = {
Text(text = "Select the Grammatical gender for your app", style = TextStyle(fontSize = 14.sp))
},
)
}
}

Folder structure for resource files

Demo

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Nav Singh

Google Developer Expert for Android | Mobile Software Engineer at Manulife | Organizer at GDG Montreal

No responses yet

What are your thoughts?

Recommended from Medium

Lists

See more recommendations