Change the Language of my current input keyboard programmatically in iOS and MacOS

We are building a Multi-Lingual Business Application -- where the user is able to enter values and data in multiple languages.

One of our main use cases is, if a user is editing or adding to a text value or data that is in a different language. For example, the data they are editing could be in Japanese, and the user's current input language is set to English, then when they enter the editing mode, the input language is automatically set to Japanese by us -- programmatically. They can go back to editing English items, and the input language is changed back to English, and similarly, when they come back to edit the Japanese data or some German data, then the input language is again automatically changed to the respective language by the application -- without causing the user to manually go to the settings and change it.

Is there any current way to have this behaviour in MacOS and iOS now?

Please suggest what can be done to achieve the same.

Answered by DTS Engineer in 851080022

If you are using UIKit, textInputContextIdentifier is what you are looking for – If you create your own subclass of UITextView or UITextField to override textInputContextIdentifier to return a unique ID, the instances of your text view class will remember the tex input mode.

NSTextView or NSTexField doesn't seem to have the same feature unfortunately, and so I’d suggest that you file a feedback report and share your report ID here. Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

If you are using UIKit, textInputContextIdentifier is what you are looking for – If you create your own subclass of UITextView or UITextField to override textInputContextIdentifier to return a unique ID, the instances of your text view class will remember the tex input mode.

NSTextView or NSTexField doesn't seem to have the same feature unfortunately, and so I’d suggest that you file a feedback report and share your report ID here. Thanks.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hi Ziqiao,

While the method works for the case of "remembering" a language for the textfield. It did not work for getting the keyboard itself to change the language. I have this method in UITextInputMode to achieve some similar functionality:

override var textInputMode: UITextInputMode? {
        for inputMode in UITextInputMode.activeInputModes {
            if let primaryLanguage = inputMode.primaryLanguage,
               primaryLanguage.hasPrefix(targetLanguageCode) {
                print("This will return Arabic if 'ar' or English if 'en'")
                return inputMode
            }
        }
        print("Target language keyboard not found. Falling back to default.")
        return super.textInputMode
    }

This seems to work well for a particular TextField, however, this is TextField specific and not app-wide. Is there any way to change the input language for the entire app? Also, please let me know if the above implementation is all right and will be allowed in production apps.

Secondly, coming to MacOS, is there something similar to the above UITextInputMode that allows me to return a primaryLanguage like this?

If not then can we use the Carbon Framework's Input Source method to achieve similar functionality? I understand that Carbon has been deprecated but these APIs seem to work? Are we not allowed to use these APIs? There is no documentation for these APIs as it has already been discussed in this thread, but there seem to be no answers here too:

https://developer.apple.com/forums/thread/124268?answerId=389152022#389152022

Are we allowed to use these methods or will this cause problems while publishing the app?

Hi Ziqiao, please refer to the comment I have added below this post. I have detailed out some further questions.

In short: I have used the primaryLanguage property in UITextInputMode to achieve some similar functionality.

This seems to work well for a particular TextField, however, this is TextField specific and not app-wide. Is there any way to change the input language for the entire app?

Secondly, coming to MacOS, is there something similar to iOS above in MacOS?

Accepted Answer

Nice to know that you find a solution by overriding textInputMode.

I don't see any way to change the language for the whole app, however. TISSelectInputSource is gated by sandbox – it doesn't do anything for a sandboxed app.

Overall, I believe changing the input mode globally for an app is more a user decision, and that's why the platforms don't provide an API. Feel free to file a feedback report to voice your need though.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Change the Language of my current input keyboard programmatically in iOS and MacOS
 
 
Q