问题描述
我想构建一个支持多语言的 iphone 应用程序.在那里,我想以用户从我的应用程序设置中的一组语言中选择的语言输入值.因此,它应该显示选定的语言键盘.
i want to build an iphone application which supports multi-language. in that i want to input values in the language which user has selected from a set of languages in my application's settings. accordingly, it should show selected language keyboard.
例如:如果用户从我的应用程序设置中选择了法语.然后在我的应用程序的所有 uitextfields 中,用户应该能够用法语输入值.因此,当键盘打开时,我需要确保它是每次显示的法语键盘,而不是默认语言键盘.
for eg : if user has selected french language from my application's setting. then in all the uitextfields of my application, user should be able to input values in french language. so for that when keyboard opens i need to make sure that it is french language keyboard which it shows every time instead of default language keyboard.
所以问题是,是否可以根据应用程序设置中选择的语言在用户点击 uitextfield 时显示特定语言键盘?如果是那怎么办?
so the question is, is it possible to show specific language keyboard whenever user taps on uitextfield based on the language selected in the application's settings? if yes then how?
推荐答案
你需要为你的 uitextview 重写 textinputmode
you need to override textinputmode for your uitextview
@implementation uitextview (localization) - (uitextinputmode *) textinputmode { for (uitextinputmode *inputmode in [uitextinputmode activeinputmodes]) { if ([[self langfromlocale:[self localekey]] isequaltostring:[self langfromlocale:inputmode.primarylanguage]]) return inputmode; } return [super textinputmode]; } - (nsstring*) localekey { nsarray* lang = [[nsuserdefaults standarduserdefaults]objectforkey:@"applelanguages"]; nsstring* currentlang = lang[0]; return currentlang; } - (nsstring *)langfromlocale:(nsstring *)locale { nsrange r = [locale rangeofstring:@"_"]; if (r.length == 0) r.location = locale.length; nsrange r2 = [locale rangeofstring:@"-"]; if (r2.length == 0) r2.location = locale.length; return [[locale substringtoindex:min(r.location, r2.location)] lowercasestring]; }
您现在可以更改输入语言:
and you now can chan change input lang:
[[nsuserdefaults standarduserdefaults] setobject:@[@"fr"] forkey:@"applelanguages"]; [[nsuserdefaults standarduserdefaults] synchronize];