开发者

change input source language programmatically OSx

I'm having problem to change a textfield input language source through codes !

All I want is to change the keyboard language to specific language when ever I enter a textfield.

I'm already searched here and just found some small guidance for Iphone but I'm working on OSx App.

Thank you guys helping me again !

EDIT:

well ! new approaches : If I change my system preferences to appropriate language then most of the problems solved ! Date formatter become true and if I use this code snippet I can achieve correct language name ( before I just got en):

NSLocale * locale = [NSLocale currentLocale];        
NSString * localLanguage = [locale objectForKey:NSLocaleLanguageCode] ;
NSLog (@"Language : %@", localLanguage);    

BUT, I want to change the keyboard input language without changing the whole system preference. In fact, I want to change the Text Input service language to other installed language ( I have 2 ) wh开发者_如何学Pythonen I enter into a special textfield ! ( is it clear ? )


Well, I answered my own question AGAIN !

for whom that may visit this question :
to get reached the user's keyboard language Input sources you have to follow this instructions :

  1. TIS ( Text Input Service ) is related to Carbon framework. So, first of all you have to import carbon.h to your implementation file:

    #import <Carbon/Carbon.h>

  2. Add carbon framework to your framework resource. To do this you have to navigate to Mac OSx Application Target -> Linked Frameworks and Libraries -> add carbon frame work.

  3. to change the keyboard input source, you can use for example controlTextDidBeginEditing delegate to detect user's textfield selection.Then you can select the proper language from installed language sources. for example I have two language installed, en and fa so I have 2 keyboard layout in language bar. Then you can select the language by selecting it's index.

  4. to find your desired language index you can use this :
    NSArray *langs=[NSLocale preferredLanguages];
    langs included by languages that you can reached by it's indexes.

  5. Now, it's time to change text input source programmatically using this codes :
    NSArray*langsArray=(NSArray*)TISCreateInputSourceList(NULL,FALSE); //make a list of installed languages
    TISInputSourceRef faSource=(TISInputSourceRef)[langsArray objectAtIndex:1]; //my second language is farsi (persian)
    TISSelectInputSource(faSource); // now second language selected for keyboard input resource

Hope it helps you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜