我正试图找到一种方法来获取当天的时间。显然有一种简单的方法Application to Display Morning,Evening 如果您对一种语言的静态单词感到满意。有没有办法根据Locale制作它? NSDateComponentsFormatter似乎没有做到这一点。
答案 0 :(得分:20)
不幸的是,没有内置的解决方案 - NSDateFormatter的相对格式仅适用于每天的基础。
获取Calendar.current.component(.hour, from: Date())
小时,并使用范围切换和NSLocalizedString()
来定位字符串。
例如:
// let hour = NSCalendar.currentCalendar().component(.Hour, fromDate: NSDate()) Swift 2 legacy
let hour = Calendar.current.component(.hour, from: Date())
switch hour {
case 6..<12 : print(NSLocalizedString("Morning", comment: "Morning"))
case 12 : print(NSLocalizedString("Noon", comment: "Noon"))
case 13..<17 : print(NSLocalizedString("Afternoon", comment: "Afternoon"))
case 17..<22 : print(NSLocalizedString("Evening", comment: "Evening"))
default: print(NSLocalizedString("Night", comment: "Night"))
}
创建文件localizable.strings
并添加所需的本地化。
答案 1 :(得分:1)
以下是我如何使用Swift 2解决问题的方法。首先,我使用this article来识别当天的各个部分。从那里,我使用了一系列if / else if语句。 我很好奇其他人是否可以使用范围来做到这一点。
//BIG PICTURE SOLUTION
//Step 1: Build a .plist or REST API service or whatever made up of different ways to describe "parts of the day" in different languages.
//http://stackoverflow.com/questions/3910244/getting-current-device-language-in-ios
//List of Language Codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
//Step 2: Get the user's local time zone
//Step 3: Calculate whether the user's local time fits within these buckets of time
import Foundation
class DayParts{
var currentHour:Int
var localLang:String?
// IDEA: Build a .plist or a REST API service or whatever that simply returns a dictiontary
let letterCodes:[String:Array<String>] = [
"en": ["Early Morning", "Late Morning", "Early Afternoon", "Late Afternoon", "Evening", "Night"],
"fr": ["Tôt le matin", "Tard dans la matinée", "Début d'après-midi", "Tard dans l'après-midi", "Soir", "Nuit"],
"es": ["Mañana Temprano", "Mañana tarde", "Temprano en la tarde", "Fin de la tarde", "Anochecer", "Noche"]
]
init(){
//A. Get the current time
let date = NSDate()
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH"
//B. Get the current hour
currentHour = Int(dateFormatter.stringFromDate(date))!
//C. Get the current phone language
localLang = NSLocale.currentLocale().objectForKey(NSLocaleLanguageCode) as? String
}
func now() -> String {
if(currentHour < 08){
return letterCodes[localLang!]![0]
}
else if(currentHour < 11){
return letterCodes[localLang!]![1]
}
else if( currentHour < 15){
return letterCodes[localLang!]![2]
}
else if( currentHour < 17){
return letterCodes[localLang!]![3]
}
else if(currentHour < 21){
return letterCodes[localLang!]![4]
}
else{
return "Night"
}
}
}
let dayParts = DayParts().now()
答案 2 :(得分:0)
实际上,您可以像NSDateFormatter
这样设置区域设置:
let df = NSDateFormatter()
df.locale = NSLocale.currentLocale()
此日期格式化程序将帮助您在currentLocale
。
但是对于你所期望的,你必须实现本地化才能获得“早晨”,“下午”,“夜晚”字符串的本地化字符串。
NSDateFormatter
- doesRelativeDateFormatting
中有一个属性。它会将日期格式化为正确语言环境中的相对日期。
如果日期格式化程序使用相对日期格式,则尽可能使用它 用短语替换其输出的日期组件,例如 “今天”或“明天” - 表示相对日期。可用 短语取决于日期格式化程序的区域设置;而对于 在将来的日期,英语可能只允许“明天”,法语可能 允许“后天后天”,如图所示 以下示例。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterNoStyle;
dateFormatter.dateStyle = NSDateFormatterMediumStyle;
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
dateFormatter.locale = frLocale;
dateFormatter.doesRelativeDateFormatting = YES;
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(@"dateString: %@", dateString);
// Output
// dateString: après-après-demain