Does anybody knows, can I get the current input language and/or keyboard layout in iPhone application? Can I also get a notification when input language was changed?
-
More recent answer: http://stackoverflow.com/questions/3860553/is-it-possible-to-detect-the-current-keyboard-input-method-on-the-iphone – Sjors Provoost Dec 26 '11 at 07:00
-
You might want to look at this link. http://razibdeb.wordpress.com/2013/01/30/how-to-check-whether-a-character-is-from-english-language-or-not-in-objective-c/ – Deepak Thakur Sep 24 '14 at 13:22
6 Answers
In iOS 4.2 and later, you can use the UITextInputMode
class to determine the primary language currently being used for text input.
[UITextInputMode currentInputMode].primaryLanguage
will give you an NSString
representing the BCP 47 language code such as “es”, “en-US”, or “fr-CA”.
You can register for the UITextInputCurrentInputModeDidChangeNotification
to be alerted when the current input mode changes.
(You might also be interested in the "Getting Your Apps Ready for China and other Hot New Markets" WWDC session, and Internationalization Programming Topics.)

- 3,455
- 23
- 26
-
-
1Because this method is deprecated in iOS7, its better to use current first responder's method **textInputMode**. Its works the same. See my answer. – kirander Apr 14 '14 at 12:27
You can ask current first responder (UITextField, UISearchBar, etc.) via UIResponder method textInputMode:
// assume you have instance variable pointing to search bar currently entering
UITextInputMode *inputMode = [self.searchBar textInputMode];
NSString *lang = inputMode.primaryLanguage;

- 2,202
- 20
- 18
You can add an observer to the default notification center:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(inputModeDidChange:)
name:@"UIKeyboardCurrentInputModeDidChangeNotification"
object:nil];
This method prints the currently selected input language (like "en_US"
or "de_DE"
):
- (void)inputModeDidChange:(NSNotification*)notification
{
id obj = [notification object];
if ([obj respondsToSelector:@selector(inputModeLastUsedPreference)]) {
id mode = [obj performSelector:@selector(inputModeLastUsedPreference)];
NSLog(@"mode: %@", mode);
}
}
BUT: All the above is not documented and you should not use it in shipping code!

- 81,520
- 17
- 180
- 200
-
1You might have gotten the neg because you're describing code that can't be shipped using the SDK. – Shaggy Frog Sep 17 '09 at 06:03
-
1Interesting, but not useful--if I indeed cannot use in "shipping" code. – bentford Mar 18 '10 at 13:08
-
6I still don’t get it... Three downvotes for some reverse-engineered information which clearly states it’s non-applicability for shipping apps. Downvoters, are you offended by the information in this answer? – Nikolai Ruhe Mar 19 '10 at 11:27
-
I'm downvoting because UITextInputMode offers a supported way to do this. – Vincent Gable Feb 21 '12 at 22:04
-
2@Vincent Gable: Please note that this answer was written at a time where `UITextInputMode` was not yet available (iOS 4.2, released Nov. 2010). – Nikolai Ruhe Feb 22 '12 at 08:21
-
-
From the Apple Reference Library - "Getting the Current Language and Locale":
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:@"AppleLanguages"];
NSString* preferredLang = [languages objectAtIndex:0];

- 95,983
- 54
- 240
- 345
-
5`[NSLocale preferredLanguages]` is the official way to get the list of preferred labguages. But I think @Danya want's the name of the current keyboard layout. That's something else. – Nikolai Ruhe Sep 12 '09 at 11:20
-
Exactly. I need to change the content of the view depending of the current input language. So, I need to have a way to get the current keyboard language as well as a notification when user pressed the keyboard button to change language. – Sep 12 '09 at 12:08
-
This is helpful, because [NSLocale currentLocale] wont give you the user's UI language! – leviathan Jan 19 '11 at 10:18
-
OP was asking for the keyboard language; this has nothing to do with it. Also reading `AppleLanguages` preferences directly is a bad idea; if you really wanted the user's preferred language you can get it with NSLocale.preferredLanguages.firstObject. Note that this does *not* mean that your application is running in this language as the preferred language could be one your app does not support. – lensovet Jan 29 '17 at 05:57
In line with the top answers, the following is a generic solution to getting the keyboard language whenever it is changed. Register for the notification UITextInputCurrentInputModeDidChangeNotification
:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(inputModeDidChange:) name:UITextInputCurrentInputModeDidChangeNotification object:nil];
Then in inputModeDidChange
-(void)inputModeDidChange:(NSNotification *)notification {
UIView *firstResponder = [UIView currentFirstResponder];
UITextInputMode *currentInputMode = firstResponder.textInputMode;
NSString *keyboardLanguage = [currentInputMode primaryLanguage];
NSLog(@"%@", keyboardLanguage); // e.g. en-US
}
Where currentFirstResponder
is from a category on UIView to get the first responder view, as suggested in this SO post:
// UIView+Additions.h
#import <UIKit/UIKit.h>
@interface UIView (Additions)
+ (id)currentFirstResponder;
@end
Implementation
// UIView+Additions.m
#import "UIView+Additions.h"
static __weak id currentFirstResponder;
@implementation UIView (Additions)
+ (id)currentFirstResponder {
currentFirstResponder = nil;
// This will invoke on first responder when target is nil
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:)
to:nil
from:nil
forEvent:nil];
return currentFirstResponder;
}
- (void)findFirstResponder:(id)sender {
// First responder will set the static variable to itself
currentFirstResponder = self;
}
@end

- 1
- 1

- 1,454
- 1
- 14
- 24
The way I would do it is as follows:
Register your ViewController as a listener to
UIApplicationDidBecomeActiveNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
In applicationDidBecomeActive handler, check the current language using
[NSLocale preferredLanguages]
and act upon it accordingly.
This approach gives you what you want and is totally shippable without having to use private API.

- 33,765
- 9
- 83
- 112

- 1,313
- 11
- 15