Skip to content

Commit

Permalink
Minor stability changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MatsMoll committed May 6, 2019
1 parent 2175660 commit 50adfa2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ struct SomeView: LocalizedTemplate {
/// Used to identify the locale
static let localePath: KeyPath<Context, String>? = \.locale

enum StringKeys: String {
enum LocalizationKeys: String {
case hello
case missingValues = "missing.values"
case footer
Expand Down
24 changes: 15 additions & 9 deletions Sources/HTMLKit/DateVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct DateVariable<T: ContextualTemplate>: CompiledTemplate {
/// The key path to the date to render
let dateReferance: Referance

/// A bool indicating if the date format is used
/// This is used to bypass a Linux bug
let usingDateFormate: Bool


// View `CompiledTemplate`
func render<T>(with manager: HTMLRenderer.ContextManager<T>) throws -> String {
Expand All @@ -51,9 +55,11 @@ struct DateVariable<T: ContextualTemplate>: CompiledTemplate {
throw Errors.unableToCopyFormatter
}
formatterCopy.locale = .init(identifier: locale)
// Used to bypass a Linux bug
formatterCopy.dateStyle = formatter.dateStyle
formatterCopy.timeStyle = formatter.timeStyle
if !usingDateFormate {
// Used to bypass a Linux bug
formatterCopy.dateStyle = formatter.dateStyle
formatterCopy.timeStyle = formatter.timeStyle
}
return formatterCopy.string(from: date)
} else {
return formatter.string(from: date)
Expand Down Expand Up @@ -81,7 +87,7 @@ extension ContextualTemplate {
let formatter = DateFormatter()
formatter.dateStyle = dateStyle
formatter.timeStyle = timeStyle
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath))
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath), usingDateFormate: false)
}

/// Render a date in a formate
Expand All @@ -91,7 +97,7 @@ extension ContextualTemplate {
/// - formatter: The DateFormatter to use when rendering the string
/// - Returns: A `CompiledTemplate`
public func date(_ datePath: KeyPath<Context, Date>, formatter: DateFormatter) -> CompiledTemplate {
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath))
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath), usingDateFormate: true)
}

/// Render a date in a formate
Expand All @@ -103,7 +109,7 @@ extension ContextualTemplate {
public func date(_ datePath: KeyPath<Context, Date>, format: String) -> CompiledTemplate {
let formatter = DateFormatter()
formatter.dateFormat = format
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath))
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath), usingDateFormate: true)
}

/// Render a date in a formate
Expand All @@ -117,7 +123,7 @@ extension ContextualTemplate {
let formatter = DateFormatter()
formatter.dateStyle = dateStyle
formatter.timeStyle = timeStyle
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath))
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath), usingDateFormate: false)
}

/// Render a date in a formate
Expand All @@ -129,7 +135,7 @@ extension ContextualTemplate {
public func date(_ datePath: KeyPath<Context, Date?>, format: String) -> CompiledTemplate {
let formatter = DateFormatter()
formatter.dateFormat = format
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath))
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath), usingDateFormate: true)
}

/// Render a date in a formate
Expand All @@ -139,6 +145,6 @@ extension ContextualTemplate {
/// - formatter: The DateFormatter to use when rendering the string
/// - Returns: A `CompiledTemplate`
public func date(_ datePath: KeyPath<Context, Date?>, formatter: DateFormatter) -> CompiledTemplate {
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath))
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath), usingDateFormate: true)
}
}
14 changes: 7 additions & 7 deletions Sources/HTMLKit/LocalizedTemplate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@
public protocol LocalizedTemplate: ContextualTemplate {

/// A data type containing the different localization string keys
associatedtype StringKeys: RawRepresentable
associatedtype LocalizationKeys: RawRepresentable

/// The path to the used locale
/// This is a optional in order to inheret a locale from a superview
static var localePath: KeyPath<Context, String>? { get }
}

extension LocalizedTemplate where StringKeys.RawValue == String {
extension LocalizedTemplate where LocalizationKeys.RawValue == String {

/// Localize a key to a locale
///
/// - Parameters:
/// - key: The key describing the text
/// - Returns: A text with the localized string
/// - Throws: If some part of the localization went wrong
public func localize(_ key: StringKeys) -> CompiledTemplate {
public func localize(_ key: LocalizationKeys) -> CompiledTemplate {
return localize(key: key.rawValue)
}

Expand All @@ -35,7 +35,7 @@ extension LocalizedTemplate where StringKeys.RawValue == String {
/// - contentPath: The path to the content needed to render the string
/// - Returns: A text with the localized string
/// - Throws: If some part of the localization went wrong
public func localize<C: Encodable>(_ key: StringKeys, with contentPath: KeyPath<Context, C>) -> CompiledTemplate {
public func localize<C: Encodable>(_ key: LocalizationKeys, with contentPath: KeyPath<Context, C>) -> CompiledTemplate {
return localize(key: key.rawValue, with: contentPath)
}

Expand All @@ -46,7 +46,7 @@ extension LocalizedTemplate where StringKeys.RawValue == String {
/// - contentPath: The path to the content needed to render the string
/// - Returns: A text with the localized string
/// - Throws: If some part of the localization went wrong
public func localize(_ key: StringKeys, with content: [String : CompiledTemplate]) -> CompiledTemplate {
public func localize(_ key: LocalizationKeys, with content: [String : CompiledTemplate]) -> CompiledTemplate {
return localize(key: key.rawValue, with: content)
}

Expand Down Expand Up @@ -84,15 +84,15 @@ extension LocalizedTemplate where StringKeys.RawValue == String {
}
}

extension LocalizedTemplate where StringKeys.RawValue == String, Context: Encodable {
extension LocalizedTemplate where LocalizationKeys.RawValue == String, Context: Encodable {

/// Localize a key to a locale
///
/// - Parameters:
/// - key: The key describing the text
/// - Returns: A text with the localized string
/// - Throws: If some part of the localization went wrong
public func localizeWithContext(_ key: StringKeys) -> CompiledTemplate {
public func localizeWithContext(_ key: LocalizationKeys) -> CompiledTemplate {
return localizeWithContext(key: key.rawValue)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/HTMLKitTests/HTMLTestDocuments.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ struct LocalizedView: LocalizedTemplate {

static let localePath: KeyPath<LocalizedView.Context, String>? = \.locale

enum StringKeys: String {
enum LocalizationKeys: String {
case helloWorld = "hello.world"
case unreadMessages = "unread.messages"
}
Expand Down Expand Up @@ -414,7 +414,7 @@ struct DateView: ContextualTemplate {

struct LocalizedDateView: LocalizedTemplate {

enum StringKeys: String {
enum LocalizationKeys: String {
case none
}

Expand Down

0 comments on commit 50adfa2

Please sign in to comment.