Skip to content

Commit 59da9d1

Browse files
authored
Move AppLocalizedString to WordPressShared (#24223)
* Move AppLocalizedString to WordPressShared * Make Bundle.app private * Remove unneeded Foundation imports
1 parent 778e087 commit 59da9d1

File tree

22 files changed

+60
-64
lines changed

22 files changed

+60
-64
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,4 @@
1-
import SwiftUI
2-
3-
extension Bundle {
4-
/// Returns the `Bundle` for the host `.app`.
5-
///
6-
/// - If this is called from code already located in the main app's bundle or from a Pod/Framework,
7-
/// this will return the same as `Bundle.main`, aka the bundle of the app itself.
8-
/// - If this is called from an App Extension (Widget, ShareExtension, etc), this will return the bundle of the
9-
/// main app hosting said App Extension (while `Bundle.main` would return the App Extension itself)
10-
///
11-
/// This is particularly useful to reference a resource or string bundled inside the app from an App Extension / Widget.
12-
///
13-
/// - Note:
14-
/// In the context of Unit Tests this will return the Test Harness (aka Test Host) app, since that is the app running said tests.
15-
///
16-
static let app: Bundle = {
17-
var url = Bundle.main.bundleURL
18-
while url.pathExtension != "app" && url.lastPathComponent != "/" {
19-
url.deleteLastPathComponent()
20-
}
21-
guard let appBundle = Bundle(url: url) else { fatalError("Unable to find the parent app bundle") }
22-
return appBundle
23-
}()
24-
}
25-
26-
/// Use this to express *intent* on your API that the string you are manipulating / returning is intended to already be localized
27-
/// and its value to have been provided via a call to `NSLocalizedString` or `AppLocalizedString`.
28-
///
29-
/// Semantically speaking, a method taking or returning a `LocalizedString` is signaling that you can display said UI string
30-
/// to the end user, without the need to be treated as a key to be localized. The string is expected to already have been localized
31-
/// at that point of the code, via a call to `NSLocalizedString`, `AppLocalizedString` or similar upstream in the code.
32-
///
33-
/// - Note: Remember though that, as a `typealias`, this won't provide any compile-time guarantee.
34-
typealias LocalizedString = String
1+
import Foundation
352

363
/// Use this function instead of `NSLocalizedString` to reference localized strings **from the app bundle** – especially
374
/// when using localized strings from the code of an app extension.
@@ -63,6 +30,29 @@ typealias LocalizedString = String
6330
/// - Returns: A localized version of the string designated by `key` in the table identified by `tableName`.
6431
/// If the localized string for `key` cannot be found within the table, `value` is returned.
6532
/// (However, `key` is returned instead when `value` is `nil` or the empty string).
66-
func AppLocalizedString(_ key: String, tableName: String? = nil, value: String? = nil, comment: String) -> LocalizedString {
33+
public func AppLocalizedString(_ key: String, tableName: String? = nil, value: String? = nil, comment: String) -> String {
6734
Bundle.app.localizedString(forKey: key, value: value, table: nil)
6835
}
36+
37+
private extension Bundle {
38+
/// Returns the `Bundle` for the host `.app`.
39+
///
40+
/// - If this is called from code already located in the main app's bundle or from a Pod/Framework,
41+
/// this will return the same as `Bundle.main`, aka the bundle of the app itself.
42+
/// - If this is called from an App Extension (Widget, ShareExtension, etc), this will return the bundle of the
43+
/// main app hosting said App Extension (while `Bundle.main` would return the App Extension itself)
44+
///
45+
/// This is particularly useful to reference a resource or string bundled inside the app from an App Extension / Widget.
46+
///
47+
/// - Note:
48+
/// In the context of Unit Tests this will return the Test Harness (aka Test Host) app, since that is the app running said tests.
49+
///
50+
static let app: Bundle = {
51+
var url = Bundle.main.bundleURL
52+
while url.pathExtension != "app" && url.lastPathComponent != "/" {
53+
url.deleteLastPathComponent()
54+
}
55+
guard let appBundle = Bundle(url: url) else { fatalError("Unable to find the parent app bundle") }
56+
return appBundle
57+
}()
58+
}

WordPress/Classes/Utility/FormattableContent/Actions/FormattableContentAction.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import WordPressShared
12

23
protocol FormattableContentActionParser {
34
func parse(_ dictionary: [String: AnyObject]?) -> [FormattableContentAction]

WordPress/Classes/ViewRelated/Aztec/Extensions/FormatBarItemProviders.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Foundation
22
import Gridicons
33
import Aztec
44
import UIKit
5+
import WordPressShared
56

67
protocol FormatBarItemProvider {
78
var iconImage: UIImage { get }

WordPress/Classes/ViewRelated/Aztec/Extensions/TextList+WordPress.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import Aztec
33
import UIKit
4+
import WordPressShared
45

56
// MARK: - TextList.Style
67
//

WordPress/Classes/ViewRelated/Stats/Extensions/Double+Stats.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import WordPressShared
23

34
fileprivate struct Unit {
45
let abbreviationFormat: String

WordPress/Classes/ViewRelated/Views/List/NotificationsList/NotificationsTableViewCellContent.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ extension NotificationsTableViewCellContent {
204204

205205
let action: () -> Void
206206

207-
let accessibilityLabel: LocalizedString
208-
let accessibilityHint: LocalizedString
207+
let accessibilityLabel: String
208+
let accessibilityHint: String
209209

210-
init(icon: SwiftUI.Image, color: Color? = nil, accessibilityLabel: LocalizedString, accessibilityHint: LocalizedString, action: @escaping () -> Void) {
210+
init(icon: SwiftUI.Image, color: Color? = nil, accessibilityLabel: String, accessibilityHint: String, action: @escaping () -> Void) {
211211
self.icon = icon
212212
self.color = color
213213
self.accessibilityLabel = accessibilityLabel

WordPress/JetpackStatsWidgets/Model/GroupedViewData.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import SwiftUI
1+
import Foundation
22

33
struct GroupedViewData {
44

5-
let widgetTitle: LocalizedString
5+
let widgetTitle: String
66
let siteName: String
7-
let upperLeftTitle: LocalizedString
7+
let upperLeftTitle: String
88
let upperLeftValue: Int
9-
let upperRightTitle: LocalizedString
9+
let upperRightTitle: String
1010
let upperRightValue: Int
11-
let lowerLeftTitle: LocalizedString
11+
let lowerLeftTitle: String
1212
let lowerLeftValue: Int
13-
let lowerRightTitle: LocalizedString
13+
let lowerRightTitle: String
1414
let lowerRightValue: Int
1515

1616
let statsURL: URL?

WordPress/JetpackStatsWidgets/Model/ListViewData.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import JetpackStatsWidgetsCore
33

44
struct ListViewData {
55

6-
let widgetTitle: LocalizedString
6+
let widgetTitle: String
77
let siteName: String
88
let items: [ThisWeekWidgetDay]
99

WordPress/JetpackStatsWidgets/Views/Cards/FlexibleCard.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import SwiftUI
33
/// A card with a title and a numeric or string value that can be either vertically or horizontally stacked
44
struct FlexibleCard: View {
55
let axis: Axis
6-
let title: LocalizedString
6+
let title: String
77
let value: Value
88
let lineLimit: Int
99

10-
init(axis: Axis, title: LocalizedString, value: Value, lineLimit: Int = 1) {
10+
init(axis: Axis, title: String, value: Value, lineLimit: Int = 1) {
1111
self.axis = axis
1212
self.title = title
1313
self.value = value

WordPress/JetpackStatsWidgets/Views/Cards/ListRow.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
import SwiftUI
33
import WidgetKit
4+
import WordPressShared
45

56
struct ListRow: View {
67
@Environment(\.widgetFamily) var family: WidgetFamily
@@ -48,7 +49,7 @@ struct ListRow: View {
4849
return percentValue < 0 ? Constants.negativeColor : Constants.positiveColor
4950
}
5051

51-
private var differenceLabelText: LocalizedString {
52+
private var differenceLabelText: String {
5253
guard !isToday else {
5354
return LocalizableStrings.todayWidgetTitle
5455
}

0 commit comments

Comments
 (0)