Set language of text
On Android, you can use LocaleSpan to speak content in a specific language. Multiple LocaleSpan's can be embedded inside a SpannableString to speak content in multiple languages.
kotlin
val locale = Locale.forLanguageTag("nl-NL")
val localeSpan = LocaleSpan(locale)
val string = SpannableString("Appt")
string.setSpan(localeSpan, 0, string.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
element.setText(string) In Jetpack Compose, you can use AnnotatedString inside the text Composable and then use SpanStyle) to set locale for a specific part of the text.
kotlin
val text = buildAnnotatedString {
// Setting locale to Dutch for Appt string
withStyle(style = SpanStyle(localeList = LocaleList("nl"))) {
append("Appt")
}
}
Text(text = text) On iOS, the accessibilitySpeechLanguage key of NSAttributedString can be used to speak content in a specific language. Multiple NSAttributedString's can be embedded inside a NSMutableAttributedString to speak content in multiple languages.
In addition, the accessibilityLanguage attribute of the UIAccessibility protocol can be used to set a single language for an element. Many objects implement this protocol, such as UIApplication and UIView.
swift
element.attributedText = NSAttributedString(
string: "Appt",
attributes: [.accessibilitySpeechLanguage: "nl_NL"]
)
application.accessibilityLanguage = "nl_NL"
view.accessibilityLanguage = "nl_NL" In SwiftUI, the accessibilitySpeechLanguage key of AttributedString can be used to speak content in a specific language. Multiple AttributedStrings can be embedded inside a single AttributedString by using the + operator to speak content in multiple languages.
swift
let attributedString = AttributedString(
"Appt",
attributes: AttributeContainer([
.accessibilitySpeechLanguage: "nl_NL"
])
)
Text(attributedString) swift
Button(action: {
// Action here
}) {
Text("search_title")
}
.accessibilityLabel(Text("search_accessibility_label"))
.accessibilityHint(Text("search_accessibility_hint")) With Flutter, the locale parameter of Text or TextSpan can be used to specify the locale for a piece of text. Multiple TextSpan elements can be combined to speak content in multiple languages.
dart
/// Text implementation
Text(
'Appt',
locale: Locale.fromSubtags(languageCode: 'nl'),
);
/// TextSpan implementation
TextSpan(
text: 'Appt',
locale: Locale.fromSubtags(languageCode: 'nl'),
); In React Native, there is limited support to change the accessibility language. The accessibilityLanguage prop of Text only works on iOS. Multiple Text elements can be combined to speak content in multiple languages.
jsx
<Text>
<Text accessibilityLanguage="nl-NL">Appt</Text>
<Text>is an accessibility platform.</Text>
</Text> In MAUI, there is no built-in support to indicate which language should be used by assistive technologies when reading content.
csharp
Not available, contribute! Unfortunately, Xamarin Forms does not have support for changing the accessibility language. When you use HTML such as <span lang="nl">Appt</span> inside a Label, the lang attribute is not used. You can create a component with a custom renderer with native Android and iOS logic. If you have done this, please contribute code.
csharp
Not available, contribute!