views:

1175

answers:

2

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?

+3  A: 

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];
Alex Reynolds
`[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
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.
Danya
+1  A: 

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!

Nikolai Ruhe
Please explain downvote.
Nikolai Ruhe
You might have gotten the neg because you're describing code that can't be shipped using the SDK.
Shaggy Frog
Interesting, but not useful--if I indeed cannot use in "shipping" code.
bentford
I 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