标签: nslocale

NSLocale识别

任何人都可以告诉我为什么以下代码在具有en_US语言环境标识符的机器中产生相反的结果?

if([[[NSLocale currentLocale] localeIdentifier] compare:@"en_US"] == NSOrderedSame)
{NSLog(@"True");} 
else
{NSLog(@"False");}


if([[NSLocale currentLocale] localeIdentifier] == @"en_USR") 
{NSLog(@"TRUE");} 
else    
{NSLog(@"FALSE");}  
Run Code Online (Sandbox Code Playgroud)

objective-c nslocale

0
推荐指数
1
解决办法
3828
查看次数

本地化问候消息的问题

我刚刚完成了我的第一个应用程序并且已经在本地化了不同的功能.我的应用程序中有一个功能,我不确定是否可以进行本地化.

基本上,当用户打开我的应用程序时,会收到一条消息,上面写着"下午好","早上好"或"晚上好".我创建了一些代码来检查时间的前缀,以决定显示什么消息,但是由于不同的国家/地区格式化时间不同,我不确定如何将其本地化.

我是否必须弄清楚它确实在哪些国家工作,并添加一个if语句来决定该应用是否可以显示此问候语?否则根据他们的名字显示问候语?

这是我的代码:

var date = NSDate()
    let dateFormatter = NSDateFormatter()
    dateFormatter.timeStyle = .ShortStyle
    let time = dateFormatter.stringFromDate(date)

    var currentTimeOfDay = ""

    if time.hasPrefix("0") {
        currentTimeOfDay = "morning"
    } else if time.hasPrefix("10") {
        currentTimeOfDay = "morning"
    } else if time.hasPrefix("11") {
        currentTimeOfDay = "morning"
    } else if time.hasPrefix("12") {
        currentTimeOfDay = "morning"
    } else if time.hasPrefix("13") {
        currentTimeOfDay = "afternoon"
    } else if time.hasPrefix("14") {
        currentTimeOfDay = "afternoon"
    } else if time.hasPrefix("15") {
        currentTimeOfDay = "afternoon"
    } else if …
Run Code Online (Sandbox Code Playgroud)

nslocale swift

0
推荐指数
1
解决办法
1112
查看次数

如何检测ios中的系统时间格式变化?

我想检测系统设置中完成的时间格式更改.我使用下面的代码,但它总是给我时间旧的格式.我怎样才能获得新的时间格式?

#pragma mark
#pragma mark - application change time format
-(void)applicationSignificantTimeChange:(UIApplication *)application
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[NSLocale currentLocale]];
    [formatter setDateStyle:NSDateFormatterNoStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    NSString *dateString = [formatter stringFromDate:[NSDate date]];
    NSLog(@"dataString ::%@",dateString);
    NSRange amRange = [dateString rangeOfString:[formatter AMSymbol]];
    NSRange pmRange = [dateString rangeOfString:[formatter PMSymbol]];
    is12Hour = (amRange.length > 0 || pmRange.length > 0);
}
Run Code Online (Sandbox Code Playgroud)

objective-c nsdate nsdateformatter ios nslocale

0
推荐指数
1
解决办法
1541
查看次数

使用 NSLocale、NSLocaleCountryCode 检查大陆的简单方法?

有没有办法检查用户当前所在的国家/地区是在欧洲、亚洲还是非洲?

\n\n

我正在开发一款为英国和美国做不同事情的应用程序。我想扩展该逻辑,因此如果用户的区域设置设置为欧洲国家/地区,那么我将默认为英国所做的操作。如果是在欧洲以外的地方,比如英国,那么我会默认我在美国所做的事情。

\n\n

这就是我现在所做的。

\n\n
NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];\nif ([countryCode isEqualToString:@"UK"]) {\n    NSString *startString = @"\xc2\xa3 ";\n}\nif ([countryCode isEqualToString:@"US"]) {\n    NSString *startString = @"$ ";\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

有没有一种简单的方法来检查大陆?

\n

objective-c ios nslocale

0
推荐指数
1
解决办法
1538
查看次数

NSDateformatter可用于阅读瑞典月份

我正在尝试解析日期列表.日期如下:"01MAY16,01JUN16,01AUG16"等等.到目前为止,所有日期都是英文版,我已成功使用NSDateFormatter,如下所示:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "ddMMMyy"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")

let date = dateFormatter.dateFromString("01MAY16")
Run Code Online (Sandbox Code Playgroud)

我现在偶然发现了一个问题.我解析的日期倾向于使用瑞典语语言环境显示:"01MAJ16,01JUN16,01OKT16".我希望通过将dateformatter中的语言环境设置为瑞典语来解析这个:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "ddMMMyy"
dateFormatter.locale = NSLocale(localeIdentifier: "sv_SE")

let date = dateFormatter.dateFromString("01MAJ16")
Run Code Online (Sandbox Code Playgroud)

当日期为"MAJ"时,这样可以正常工作,但仅限于该月是3个字母的月份.如果我尝试以下操作,则返回的日期为零:

let date = dateFormatter.dateFromString("01JUN16")
Run Code Online (Sandbox Code Playgroud)

它返回nil,因为它正在寻找的格式显然是'juni'.因此格式MMM并不意味着3个字母.

所以对我的问题.解决这个问题的最简单方法是什么?有没有我可以使用的技巧或我必须创建自己的格式化程序?

编辑

我从你那里学到了很多新东西,谢谢.手动设置shortMonthSymbols是一个很好的解决方案,但在我的情况下,我必须为挪威语,瑞典语和丹麦语语言环境做到这一点.我想出了一个覆盖NSDateFormatter的dateFromString()方法的解决方案.如果我做了一些非常愚蠢的事情,请告诉我.当我知道我解析的所有日期(任何语言环境)总是使用3个字母的月份时,使用此格式化程序:

class CustomDateFormatter : NSDateFormatter {

override func dateFromString(string: String) -> NSDate? {

    guard let date = super.dateFromString(string) else {

        //I only override the dateFormatter if no date is found and the dateFormat is set to exactly 3 M's (i.e …
Run Code Online (Sandbox Code Playgroud)

nsdateformatter date-formatting nslocale swift

0
推荐指数
1
解决办法
440
查看次数