Skip to content

Commit

Permalink
Localize AM/PM symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
richardtop committed Jan 12, 2020
1 parent b8a2dc5 commit c56a995
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CalendarKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|
s.name = "CalendarKit"
s.summary = "Fully customizable calendar for iOS"
s.version = "0.11.1"
s.version = "0.11.2"
s.homepage = "https://github.com/richardtop/CalendarKit"
s.license = 'MIT'
s.author = { "Richard Topchii" => "[email protected]" }
Expand Down
4 changes: 2 additions & 2 deletions Source/Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extension Bundle {
}
}

func localizedString(_ key: String) -> String? {
func localizedString(_ key: String) -> String {
return Bundle.localizationBundle?.localizedString(forKey: key,
value: nil,
table: nil)
table: nil) ?? key
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Foundation

enum Generator {
static func timeStrings24H() -> [String] {
struct TimeStringsFactory {
private let calendar: Calendar

init(_ calendar: Calendar = Calendar.autoupdatingCurrent) {
self.calendar = calendar
}

func make24hStrings() -> [String] {
var numbers = [String]()
numbers.append("00:00")

Expand All @@ -15,7 +21,7 @@ enum Generator {
return numbers
}

static func timeStrings12H() -> [String] {
func make12hStrings() -> [String] {
var numbers = [String]()
numbers.append("12")

Expand All @@ -24,9 +30,10 @@ enum Generator {
numbers.append(string)
}

var am = numbers.map { $0 + " AM" }
var pm = numbers.map { $0 + " PM" }
am.append("Noon")
var am = numbers.map { $0 + " " + calendar.amSymbol}
var pm = numbers.map { $0 + " " + calendar.pmSymbol}

am.append(localizedString("12:00"))
pm.removeFirst()
pm.append(am.first!)
return am + pm
Expand Down
11 changes: 9 additions & 2 deletions Source/Timeline/TimelineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public class TimelineView: UIView {
didSet {
snappingBehavior = snappingBehaviorType.init(calendar)
nowLine.calendar = calendar
regenerateTimeStrings()
setNeedsLayout()
}
}
Expand All @@ -128,8 +129,14 @@ public class TimelineView: UIView {
return is24hClock ? _24hTimes : _12hTimes
}

fileprivate lazy var _12hTimes: [String] = Generator.timeStrings12H()
fileprivate lazy var _24hTimes: [String] = Generator.timeStrings24H()
private lazy var _12hTimes: [String] = TimeStringsFactory(calendar).make12hStrings()
private lazy var _24hTimes: [String] = TimeStringsFactory(calendar).make24hStrings()

private func regenerateTimeStrings() {
let factory = TimeStringsFactory(calendar)
_12hTimes = factory.make12hStrings()
_24hTimes = factory.make24hStrings()
}

public lazy var longPressGestureRecognizer = UILongPressGestureRecognizer(target: self,
action: #selector(longPress(_:)))
Expand Down

0 comments on commit c56a995

Please sign in to comment.