My App is translated into several languages, but for now I want to force the language to Dutch. I did force the language to Dutch by setting the AppleLanguages key in the standardUserDefaults to Dutch. This is working for localized strings and xib files.
But the NSDateFormatter seems to ignore this value completely. It's just using the iPhones en_US locale to format the date, even if I set the locale myself on the NSDateFormatter object:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *dutch = [[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"];
[dateFormatter setLocale:dutch];
[dutch release];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
dateLabel.text = [dateFormatter stringFromDate:[BabyInfo getDate]];
[dateFormatter release];
Does anyone have a clue how to solve this?
Update: So it seems the all the date stuff (like NSDateFormatter and NSDatePicker) are not using the language locale to determine what type of date to show, but are looking at the location locale. Still, I don't know how to tell the formatter and picker that I want them to show their dates in Dutch. So any help is still welcome, but at least I now understand what the problem is.
Maybe this answer comes too late but check this out:
http://www.alexcurylo.com/blog/2011/02/16/tip-nsdateformatter-localization/
This will force the Formatter/Picker to use the locale with the preferred language without having to change your iPhone/iPad settings.
To use the selected language for your date formatter use:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]]];
Related
I noticed a nifty setting in the xcode simulator while running that I can change the current location and hence simulate location based tests
However, when I try to get the date using NSDateFormatter, the local date is still in PST. I am in the Pacific Time Zone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle]; // Jan 1, 2010
[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; // 1:43 PM
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
[usLocale release];
NSDate *testDate = [NSDate date];
NSLog(#"Local date: %#", [dateFormatter stringFromDate:testDate]);
According to the docs, stringFromDate should use the receivers current setting, which should use the timeZone of the Tokyo, Japan.
Is this expected?
Chech the - timezone-attribute of the NSDateFormatter - I think this is controlled by the device itself (or your computer in this case), not the location. There is a setting in the settings-app however that lets you set the time by location, I'm not sure if that would change anything, but try activating that.
And a general tips: always test it on a device as well - I think the "Simulate Location"-setting works on the phone as well.
Edit: Just saw an answer that could be relevant, try calling [NSTimeZone resetTimeZone]; after setting a new location. That should trigger a reset and hopefully display the correct timezone. Ref: https://stackoverflow.com/a/5987409/267892
I'm having a bit of a problem with NSDateFormatter failing on one user's device (returning nil when parsing a string) and working perfectly when I run it locally (either in the simulator or on my device).
I'm trying to rule out what could be causing a difference in this behaviour. My first thought was the locale but I've tried setting it explicitly to ensure the same locale is always used but it makes no difference.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatter setLocale:locale];
[locale release];
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSLog(#"PARSING DATE %# AS %#", dateString, theDate);
On the failing device, I get:
PARSING DATE 2010-11-28T20:30:49-0000 AS (null)
But locally I get:
PARSING DATE 2010-11-28T20:30:49-0000 AS 2010-11-28 20:30:49 +0000
This is driving me crazy, am I missing something else?
I am running 4.2 locally (simulator) and on my device (an iPhone 4). The failing device is a 3GS running 4.2.1.
Any ideas would be much appreciated!
I'm pleased to say that I eventually got to the bottom of this issue and I must pass on my thanks to #bendodson on Twitter for helping me out with this. aBitObvious also hit on the issue in his comment above; I'd have up-voted him if I could.
There was one difference between the user's device and mine, and that was that his device was set to use the 12 hour clock and mine was not. This single thing meant that the NSDateFormatter was unable to parse the time in the above examples and returned nil.
By far the biggest issue for me with this problem was being unable to reproduce the problem locally!
So, to be clear, to solve this issue; that is, if you are parsing date/time strings that are in a known, fixed format (often coming from some API as this was in my case), you should set the correct locale for the date formatter, which will often be en_US_POSIX.
...
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:locale];
[locale release];
For more information on this, read Apple QA1480.
What am I doing wrong?
I am trying to getNSDateFormatter to translate custom patterns for dates using the current locale.
Example:
dateFormat = [[NSDateFormatter alloc] init];
dateFormat.locale = [NSLocale currentLocale];
[dateFormat setDateFormat:#"MMM"];
output = [self.dateFormat stringFromDate:dateObject];
No matter what I change my current locale settings to, I always see the English month abbreviations.
Thanks for any help you can provide.
It looks like it was working all of the time. I had been leaving my location set to United States and changing only the language. To get it to work correctly you have to change the location as well as the language.
According to this site:
http://iosdevelopertips.com/cocoa/date-formatter-examples.html
there is a class that handles formatting, which takes in a set of constants/enums (e.g. NSDateFormatterShortStyle) to the "setDateStyle" property/method.
Somehow the NSDateFormatter knows to retrieve the proper locale-specific date format. What I want is to be able to retrieve the default formats based on the user's choice of region format. I have a feeling it is stored in NSLocale, but that does not seem to expose anything that will retrieve the format strings.
Is there a way to extract the formats? It has to be in memory somewhere; I'm hoping the retrieval mechanism is exposed somewhere.
I've looked in several places, but the only answers I get are a lesson on how to create an NSDate from a custom format.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
NSString *dateFormat = [df dateFormat];
NSLog(#"Date format: %#", dateFormat);
[df release];
Just tested on OS X but this should also work in iOS.
In particular I'm interested in shortStandaloneWeekdaySymbols. I'm using this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSArray *shortWeekdaySymbols = [dateFormatter shortStandaloneWeekdaySymbols];
But if iPhone region format is set to US but language to French/German/any other, NSDateFormatter returns English strings (mon tue ...). But I want to respect the language settings and get weekday names in the current language (standard Clock app does this for example). Is this possible?
The only way I can think of to do this would be to get the current user language (how?) and set locale on the date formatter to this language's region.
The only way you can think of, "get the current user language (how?) and set locale on the date formatter to this language's region" is correct. This is how.
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale preferredLanguages] objectAtIndex:0]] autorelease]];
Try to look here
Detecting current iPhone input language