diff --git a/README.md b/README.md index 9d409566..a697a5b7 100644 --- a/README.md +++ b/README.md @@ -236,7 +236,7 @@ struct SomeView: LocalizedTemplate { /// Used to identify the locale static let localePath: KeyPath? = \.locale - enum StringKeys: String { + enum LocalizationKeys: String { case hello case missingValues = "missing.values" case footer diff --git a/Sources/HTMLKit/DateVariable.swift b/Sources/HTMLKit/DateVariable.swift index 2b722311..835ecc1b 100644 --- a/Sources/HTMLKit/DateVariable.swift +++ b/Sources/HTMLKit/DateVariable.swift @@ -29,6 +29,10 @@ struct DateVariable: 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(with manager: HTMLRenderer.ContextManager) throws -> String { @@ -51,9 +55,11 @@ struct DateVariable: 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) @@ -81,7 +87,7 @@ extension ContextualTemplate { let formatter = DateFormatter() formatter.dateStyle = dateStyle formatter.timeStyle = timeStyle - return DateVariable(formatter: formatter, dateReferance: .solid(datePath)) + return DateVariable(formatter: formatter, dateReferance: .solid(datePath), usingDateFormate: false) } /// Render a date in a formate @@ -91,7 +97,7 @@ extension ContextualTemplate { /// - formatter: The DateFormatter to use when rendering the string /// - Returns: A `CompiledTemplate` public func date(_ datePath: KeyPath, formatter: DateFormatter) -> CompiledTemplate { - return DateVariable(formatter: formatter, dateReferance: .solid(datePath)) + return DateVariable(formatter: formatter, dateReferance: .solid(datePath), usingDateFormate: true) } /// Render a date in a formate @@ -103,7 +109,7 @@ extension ContextualTemplate { public func date(_ datePath: KeyPath, format: String) -> CompiledTemplate { let formatter = DateFormatter() formatter.dateFormat = format - return DateVariable(formatter: formatter, dateReferance: .solid(datePath)) + return DateVariable(formatter: formatter, dateReferance: .solid(datePath), usingDateFormate: true) } /// Render a date in a formate @@ -117,7 +123,7 @@ extension ContextualTemplate { let formatter = DateFormatter() formatter.dateStyle = dateStyle formatter.timeStyle = timeStyle - return DateVariable(formatter: formatter, dateReferance: .optional(datePath)) + return DateVariable(formatter: formatter, dateReferance: .optional(datePath), usingDateFormate: false) } /// Render a date in a formate @@ -129,7 +135,7 @@ extension ContextualTemplate { public func date(_ datePath: KeyPath, format: String) -> CompiledTemplate { let formatter = DateFormatter() formatter.dateFormat = format - return DateVariable(formatter: formatter, dateReferance: .optional(datePath)) + return DateVariable(formatter: formatter, dateReferance: .optional(datePath), usingDateFormate: true) } /// Render a date in a formate @@ -139,6 +145,6 @@ extension ContextualTemplate { /// - formatter: The DateFormatter to use when rendering the string /// - Returns: A `CompiledTemplate` public func date(_ datePath: KeyPath, formatter: DateFormatter) -> CompiledTemplate { - return DateVariable(formatter: formatter, dateReferance: .optional(datePath)) + return DateVariable(formatter: formatter, dateReferance: .optional(datePath), usingDateFormate: true) } } diff --git a/Sources/HTMLKit/LocalizedTemplate.swift b/Sources/HTMLKit/LocalizedTemplate.swift index fcbcbf93..bf465206 100644 --- a/Sources/HTMLKit/LocalizedTemplate.swift +++ b/Sources/HTMLKit/LocalizedTemplate.swift @@ -9,14 +9,14 @@ 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? { get } } -extension LocalizedTemplate where StringKeys.RawValue == String { +extension LocalizedTemplate where LocalizationKeys.RawValue == String { /// Localize a key to a locale /// @@ -24,7 +24,7 @@ extension LocalizedTemplate where StringKeys.RawValue == String { /// - 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) } @@ -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(_ key: StringKeys, with contentPath: KeyPath) -> CompiledTemplate { + public func localize(_ key: LocalizationKeys, with contentPath: KeyPath) -> CompiledTemplate { return localize(key: key.rawValue, with: contentPath) } @@ -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) } @@ -84,7 +84,7 @@ 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 /// @@ -92,7 +92,7 @@ extension LocalizedTemplate where StringKeys.RawValue == String, Context: Encoda /// - 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) } diff --git a/Tests/HTMLKitTests/HTMLTestDocuments.swift b/Tests/HTMLKitTests/HTMLTestDocuments.swift index 0496c179..266fec26 100644 --- a/Tests/HTMLKitTests/HTMLTestDocuments.swift +++ b/Tests/HTMLKitTests/HTMLTestDocuments.swift @@ -358,7 +358,7 @@ struct LocalizedView: LocalizedTemplate { static let localePath: KeyPath? = \.locale - enum StringKeys: String { + enum LocalizationKeys: String { case helloWorld = "hello.world" case unreadMessages = "unread.messages" } @@ -414,7 +414,7 @@ struct DateView: ContextualTemplate { struct LocalizedDateView: LocalizedTemplate { - enum StringKeys: String { + enum LocalizationKeys: String { case none }