Skip to content

Commit 360490a

Browse files
authored
Make attributes localizable (#174)
* Make the placeholder attribute localizable * Make the alternate attribute localizable * Make the value attribute localizable * Make the title attribute localizable * Make the content attribute localizable * Disfavor the designated functions * Add support for verbatim content
1 parent 7d1eef0 commit 360490a

17 files changed

+1457
-17
lines changed

Sources/HTMLKit/Abstraction/Attributes/BasicAttributes.swift

Lines changed: 218 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,65 @@ extension ActionAttribute where Self: EmptyNode {
9797
@_documentation(visibility: internal)
9898
public protocol AlternateAttribute: Attribute {
9999

100-
/// The function represents the html-attribute 'alt'.
100+
/// Provide an alternative information
101101
///
102-
/// ```html
103-
/// <tag alt="" />
102+
/// ```swift
103+
/// Image()
104+
/// .alternate("Lorem ipsum...")
104105
/// ```
106+
///
107+
/// - Parameter value: The text to describe the image
108+
///
109+
/// - Returns: The element
105110
func alternate(_ value: String) -> Self
111+
112+
/// Provide an alternative information
113+
///
114+
/// ```swift
115+
/// Image()
116+
/// .alternate("Lorem ipsum...")
117+
/// ```
118+
///
119+
/// - Parameter localizedKey: The string key to be translated
120+
/// - Parameter tableName: The translation table to look in
121+
///
122+
/// - Returns: The element
123+
func alternate(_ localizedKey: LocalizedStringKey, tableName: String?) -> Self
124+
125+
/// Provide an alternative information without localization.
126+
///
127+
/// ```swift
128+
/// Image()
129+
/// .alternate(verbatim: "Lorem ipsum...")
130+
/// ```
131+
///
132+
/// - Parameter value: The text to describe the image
133+
///
134+
/// - Returns: The element
135+
func alternate(verbatim value: String) -> Self
136+
106137
}
107138

108139
extension AlternateAttribute where Self: ContentNode {
109140

110141
internal func mutate(alternate value: String) -> Self {
111142
return self.mutate(key: "alt", value: value)
112143
}
144+
145+
internal func mutate(alternate value: LocalizedString) -> Self {
146+
return self.mutate(key: "alt", value: value)
147+
}
113148
}
114149

115150
extension AlternateAttribute where Self: EmptyNode {
116151

117152
internal func mutate(alternate value: String) -> Self {
118153
return self.mutate(key: "alt", value: value)
119154
}
155+
156+
internal func mutate(alternate value: LocalizedString) -> Self {
157+
return self.mutate(key: "alt", value: value)
158+
}
120159
}
121160

122161
/// The protocol provides the element with the asynchronously handler.
@@ -430,26 +469,67 @@ extension ColumnSpanAttribute where Self: EmptyNode {
430469
@_documentation(visibility: internal)
431470
public protocol ContentAttribute: Attribute {
432471

433-
/// The function represents the html-attribute 'content'.
472+
/// Supply a value to the associated name.
434473
///
435-
/// ```html
436-
/// <tag content="" />
474+
/// ```swift
475+
/// Meta()
476+
/// .name(.description)
477+
/// .content("Lorem ipsum...")
437478
/// ```
479+
///
480+
/// - Parameter value: The value to describe the name
481+
///
482+
/// - Returns: The element
438483
func content(_ value: String) -> Self
484+
485+
/// Supply a value to the associated name.
486+
///
487+
/// ```swift
488+
/// Meta()
489+
/// .name(.description)
490+
/// .content("Lorem ipsum...")
491+
/// ```
492+
///
493+
/// - Parameter localizedKey: The string key to be translated
494+
/// - Parameter tableName: The translation table to look in
495+
///
496+
/// - Returns: The element
497+
func content(_ localizedKey: LocalizedStringKey, tableName: String?) -> Self
498+
499+
/// Supply a value to the associated name without localization
500+
///
501+
/// ```swift
502+
/// Meta()
503+
/// .name(.description)
504+
/// .content(verbatim: "Lorem ipsum...")
505+
/// ```
506+
///
507+
/// - Parameter value: The value to describe the name
508+
///
509+
/// - Returns: The element
510+
func content(verbatim value: String) -> Self
439511
}
440512

441513
extension ContentAttribute where Self: ContentNode {
442514

443515
internal func mutate(content value: String) -> Self {
444516
return self.mutate(key: "content", value: value)
445517
}
518+
519+
internal func mutate(content value: LocalizedString) -> Self {
520+
return self.mutate(key: "content", value: value)
521+
}
446522
}
447523

448524
extension ContentAttribute where Self: EmptyNode {
449525

450526
internal func mutate(content value: String) -> Self {
451527
return self.mutate(key: "content", value: value)
452528
}
529+
530+
internal func mutate(content value: LocalizedString) -> Self {
531+
return self.mutate(key: "content", value: value)
532+
}
453533
}
454534

455535
/// The protocol provides the element with the iseditable handler.
@@ -1913,26 +1993,64 @@ extension PingAttribute where Self: EmptyNode {
19131993
@_documentation(visibility: internal)
19141994
public protocol PlaceholderAttribute: Attribute {
19151995

1916-
/// The function represents the html-attribute 'placeholder'.
1996+
/// Supply a short hint.
19171997
///
1918-
/// ```html
1919-
/// <tag placeholder="" />
1998+
/// ```swift
1999+
/// Input()
2000+
/// .placeholder("Lorem ipsum...")
19202001
/// ```
2002+
///
2003+
/// - Parameter value: The text to display as a hint
2004+
///
2005+
/// - Returns: The element
19212006
func placeholder(_ value: String) -> Self
2007+
2008+
/// Supply a short hint.
2009+
///
2010+
/// ```swift
2011+
/// Input()
2012+
/// .placeholder("Lorem ipsum...")
2013+
/// ```
2014+
///
2015+
/// - Parameter localizedKey: The string key to be translated
2016+
/// - Parameter tableName: The translation table to look in
2017+
///
2018+
/// - Returns: The element
2019+
func placeholder(_ localizedKey: LocalizedStringKey, tableName: String?) -> Self
2020+
2021+
/// Supply a short hint without localization.
2022+
///
2023+
/// ```swift
2024+
/// Input()
2025+
/// .placeholder(verbatim: "Lorem ipsum...")
2026+
/// ```
2027+
///
2028+
/// - Parameter value: The text to display as a hint
2029+
///
2030+
/// - Returns: The element
2031+
func placeholder(verbatim value: String) -> Self
19222032
}
19232033

19242034
extension PlaceholderAttribute where Self: ContentNode {
19252035

19262036
internal func mutate(placeholder value: String) -> Self {
19272037
return self.mutate(key: "placeholder", value: value)
19282038
}
2039+
2040+
internal func mutate(placeholder value: LocalizedString) -> Self {
2041+
return self.mutate(key: "placeholder", value: value)
2042+
}
19292043
}
19302044

19312045
extension PlaceholderAttribute where Self: EmptyNode {
19322046

19332047
internal func mutate(placeholder value: String) -> Self {
19342048
return self.mutate(key: "placeholder", value: value)
19352049
}
2050+
2051+
internal func mutate(placeholder value: LocalizedString) -> Self {
2052+
return self.mutate(key: "placeholder", value: value)
2053+
}
19362054
}
19372055

19382056
/// The protocol provides the element with the poster handler.
@@ -2645,26 +2763,70 @@ extension TargetAttribute where Self: EmptyNode {
26452763
@_documentation(visibility: internal)
26462764
public protocol TitleAttribute: Attribute {
26472765

2648-
/// The function represents the html-attribute 'title'.
2766+
/// Supply extra information about the element.
26492767
///
2650-
/// ```html
2651-
/// <tag title="" />
2768+
/// ```swift
2769+
/// Paragraph {
2770+
/// "Lorem ipsum..."
2771+
/// }
2772+
/// .title("Lorem ipsum")
26522773
/// ```
2774+
///
2775+
/// - Parameter value: The extra information to display
2776+
///
2777+
/// - Returns: The element
26532778
func title(_ value: String) -> Self
2779+
2780+
/// Supply extra information about the element..
2781+
///
2782+
/// ```swift
2783+
/// Paragraph {
2784+
/// "Lorem ipsum..."
2785+
/// }
2786+
/// .title("Lorem ipsum")
2787+
/// ```
2788+
///
2789+
/// - Parameter localizedKey: The string key to be translated
2790+
/// - Parameter tableName: The translation table to look in
2791+
///
2792+
/// - Returns: The element
2793+
func title(_ localizedKey: LocalizedStringKey, tableName: String?) -> Self
2794+
2795+
/// Supply extra information about the element without localization.
2796+
///
2797+
/// ```swift
2798+
/// Paragraph {
2799+
/// "Lorem ipsum..."
2800+
/// }
2801+
/// .title(verbatim: "Lorem ipsum")
2802+
/// ```
2803+
///
2804+
/// - Parameter value: The extra information to display
2805+
///
2806+
/// - Returns: The element
2807+
func title(verbatim value: String) -> Self
26542808
}
26552809

26562810
extension TitleAttribute where Self: ContentNode {
26572811

26582812
internal func mutate(title value: String) -> Self {
26592813
return self.mutate(key: "title", value: value)
26602814
}
2815+
2816+
internal func mutate(title value: LocalizedString) -> Self {
2817+
return self.mutate(key: "title", value: value)
2818+
}
26612819
}
26622820

26632821
extension TitleAttribute where Self: EmptyNode {
26642822

26652823
internal func mutate(title value: String) -> Self {
26662824
return self.mutate(key: "title", value: value)
26672825
}
2826+
2827+
internal func mutate(title value: LocalizedString) -> Self {
2828+
return self.mutate(key: "title", value: value)
2829+
}
26682830
}
26692831

26702832
/// The protocol provides the element with the translate handler.
@@ -2760,26 +2922,67 @@ extension UseMapAttribute where Self: EmptyNode {
27602922
@_documentation(visibility: internal)
27612923
public protocol ValueAttribute: Attribute {
27622924

2763-
/// The function represents the html-attribute 'value'.
2925+
/// Set a initial value for the element.
27642926
///
2765-
/// ```html
2766-
/// <tag value="" />
2927+
/// ```swift
2928+
/// Input()
2929+
/// .type(.text)
2930+
/// .value("Lorem ipsum...")
27672931
/// ```
2932+
///
2933+
/// - Parameter value: The initial value
2934+
///
2935+
/// - Returns: The element
27682936
func value(_ value: String) -> Self
2937+
2938+
/// Set a initial value for the element.
2939+
///
2940+
/// ```swift
2941+
/// Input()
2942+
/// .type(.text)
2943+
/// .value("Lorem ipsum...")
2944+
/// ```
2945+
///
2946+
/// - Parameter localizedKey: The string key to be translated
2947+
/// - Parameter tableName: The translation table to look in
2948+
///
2949+
/// - Returns: The element
2950+
func value(_ localizedKey: LocalizedStringKey, tableName: String?) -> Self
2951+
2952+
/// Set a initial value for the element without localization.
2953+
///
2954+
/// ```swift
2955+
/// Input()
2956+
/// .type(.text)
2957+
/// .value(verbatim: "Lorem ipsum...")
2958+
/// ```
2959+
///
2960+
/// - Parameter value: The initial value
2961+
///
2962+
/// - Returns: The element
2963+
func value(verbatim value: String) -> Self
27692964
}
27702965

27712966
extension ValueAttribute where Self: ContentNode {
27722967

27732968
internal func mutate(value: String) -> Self {
27742969
return self.mutate(key: "value", value: value)
27752970
}
2971+
2972+
internal func mutate(value: LocalizedString) -> Self {
2973+
return self.mutate(key: "value", value: value)
2974+
}
27762975
}
27772976

27782977
extension ValueAttribute where Self: EmptyNode {
27792978

27802979
internal func mutate(value: String) -> Self {
27812980
return self.mutate(key: "value", value: value)
27822981
}
2982+
2983+
internal func mutate(value: LocalizedString) -> Self {
2984+
return self.mutate(key: "value", value: value)
2985+
}
27832986
}
27842987

27852988
/// The protocol provides the element with the width handler.

Sources/HTMLKit/Abstraction/Elements/BasicElements.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,19 @@ extension Html: GlobalAttributes, GlobalEventAttributes {
185185
return mutate(tabindex: value)
186186
}
187187

188+
@_disfavoredOverload
188189
public func title(_ value: String) -> Html {
189190
return mutate(title: value)
190191
}
191192

193+
public func title(_ localizedKey: LocalizedStringKey, tableName: String? = nil) -> Html {
194+
return mutate(title: LocalizedString(key: localizedKey, table: tableName))
195+
}
196+
197+
public func title(verbatim value: String) -> Html {
198+
return mutate(title: value)
199+
}
200+
192201
public func translate(_ value: Values.Decision) -> Html {
193202
return mutate(translate: value.rawValue)
194203
}

0 commit comments

Comments
 (0)