This question already has answers here:
Detecting current iPhone input language
(6 answers)
Closed 8 years ago.
Can we get the language of the current keyboard that user is using.. that is see wether the user is using English keyboard or Chinese Keyboard or some other language one…?
I can get the language of the device but i have to get the user's typing language.
Thanks in advance.
You can get like bellow
[UITextInputMode currentInputMode].primaryLanguage
From this documentation, you can further get the displayName of the language using NSLocale.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:[YOUR_TEXTFIELD_INSTANCE textInputMode].primaryLanguage];
NSLog(#"%#", [locale displayNameForKey:NSLocaleIdentifier value:[YOUR_TEXTFIELD_INSTANCE textInputMode].primaryLanguage]);
NSLog prints "English (United States)" if the primary language is En-US.
Note: currentInputMode is deprecated from iOS 7.0. Consider using textInputMode like above.
Related
This question already has an answer here:
NSLocale returning wrong value in iOS 11
(1 answer)
Closed 4 years ago.
When I change my language in iPhone settings it isn't reflected in NSLocale.current.language.
Is this because my app does not support Spanish? I would expect it to be reflected regardless. Ultimately I'm trying to get the ISO 639-1 (2-digit) region code for the users preferred language.
Code:
guard var preferredLanguage = NSLocale.preferredLanguages.first else { return }
for language in NSLocale.preferredLanguages {
print("preferredLanguage: \(language)")
}
print("languageCode: \(NSLocale.current.languageCode)")
print("regionCode: \(NSLocale.current.regionCode)")
Output:
preferredLanguage: es-US
preferredLanguage: haw-US
preferredLanguage: en-US
languageCode: Optional("en")
regionCode: Optional("US")
Since iOS 11, NSLocale.current can only return languages supported by your app.
More info can be found here:
https://stackoverflow.com/a/47678964/173623
https://jaanus.com/ios-11-changes-localized-date-handling/
When I have an UI string with capital letters, i'm used to define them in lowercase as for all the others, and then to use uppercaseStringWithLocale:[NSLocale currentLocale].
But recently I happened to notice that the [NSLocale currentLocale] may not be the one used in your app. For example if your device is in Turkish but your app only support english, the currentLocale would be a Turkish locale while your app is localized in english.
With those settings, a direct effect of using [NSLocale currentLocale] is that my uppercaseString will be "İ LİKE İOS" instead of "I LIKE IOS".
So far, the only workaround I see is to create a category of NSLocale to add a +(NSLocale*) applicationLocale; and use it in all uppercaseStringWithLocale:.
+ (NSLocale*) applicationLocale
{
NSMutableDictionary<NSString*,NSString*>* localeComponents = [[NSLocale componentsFromLocaleIdentifier:[NSLocale currentLocale].localeIdentifier] mutableCopy];
localeComponents[NSLocaleLanguageCode] = NSBundle.mainBundle.preferredLocalizations.firstObject;
return [NSLocale localeWithLocaleIdentifier:[NSLocale localeIdentifierFromComponents:localeComponents]];
}
My question is simple: am I doing this the right way or did I miss something? I indeed wonder why Apple links to currentLocale while it won't work as expected in a lot of cases.
The most robust way to get the application locale is to edit your Localizable.strings files. In the English localization file add an entry
"lang"="en";
in the German localization file add an entry
"lang"="de";
in the French localization file add an entry
"lang"="fr";
and so on... You can get the localization code with NSLocalizedString(#"lang").
I'm quite confusing, why in iOS 9 beta the return value of language code is different from iOS 8.4?
Function:
NSUserDefaults.standardUserDefaults().objectForKey("AppleLanguages")
Just set Language to "Simple Chinese" and Region to "China".
In iOS 8.4, return "zh-Hanz" but in iOS 9 beta 4 return "zh-Hanz-CN".
The Language ID syntax is much more like
"[language designator]-[script designator]-[region designator]".
Is different with apple document:
Is it a new rule in iOS 9? Can someone help me to confirm this.
Thank you for your help.
Yes, I noticed it too while using
[NSLocale preferredLanguages]
In iOS 9 beta it returned zh-Hans-US ( with region set to US and language to Chinese Simplified ) whereas in iOS 8.4 it used to return zh-Hans only.
So meanwhile I have been using
[[NSBundle mainBundle] preferredLocalizations]
This returns the current localization ( zh-hans ) which is without the region post-fix ( -US )
To confirm, iOS 9 adds the region post-fix ( like -US or -CN ) to all of the languages.
You should use componentsFromLocaleIdentifiert to split the locale identifier and find the language code. Like here:
+ (NSString*)currentLanguage
{
NSLocale* locale = [NSLocale currentLocale];
NSString* identifier = locale.localeIdentifier;
NSDictionary* components = [NSLocale componentsFromLocaleIdentifier:identifier];
NSLog(#"components:\n%#", components);
return [components valueForKey:NSLocaleLanguageCode];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *allLanguage = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [allLanguage objectAtIndex:0];
NSLog(#"The current language is : %#", currentLanguage);
iOS 9 Before:returns:language ID.for example:"zh-Hans"
iOS 9:returns:Language ID + region ID.for example:"zh-Hans-US"
Notice:
1.Don't use the following method to check the current language.
if ([currentLanguage isEqualToString:#"zh-Hans"])
you can use the following method instead:
if ([currentLanguage hasPrefix:#"zh-Hans"])
Also:If you care about Chinese,you must pay attention to these:
Simplified Chinese:zh-Hans
Traditional Chinese:zh-Hant
Hong Kong Chinese:zh-HK
Macao Chinese:zh-MO
Taiwan Chinese:zh-TW
Singapore Chinese:zh-SG
Notice:In some case,we have different result,especially when you deal with Chinese.It's a little strange.For example:
My mobile phone is:China Mainland/China Telecom/A1533/16GB
version.When I choose the language as Taiwan,the region is
China,HongKong or Taiwan,the result is "zh-TW",and when you set the
region as Not Chinese Region,the language will change to the
traditional Chinese.
So please pay more attention to the language especially when you deal with the language Chinese!
You can also check the iOS 9 adaption details here:https://github.com/ChenYilong/iOS9AdaptationTips
I have an iOS application that is localized in multiple languages. I have Localizable.strings files for English, Dutch and Dutch (Belgium). The problem is that when Dutch is set as the device language and Belgium as the country, the Dutch strings are used and NOT the Dutch-Belgium strings.
I've added some logging to make sure that the correct language is set on the device, and it appears to be correct:
NSLocale *locale = [NSLocale currentLocale];
NSString *language = [locale displayNameForKey:NSLocaleIdentifier
value:[locale localeIdentifier]];
NSLog(#"language %#", language);
This prints out
2015-04-09 11:49:26.227 MyTestApp[222:7459] language Nederlands (België)
But if I try to get a string using NSLocalizedString I get the Dutch resource, not the Dutch Belgium one.
I've tested this on an iPhone 5s running 8.1 and on several 7.1/8.1 simulators and I can't get it to work. The interesting thing is that if I forcefully set the language to Dutch (Belgium) in XCode under Edit Scheme -> Application Language, the correct language is used.
It turns out that the problem is that Xcode creates resource folders for each language with a hyphen separating the language and region, e.g. nl-BE.proj. However, the locale code uses an underscore, e.g. nl_BE, so the region is never matched. This appears to be an XCode bug.
The solution is to change the locale so that it includes a hyphen as discussed here https://stackoverflow.com/a/14357315/416214
This question already has answers here:
Getting current device language in iOS?
(33 answers)
Detect language in use by iOS application
(2 answers)
Closed 9 years ago.
I'm very new to iOS and Objective-C and I've been working on a project for an SMS app in which the app contains a text box for the message and that box has a counter for the characters but the counter depends on the language of the text, so my question is how can I identify/detect what language is the user using?
You can get user's prefered language list by using NSLocal:
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
if ([language isEqualToString:#"en"]) {
NSLog(#"user's prefered first language is English");
}
NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
Have a look at this: Getting current device language in iOS?