Skip to content

Commit 1473fd4

Browse files
authored
Make components localizable (#177)
* Make the fieldlabel component localizable * Make the disclosure component localizable * Make the textfield component localizable * Make the texteditor component localizable * Make the selectfield component localizable * Make the securefield component localizable * Make the searchfield component localizable * Make the textpad component localizable * Make the checkfield component localizable * Make the radioselect component localizable * Make the button component localizable * Make the link component localizable * Undo the last commit and instead make the linkbutton component localizable
1 parent 1a1b6bb commit 1473fd4

8 files changed

Lines changed: 611 additions & 58 deletions

File tree

Sources/HTMLKitComponents/Components/Button.swift

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
import HTMLKit
77
import Foundation
88

9-
/// A component that initiates an action.
9+
/// A view that represents a button.
10+
///
11+
/// Use `Button` to trigger an action when tapped or clicked.
12+
///
13+
/// ```swift
14+
/// Button(role: .button) {
15+
/// "Lorem ipsum"
16+
/// }
17+
/// ```
1018
public struct Button: View, Modifiable, Actionable {
1119

20+
/// The identifier of the button.
1221
internal var id: String?
1322

14-
/// The role of the button
23+
/// The role of the button.
1524
internal var role: HTMLKit.Values.Button
1625

1726
/// The content of the button.
@@ -20,16 +29,33 @@ public struct Button: View, Modifiable, Actionable {
2029
/// The classes of the button.
2130
internal var classes: [String]
2231

32+
/// The events of the button.
2333
internal var events: [String]?
2434

25-
/// Creates a action button.
35+
/// Create a button.
36+
///
37+
/// - Parameters:
38+
/// - role: The role of the button.
39+
/// - content: The button's content.
2640
public init(role: HTMLKit.Values.Button, @ContentBuilder<Content> content: () -> [Content]) {
2741

2842
self.role = role
2943
self.content = content()
3044
self.classes = ["button"]
3145
}
3246

47+
/// Create a button.
48+
///
49+
/// - Parameters:
50+
/// - localizedStringKey: The key of the localized string used as a label.
51+
/// - role: The role of the button.
52+
public init(_ localizedStringKey: LocalizedStringKey, role: HTMLKit.Values.Button) {
53+
54+
self.role = role
55+
self.content = [LocalizedString(key: localizedStringKey)]
56+
self.classes = ["button"]
57+
}
58+
3359
public var body: Content {
3460
HTMLKit.Button {
3561
self.content
@@ -146,11 +172,21 @@ extension Button: ViewModifier {
146172
}
147173
}
148174

149-
/// A component that initiates an action.
175+
/// A view that represents a link button.
176+
///
177+
/// Use `LinkButton`to navigate to a target.
178+
///
179+
/// ```swift
180+
/// LinkButton(destination: "https://..") {
181+
/// "Lorem ipsum"
182+
/// }
183+
/// ```
150184
public struct LinkButton: View, Modifiable, Identifiable {
151185

186+
/// The unique identifier of the button.
152187
internal var id: String?
153188

189+
/// The target behaviour for the destination
154190
internal let target: HTMLKit.Values.Target
155191

156192
/// The url path of the target.
@@ -165,7 +201,12 @@ public struct LinkButton: View, Modifiable, Identifiable {
165201
/// The events of the button.
166202
internal var events: [String]?
167203

168-
/// Creates a action button.
204+
/// Create a link button.
205+
///
206+
/// - Parameters:
207+
/// - destination: The url of the target to navigate to.
208+
/// - target: The behaviour that determines how to open the target.
209+
/// - content: The content displayed as the label.
169210
public init(destination: String, target: HTMLKit.Values.Target = .current, @ContentBuilder<Content> content: () -> [Content]) {
170211

171212
self.destination = destination
@@ -174,7 +215,26 @@ public struct LinkButton: View, Modifiable, Identifiable {
174215
self.classes = ["button"]
175216
}
176217

177-
/// Creates a action button.
218+
/// Create a link button.
219+
///
220+
/// - Parameters:
221+
/// - localizedStringKey: The key of the localized string used as the label.
222+
/// - destination: The url of the target to navigate to.
223+
/// - target: The behaviour that determines how to open the target.
224+
public init(_ localizedStringKey: LocalizedStringKey, destination: String, target: HTMLKit.Values.Target = .current) {
225+
226+
self.destination = destination
227+
self.target = target
228+
self.content = [LocalizedString(key: localizedStringKey)]
229+
self.classes = ["button"]
230+
}
231+
232+
/// Create a link button.
233+
///
234+
/// - Parameters:
235+
/// - destination: The url of the target to navigate to.
236+
/// - target: The behaviour that determines how to open the target.
237+
/// - content: The content displayed as the label.
178238
public init(destination: URL, target: HTMLKit.Values.Target = .current, @ContentBuilder<Content> content: () -> [Content]) {
179239

180240
self.destination = destination.absoluteString
@@ -183,6 +243,20 @@ public struct LinkButton: View, Modifiable, Identifiable {
183243
self.classes = ["button"]
184244
}
185245

246+
/// Create a link button.
247+
///
248+
/// - Parameters:
249+
/// - localizedStringKey: The key of the localized string used as the label.
250+
/// - destination: The url of the target to navigate to.
251+
/// - target: The behaviour that determines how to open the target.
252+
public init(_ localizedStringKey: LocalizedStringKey, destination: URL, target: HTMLKit.Values.Target = .current) {
253+
254+
self.destination = destination.absoluteString
255+
self.target = target
256+
self.content = [LocalizedString(key: localizedStringKey)]
257+
self.classes = ["button"]
258+
}
259+
186260
public var body: Content {
187261
Anchor {
188262
self.content

Sources/HTMLKitComponents/Components/Disclosure.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,53 @@
55

66
import HTMLKit
77

8-
/// A component that distinguish content.
8+
/// A view that represents a disclosure.
9+
///
10+
/// Use `Disclosure` to reveal or hide content when interacted with.
11+
///
12+
/// ```swift
13+
/// Disclosure("Lorem ipsum") {
14+
/// Text {
15+
/// "Lorem ipsum..."
16+
/// }
17+
/// }
18+
/// ```
919
public struct Disclosure: View, Modifiable {
1020

11-
internal let label: String
21+
/// The label of the disclosure.
22+
internal let label: Content
1223

24+
/// The disclosure's content.
1325
internal let content: [Content]
1426

1527
/// The classes of the content.
1628
internal var classes: [String]
17-
18-
/// Creates a card.
29+
30+
/// Create a disclosure.
31+
///
32+
/// - Parameters:
33+
/// - label: The label to describe the content.
34+
/// - content: The disclosure's content.
35+
@_disfavoredOverload
1936
public init(_ label: String, @ContentBuilder<Content> content: () -> [Content]) {
2037

2138
self.label = label
2239
self.content = content()
2340
self.classes = ["disclosure"]
2441
}
2542

43+
/// Create a disclosure.
44+
///
45+
/// - Parameters:
46+
/// - label: The key of the localized string to describe the content.
47+
/// - content: The disclosure's content.
48+
public init(_ label: LocalizedStringKey, @ContentBuilder<Content> content: () -> [Content]) {
49+
50+
self.label = LocalizedString(key: label)
51+
self.content = content()
52+
self.classes = ["disclosure"]
53+
}
54+
2655
public var body: Content {
2756
Division {
2857
Division {

0 commit comments

Comments
 (0)