Skip to content

Commit d7a89d0

Browse files
authoredNov 28, 2022
Merge pull request #768 from mac-cain13/update
Always use String(format:), also for strings without arguments
2 parents b88d620 + 13c48b3 commit d7a89d0

File tree

7 files changed

+22
-27
lines changed

7 files changed

+22
-27
lines changed
 

‎Documentation/Migration.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Pointers for migration between major versions.
66

77
[Demo video: Updating from R.swift 6 to Rswift 7](https://www.youtube.com/watch?v=icihJ_hin3I)
88

9+
#### R.string changes
10+
- Strings are always formatted using `String(format:)`, even strings without arguments, make sure to escape any percentage signs (`%`) with `%%`
11+
- Use a custom language: `R.string(preferredLanguages: ["fr"]).example.hello()`
12+
913
#### Running the executable
1014

1115
If you're using Swift Package Manager:

‎Examples/ResourceApp/ResourceApp/Strings/Generic.strings

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@
1212
"precision2" = "two - %12.2f";
1313
"precision3" = "three - %12.4f";
1414
"precision4" = "four - %.2f";
15+
"discount10" = "Today, 10%% off!";
16+
"discountX" = "Today, %d%% off!";
17+
"url" = "http%%3A%%2F%%2Fwww.abc.xyz";

‎Examples/ResourceApp/ResourceApp/Strings/en.lproj/Localizable.strings

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ one = One;
1111
two = 2;
1212

1313
"quote" = "There are %d lights!";
14+
"discount10" = "Today, 10%% off!";
15+
"discountX" = "Today, %d%% off!";

‎Examples/ResourceApp/ResourceApp/Strings/es.lproj/Localizable.strings

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
one = Uno;
1111
two = 2;
1212

13-
"quote" = "Hay %d luces!";
13+
"quote" = "Hay %d luces!";
14+
"discount10" = "Today, 10%% off!";
15+
"discountX" = "Today, %d%% off!";

‎Examples/ResourceApp/ResourceApp/Strings/ja.lproj/Localizable.strings

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ one = "一つ";
1111
two = 2;
1212

1313
"quote" = "%dつの光があります!";
14+
"discount10" = "Today, 10%% off!";
15+
"discountX" = "Today, %d%% off!";
1416
"japanese only" = "Not translated in other languages, and there is no Base";

‎Examples/ResourceApp/ResourceAppTests/StringsTests.swift

+6
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class StringsTests: XCTestCase {
5050
XCTAssertEqual(stringsFR.generic.precision3(12345.678), "three - 12\u{202F}345,6780")
5151
XCTAssertEqual(stringsFR.generic.precision4(12345.678), "four - 12\u{202F}345,68")
5252

53+
XCTAssertEqual(R.string.generic.discount10(), "Today, 10% off!")
54+
XCTAssertEqual(R.string.generic.discountX(20), "Today, 20% off!")
55+
XCTAssertEqual(R.string.localizable.discount10(), "Today, 10% off!")
56+
XCTAssertEqual(R.string.localizable.discountX(20), "Today, 20% off!")
57+
XCTAssertEqual(R.string.generic.url(), "http%3A%2F%2Fwww.abc.xyz")
58+
5359
XCTAssertEqual(
5460
R.string.settings.multilineKeyWeird(),
5561
NSLocalizedString("Multiline\t\\key/\n\"weird\"?!", tableName: "Settings", comment: ""))

‎Sources/RswiftResources/Integrations/StringResource+Integrations.swift

+2-26
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,6 @@ import Foundation
99
import SwiftUI
1010

1111
extension String {
12-
init(key: StaticString, tableName: String, source: StringResource.Source, developmentValue: String?) {
13-
switch source {
14-
case let .hosting(bundle):
15-
// With fallback to developmentValue
16-
self = NSLocalizedString(key.description, tableName: tableName, bundle: bundle, value: developmentValue ?? "", comment: "")
17-
18-
case let .selected(bundle, _):
19-
// Don't use developmentValue with selected bundle/locale
20-
self = NSLocalizedString(key.description, tableName: tableName, bundle: bundle, value: "", comment: "")
21-
22-
case .none:
23-
self = key.description
24-
}
25-
}
26-
27-
init(key: StaticString, tableName: String, source: StringResource.Source, developmentValue: String?, preferredLanguages: [String]) {
28-
guard let (bundle, locale) = source.bundle?.firstBundleAndLocale(tableName: tableName, preferredLanguages: preferredLanguages) else {
29-
self = key.description
30-
return
31-
}
32-
33-
self.init(key: key, tableName: tableName, source: .selected(bundle, locale), developmentValue: developmentValue)
34-
}
35-
3612
init(key: StaticString, tableName: String, source: StringResource.Source, developmentValue: String?, locale overrideLocale: Locale?, arguments: [CVarArg]) {
3713
switch source {
3814
case let .hosting(bundle):
@@ -62,11 +38,11 @@ extension String {
6238

6339
extension String {
6440
public init(resource: StringResource) {
65-
self.init(key: resource.key, tableName: resource.tableName, source: resource.source, developmentValue: resource.developmentValue)
41+
self.init(key: resource.key, tableName: resource.tableName, source: resource.source, developmentValue: resource.developmentValue, locale: nil, arguments: [])
6642
}
6743

6844
public init(resource: StringResource, preferredLanguages: [String], locale overrideLocale: Locale? = nil) {
69-
self.init(key: resource.key, tableName: resource.tableName, source: resource.source, developmentValue: resource.developmentValue, preferredLanguages: preferredLanguages)
45+
self.init(key: resource.key, tableName: resource.tableName, source: resource.source, developmentValue: resource.developmentValue, preferredLanguages: preferredLanguages, locale: nil, arguments: [])
7046
}
7147

7248
public init<Arg1: CVarArg>(format resource: StringResource1<Arg1>, locale overrideLocale: Locale? = nil, _ arg1: Arg1) {

0 commit comments

Comments
 (0)
Please sign in to comment.