Grammatical Inflection API-Android14
![](https://miro.medium.com/v2/resize:fit:678/1*vkUDEw4docTqebNSKiPOYQ.png)
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://miro.medium.com/v2/resize:fit:654/1*0jgRf1P7OADoGknCqhG00A.png)
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 bebacked up
.App grammatical gender changes will result in configuration changes (Activity re-creation)
For example: Using Dialog
with possible options.
![](https://miro.medium.com/v2/resize:fit:350/1*T7japq0dEeOGKKTni0oxEQ.png)
- Call
setRequestedApplicationGrammaticalGender (int grammaticalGender)
selected option - Possible values:
![](https://miro.medium.com/v2/resize:fit:408/1*MzxqHPSsMNKVhmBE21YimQ.png)
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
![](https://miro.medium.com/v2/resize:fit:271/1*2j5QGfRdGfZcyb2RK2bsMQ.png)
Demo
![](https://miro.medium.com/v2/resize:fit:700/1*8HLEjOW0h-T2nc7I4Juebw.gif)