Skip to content

Add support for language hints in TextFields (Android only) - #165554

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
bleroux:add_ime_hint_locales
Apr 17, 2025
Merged

Add support for language hints in TextFields (Android only)#165554
auto-submit[bot] merged 3 commits into
flutter:masterfrom
bleroux:add_ime_hint_locales

Conversation

@bleroux

@bleroux bleroux commented Mar 20, 2025

Copy link
Copy Markdown
Contributor

Description

This PR adds support for language hints in TextFields.
This is supported only on Android for the moment and the property name, aka TextField.hintLocales, is based on Android's EditorInfo.hintLocales property, see https://developer.android.com/reference/android/view/inputmethod/EditorInfo#hintLocales.

Related Issue

Fixes Support for language hints in TextFields

Tests

Adds 4 tests.
Updates several tests.

@github-actions github-actions Bot added a: text input Entering text in a text field or keyboard related problems platform-android Android applications specifically framework flutter/packages/flutter repository. See also f: labels. engine flutter/engine related. See also e: labels. f: material design flutter/packages/flutter/material repository. labels Mar 20, 2025
@bleroux
bleroux force-pushed the add_ime_hint_locales branch 5 times, most recently from d6214e6 to 08ef92e Compare March 24, 2025 09:32
@bleroux
bleroux requested a review from justinmc March 24, 2025 13:06

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking this on @bleroux! It's always extra work to add engine+framework features like this.

Can you take a quick look at the iOS docs (if you haven't already) to see if they have a similar feature? I just want to make sure that what we're adding is more or less compatible if so. If you can reassure me there then LGTM.

@reidbaker Would you be able to back me up by reviewing the Android code in this PR?

Comment thread packages/flutter/lib/src/services/text_input.dart Outdated
Comment thread packages/flutter/lib/src/services/text_input.dart
Comment thread packages/flutter/lib/src/services/text_input.dart
Comment thread packages/flutter/lib/src/services/text_input.dart Outdated
Comment thread packages/flutter/test/widgets/editable_text_test.dart
Comment thread packages/flutter/lib/src/services/text_input.dart Outdated
Comment thread packages/flutter/lib/src/services/text_input.dart Outdated
@bleroux

bleroux commented Mar 25, 2025

Copy link
Copy Markdown
Contributor Author

Can you take a quick look at the iOS docs (if you haven't already) to see if they have a similar feature?

I found https://developer.apple.com/documentation/uikit/uitextinputmode/primarylanguage which accepts one language tag instead of a list of locales. It looks like we would be able to provide this feature for iOS and macOS 🎉. The comment will reflect that only the first locale is used on iOS.

@justinmc Do you think we should keep the name 'hintLocales' or used something agnostic such as 'preferredLocales'?

@bleroux
bleroux force-pushed the add_ime_hint_locales branch from 08ef92e to da3401d Compare March 25, 2025 08:17
@jesswrd
jesswrd requested a review from reidbaker March 25, 2025 18:44
@bleroux
bleroux force-pushed the add_ime_hint_locales branch from da3401d to 3f9cbf6 Compare March 27, 2025 13:16
@justinmc

Copy link
Copy Markdown
Contributor

@bleroux Let's say we keep the name hintLocales but add iOS support later. Are you thinking that iOS will take the first item in the list since it only supports one? Are the strings compatible like that?

Overall I don't have a strong opinion about the name at the EditableText/TextField level. However at the TextInputConfiguration level I wonder if we can support both separately via different values in TextInputConfiguration? In order to give fine grained control to users that might want that.

Maybe it's such a simple feature that fine grained control doesn't really matter in this case, but that's the approach I generally try to take for these kinds of cross platform APIs.

@bleroux

bleroux commented Mar 27, 2025

Copy link
Copy Markdown
Contributor Author

Let's say we keep the name hintLocales but add iOS support later.

Perfectly fine to me. 👍

Are you thinking that iOS will take the first item in the list since it only supports one? Are the strings compatible like that?

Yes, It was my thinking. And yes the strings are compatible because the string expected by iOS is the result of Locale.toLanguageTag().

However at the TextInputConfiguration level I wonder if we can support both separately via different values in TextInputConfiguration? In order to give fine grained control to users that might want that.

Great point! I will have to think about it and share my findings.

Maybe it's such a simple feature that fine grained control doesn't really matter in this case, but that's the approach I generally try to take for these kinds of cross platform APIs.

Very interesting, thank you for sharing this.

@reidbaker
reidbaker requested review from a team and removed request for reidbaker April 8, 2025 18:56

@camsim99 camsim99 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Android parts look solid to me!

@bleroux
bleroux requested a review from justinmc April 14, 2025 18:46
@bleroux

bleroux commented Apr 14, 2025

Copy link
Copy Markdown
Contributor Author

Thanks @camsim99! 🙏

@justinmc Based on #165554 (comment) do you think we can proceed with this PR? Once merged I will work on the iOS support.

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with respect to the future iOS support 👍 . So this PR supports only Android and uses the native Android name hintLocales. Native iOS calls it primaryLanguage, and that name is still available to us in case we find some reason to use both. Otherwise we can keep everything under hintLocales.

@bleroux
bleroux force-pushed the add_ime_hint_locales branch from 3f9cbf6 to 3b50a7b Compare April 17, 2025 08:34
@bleroux bleroux added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 17, 2025
@auto-submit
auto-submit Bot added this pull request to the merge queue Apr 17, 2025
Merged via the queue into flutter:master with commit fbed8a7 Apr 17, 2025
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 17, 2025
@bleroux
bleroux deleted the add_ime_hint_locales branch April 17, 2025 12:35
@valerauko

Copy link
Copy Markdown

Is there an issue tracking the iOS implementation for this?

@bleroux

bleroux commented Jul 23, 2025

Copy link
Copy Markdown
Contributor Author

Is there an issue tracking the iOS implementation for this?

I filed #172620 to track the iOS implementation.
Unfortunately, it seems this is not possible to force the IME language on iOS programmatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems engine flutter/engine related. See also e: labels. f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. platform-android Android applications specifically

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for language hints in TextFields

4 participants