How can I use language metadata in native APIs?

Intended audience: Developers

Sometimes a program receives data from the Web that includes important metadata about the language of a string or a range of text. Developers working with document formats and protocols that include language metadata should preserve it and pass it to the native text APIs that process or display the associated text. This article provides links to documentation for several operating systems, programming environments, and text libraries.

Language metadata in this article means the language of the text itself. It is different from metadata about the language of the intended audience, and it is different from the user's preferred language. For that distinction, see Types of language declaration. For an introduction to language and direction metadata on the Web, see Strings on the Web: Language and Direction Metadata.

Principles

Language tags are normally BCP 47 language tags, such as fr, sr-Latn, or zh-Hant-TW. Treat the tag as data that belongs to the text and keep the whole tag when passing it between layers.

APIs

This section links to APIs for applying language metadata to text ranges, controls, and speech engines.

The list is necessarily incomplete: the Internationalization Working Group welcomes contributions and corrections. Click on the link Leave a comment in the Tell us what you think box below.

Example: a language range in Android

Android's LocaleSpan is useful when one TextView contains text in more than one language.

Locale language = Locale.forLanguageTag("zh-Hant-TW");
SpannableString styled = new SpannableString(text);
styled.setSpan(new LocaleSpan(language), 0, styled.length(),
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(styled);

If the whole control has one language, TextView.setTextLocale or setTextLocales is the appropriate default. For speech, pass the language to TextToSpeech.setLanguage and check its return value.