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
Related
Since the last update, I can't translate the language code (en, fr, etc...) into their respective names (English, French, etc...).
It works on a real device, but not in the emulator. It was working using former versions of Xcode. I'm aware that it's written in the release notes that [NSLocale currentLocale] may return en_US in some situations, but that doesn't explain why they are no more "translated". I use this code:
NSString* lang = #"en";
NSLog(#"%#",
[[NSLocale currentLocale]
displayNameForKey:NSLocaleIdentifier
value:lang]
);
which displays (null), instead of English.
The problem is that my app is crashing at some places because of that so I would like to know if there's a workaround.
The weird thing is that the following example, given by Apple, works really well.
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
NSString *displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:#"fr_FR"];
NSLog(#"displayNameString fr_FR: %#", displayNameString);
displayNameString = [frLocale displayNameForKey:NSLocaleIdentifier value:#"en_US"];
NSLog(#"displayNameString en_US: %#", displayNameString);
From Xcode release note
In some situations, [NSLocale currentLocale] may return en_US instead
of the chosen locale in the iOS 8.1 simulator. (18512161)
+ (NSLocale *)autoupdatingCurrentLocale NS_AVAILABLE(10_5, 2_0); // generally you should use this method
+ (NSLocale *)currentLocale; // an object representing the user's current locale
A workaround is to use the first one. Using objectForKey on currentLocal instance works.
NSString* lang = #"en_US";
NSLocale *currentLocal = [NSLocale autoupdatingCurrentLocale];
//get the localeIdentifier and create a new NSLocale.
id localIdentifier = [currentLocal objectForKey:NSLocaleIdentifier];
NSLocale *theLocal = [NSLocale localeWithLocaleIdentifier:localIdentifier];
NSLog(#"%#",[theLocal displayNameForKey:NSLocaleIdentifier
value:lang]
);
This is a known Apple issue for iOS 8.1 simulator only - not reproducible on 8.1 devices. See below issue description from Xcode 6.1 release notes:
Localization and Keyboard settings (including 3rd party keyboards) are
not correctly honored by Safari, Maps, and developer apps in the iOS
8.1 Simulator. [NSLocale currentLocale] returns en_US and only the English and Emoji keyboards are available. (18418630, 18512161).
See Xcode 6.1 Release Notes for more details.
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.
My application need to be localised in 2 languages: German and English. German should be Base language. It means that app should always localize to German except the language on device is english (in this case it should be on english)
I have custom *.string files for localisation and use localizedStringForKey:value:table to localise strings.
When I have only base localisation everything works fine. But in case if I add english localisation, in some reason localizedStringForKey:value:table just ignore Base localisation and always use English (for all languages)
Here how it looks like after I've added english:
and here is how I localise strings:
[[NSBundle mainBundle] localizedStringForKey:#"key" value:#"" table:#"Shared"]
I'm testing on simulator and here is my language screen:
Does anybody know what could be a problem? Thanks in advance!
As I mentioned in the comments, you need to set the Localization native development region (CFBundleDevelopmentRegion) in the Info.plist to your language code. Xcode seems to set it to a en_GB or de_DE style region code, setting it to de (no region) will fix it. Note that by default it is en, but selecting United Kingdom or Germany will change it to use the longer codes.
I have another solution, I think it works for you:
NSString* NSCustomLocalizedString( NSString *key , NSString *comment)
{
NSString *rs = nil;
if( [[NSUserDefaults standardUserDefaults] integerForKey:KEY_LANGUAGE ] == e_language_japanese)
{
rs = NSLocalizedStringFromTable(key,#"Localizable.strings-ja",nil);
}
else
{
rs = NSLocalizedStringFromTable(key,#"Localizable.strings-en",nil);
}
return rs;
}
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?
I have to make my application multi language supported on the selection of particular radio buttons(i.e. English, French, German etc...). For that what I have to do? any external framework or API is required?
To do so from where I have to start & if any inbuilt method is there then please suggest me.
I want to add another thing, can it be possible according to the selected language of the device we can get all the effect in application same as the selected device language?
In code you can access current locale and language that user set in Settings:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(#"Current Locale: %#", [[NSLocale currentLocale] localeIdentifier]);
NSLog(#"Current language: %#", currentLanguage);
You need to support Localization in the app for all the hardcoded strings.
The below code returns the code for the device language selected. eg: en, zh, etc..
let langStr = Locale.current.languageCode
Based on the code returned, the repective Localizable.strings file displays the value for the strings.