Skip to content

Commit

Permalink
Prepare notifications for localization
Browse files Browse the repository at this point in the history
  • Loading branch information
daneden committed Oct 1, 2023
1 parent c3b386c commit 47ae3c0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 22 deletions.
15 changes: 15 additions & 0 deletions Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
}
}
},
"(differenceString) less daylight than yesterday" : {
"comment" : "Notification fragment for less daylight compared to yesterday"
},
"(differenceString) more daylight than yesterday" : {
"comment" : "Notification fragment for more daylight compared to yesterday"
},
"(duration) of daylight today." : {
"comment" : "Notification fragment for length of daylight"
},
"**Thank you so much for your support.** Feel free to leave another tip in the future if you’re feeling generous." : {
"localizations" : {
"it" : {
Expand Down Expand Up @@ -1152,6 +1161,9 @@
}
}
},
"Open Solstice to see how today’s daylight has changed." : {
"comment" : "Fallthrough notification content for when notification settings specify no content."
},
"Open Source Acknowledgements" : {
"localizations" : {
"it" : {
Expand Down Expand Up @@ -1582,6 +1594,9 @@
}
}
},
"The sun rises at (sunriseTime) and sets at (sunsetTime)." : {
"comment" : "Notification fragment for sunrise and sunset times"
},
"Time" : {
"localizations" : {
"it" : {
Expand Down
55 changes: 33 additions & 22 deletions Solstice/Helpers/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,31 +168,42 @@ class NotificationManager: NSObject, UNUserNotificationCenterDelegate, Observabl
}
}

if includeSunTimes {
content.body += "The sun rises at \(solar.safeSunrise.withTimeZoneAdjustment(for: timeZone).formatted(.dateTime.hour().minute())) "
content.body += "and sets at \(solar.safeSunset.withTimeZoneAdjustment(for: timeZone).formatted(.dateTime.hour().minute())). "
}

if includeDaylightDuration {
content.body += "\(duration) of daylight today. "
}

if includeDaylightChange {
if !(difference < 0 && sadPreference == .removeDifference) || context == .preview {
content.body += "\(differenceString) \(difference >= 0 ? "more" : "less") daylight than yesterday. "
@StringBuilder var notificationBody: String {
if includeSunTimes {
let sunriseTime = solar.safeSunrise.withTimeZoneAdjustment(for: timeZone).formatted(.dateTime.hour().minute())
let sunsetTime = solar.safeSunset.withTimeZoneAdjustment(for: timeZone).formatted(.dateTime.hour().minute())
NSLocalizedString(
"The sun rises at \(sunriseTime) and sets at \(sunsetTime).",
comment: "Notification fragment for sunrise and sunset times"
)
}

if includeDaylightDuration {
NSLocalizedString(
"\(duration) of daylight today.",
comment: "Notification fragment for length of daylight"
)
}

if includeDaylightChange {
if !(difference < 0 && sadPreference == .removeDifference) || context == .preview {
if difference >= 0 {
NSLocalizedString("\(differenceString) more daylight than yesterday", comment: "Notification fragment for more daylight compared to yesterday")
} else {
NSLocalizedString("\(differenceString) less daylight than yesterday", comment: "Notification fragment for less daylight compared to yesterday")
}
}
}

if !includeDaylightChange && !includeDaylightDuration && !includeSolsticeCountdown && !includeSunTimes {
NSLocalizedString(
"Open Solstice to see how today’s daylight has changed.",
comment: "Fallthrough notification content for when notification settings specify no content."
)
}
}

if includeSolsticeCountdown {
content.body += "The next solstice is \(solar.date.nextSolstice.formatted(.relative(presentation: .named))). "
}

/**
Fallthrough for when notification settings specify no content
*/
if !includeDaylightChange && !includeDaylightDuration && !includeSolsticeCountdown && !includeSunTimes {
content.body += "Open Solstice to see how today’s daylight has changed."
}
content.body = notificationBody

return content
}
Expand Down
16 changes: 16 additions & 0 deletions Solstice/Helpers/StringBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,20 @@ struct StringBuilder {
static func buildBlock(_ parts: String...) -> String {
parts.joined(separator: " ")
}

static func buildEither(first component: String) -> String {
return component
}

static func buildEither(second component: String) -> String {
return component
}

static func buildArray(_ components: [String]) -> String {
components.joined(separator: "\n")
}

static func buildOptional(_ component: String?) -> String {
return component ?? ""
}
}

0 comments on commit 47ae3c0

Please sign in to comment.