How to identify the user's language in iOS? [duplicate] - ios

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?

Related

IOS Calling Phone number with using * and # add in number [duplicate]

This question already has answers here:
Phone call number with hashtag on iOS
(3 answers)
Closed 4 years ago.
In iOS i want to call calling dial pad when I am tried to a normal number and using + and - symbols then it's working good, but when I am trying to add * and # Tag then call dial is not open. Please give me solution how can i do this
Here is My CODE
NSString *phoneNumber = [#"tel://" stringByAppendingString:#"*123*12335553#"];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Please replace your character : [<your string> stringByReplacingOccurrencesOfString:#"*" withString:#""]; from your string.

How to translate app in to another language in iOS?

I am developing an ecommerce app. Initially the app is in the English language and now I want to convert it into the Chinese and French language. I referred to this link
So what I have understood is that every text we need to convert into French and Chinese, in the respective string files for the static data, but I am getting data from the backend. So then how do I convert that text dynamically?
You can use this code. And in Project you can add localisations in info.plist.Hope it will help you.Thank You
NSLocale* curentLocale = [NSLocale currentLocale];
namearray=[NSMutableArray arrayWithObjects:NSLocalizedString(#"Hellokey1",#""),NSLocalizedString(#"Hellokey2",#""),NSLocalizedString(#"Hellokey3",#""),NSLocalizedString(#"Hellokey4",#""),NSLocalizedString (#"Hellokey5",#""),NSLocalizedString(#"Hellokey6",#""),NSLocalizedString(#"Hellokey7",#""),NSLocalizedString(#"Hellokey8",#""),NSLocalizedString(#"Hellokey9",#""),NSLocalizedString(#"Hellokey10",#""),NSLocalizedString(#"Hellokey11",#""),NSLocalizedString(#"Hellokey12",#""),NSLocalizedString(#"Hellokey13",#""),NSLocalizedString(#"Hellokey14",#""),NSLocalizedString(#"Hellokey15",#""),NSLocalizedString(#"Hellokey16",#""),NSLocalizedString(#"Hellokey17",#""),NSLocalizedString(#"Hellokey18",#""),NSLocalizedString(#"Hellokey19",#""),NSLocalizedString(#"Hellokey20",#""),NSLocalizedString(#"Hellokey21",#""),NSLocalizedString(#"Hellokey22",#""),NSLocalizedString(#"Hellokey23",#""),NSLocalizedString(#"Hellokey24",#""),NSLocalizedString(#"Hellokey25",#""),NSLocalizedString(#"Hellokey26",#""),NSLocalizedString(#"Hellokey27",#""),nil];
[curentLocale displayNameForKey:NSLocaleIdentifier
value:[curentLocale localeIdentifier]];
// NSString *path = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
// NSLog(#"path:%#",path);
NSString *language = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
If the text received is truly dynamic (i.e. there's no way to know ahead of time what the possible variations are), there's no way to do it. If you know what the possibilities are, you embed the translations just as you do for static text, and you have the server send the localization key. This is the scheme used by iOS for translating notification alerts.
(You could theoretically use a translation API, such as Google Translate, but that has many downsides, and few upsides.)

iOS 9 beta Language ID syntax change

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

iOS Get Language of Keyboard [duplicate]

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.

How to get created date of file from document directory in iOS? [duplicate]

This question already has answers here:
reading time stamp from files in Cocoa?
(2 answers)
Closed 9 years ago.
Yes i would like to get the created date of file from document directory in iOS.
It's like NOTES App from iOS.
In Notes App , there is a date that created and modified of file.
I also want to get date like that.
How can i do that?
Thanks for any help.
NSError* error = nil;
NSDictionary *file_info = [[NSFileManager defaultManager] attributesOfItemAtPath: your_path error:&error];
NSDate *modified = file_info[NSFileModificationDate];
NSDate *created = file_info[NSFileCreationDate];

Resources