0

I am just finishing up my first app and have been localising different features. I have one feature in my app that I am not sure if I can localise or not.

Basically, when users open my app, they are greeted with a message that says 'good afternoon', 'good morning' or 'good evening'. I created some code that checks the prefix of the time to decide what message to display, however since different countries format time differently, I'm not sure how I can localise this.

Will I have to figure out the countries that it does work in and add an if statement that decides whether the app can display this greeting or not? Otherwise display a greeting based on their name instead?

Here is my code:

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 time.hasPrefix("16") {
        currentTimeOfDay = "afternoon"
    } else if time.hasPrefix("17") {
        currentTimeOfDay = "afternoon"
    } else if time.hasPrefix("18") {
        currentTimeOfDay = "evening"
    } else if time.hasPrefix("19") {
        currentTimeOfDay = "evening"
    } else if time.hasPrefix("2") {
        currentTimeOfDay = "evening"
    }
user3746428
  • 11,047
  • 20
  • 81
  • 137

2 Answers2

6

You should not use a localized time string to determine the time of the day.

Use NSCalendar and NSDateComponents:

let now = NSDate()
let cal = NSCalendar.currentCalendar()
let comps = cal.components(.CalendarUnitHour, fromDate: now)
let hour = comps.hour

Now hour is an integer in the range 0 ... 23.

var currentTimeOfDay = ""
switch hour {
case 0 ... 12:
    currentTimeOfDay = "morning"
case 13 ... 17:
    currentTimeOfDay = "afternoon"
default:
    currentTimeOfDay = "evening"
}
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Ah! Thanks a lot! I should have been using the range operator in my original code but I completely forgot about it. – user3746428 Aug 29 '14 at 15:04
  • @user3746428: You are welcome. Have also a look at the `NSLocalizedString()` methods to get the output localized to the device settings. – Martin R Aug 29 '14 at 15:05
  • I have just tried my app on an iOS 7 device for the first time and I am receiving an error. When I comment out this code the app runs fine but when I leave it in I get this error '-[_NSCopyOnWriteCalendarWrapper component:fromDate:]: unrecognized selector sent to instance 0x175b5580' – user3746428 Aug 29 '14 at 21:07
  • @user3746428: The `component()` method was introduced with iOS 8. I have changed the code such that it should work now on iOS 7 as well. – Martin R Aug 29 '14 at 21:13
  • Ah, I assumed that must have been the issue. It wouldn't have been a problem but I just decided to change my deployment target from iOS 8 to 7. Your change fixed the issue though so thanks again! – user3746428 Aug 29 '14 at 21:17
4

For those who are using Swift 4.0

    let dateComponents = Calendar.current.dateComponents([.hour], from: Date())

    if let hour = dateComponents.hour {
      let greetingString: String
      switch hour {
      case 0..<12:
        greetingString = "Good morning"
      case 12..<17:
        greetingString = "Good afternoon"
      default:
        greetingString = "Good evening"
      }
   }
San
  • 1,796
  • 13
  • 22
  • Well, that's crazy, I can only edit when there are more than 6 characters to add! Anyways, you're missing the closing parentheses on the `if let hour` block. – Michael Volo Jun 17 '18 at 13:28