Skip to content

Commit 27e8d59

Browse files
committed
Merge pull request #188 from mac-cain13/feature/strings
Generate R.string
2 parents ea783c8 + fed6609 commit 27e8d59

26 files changed

+860
-7
lines changed

Documentation/Examples.md

+19
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ label.textColor = R.color.appColors.textColor()
7171

7272
There are some points to keep in mind when using Color palettes, see [About Colors](Colors.md)
7373

74+
## Localized strings
75+
76+
*Vanilla*
77+
```swift
78+
let welcomeMessage = NSLocalizedString("welcome.message", comment: "")
79+
let settingsTitle = NSLocalizedString("title", tableName: "Settings", comment: "")
80+
81+
let welcomeName = String(format: NSLocalizedString("welcome.withName", comment: ""), locale: NSLocale.currentLocale(), "Alice")
82+
```
83+
84+
*With R.swift*
85+
```swift
86+
// Localized strings are grouped per table (.strings file)
87+
let welcomeMessage = R.string.localizable.welcomeMessage()
88+
let settingsTitle = R.string.settings.title()
89+
90+
let welcomeName = R.string.localizable.welcomeWithName("Alice")
91+
```
92+
7493
## Storyboards
7594

7695
*Vanilla*

License

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2020
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
THE SOFTWARE.
2222

23+
---
24+
BE AWARE: This software includes Xcode.swift from Tom Lokhorst as a whole, the copyright notice and this permission notice of the MIT license from Xcode.swift should also be included. See: https://github.com/tomlokhorst/Xcode.swift/blob/develop/LICENSE

R.swift.xcodeproj/project.pbxproj

+18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5D45C2191CA12913000B8DC9 /* StringsGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D997C941C7C291900B2F376 /* StringsGenerator.swift */; };
11+
5D45C21B1CA1294A000B8DC9 /* LocalizableStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D997C961C7C2BEE00B2F376 /* LocalizableStrings.swift */; };
12+
5D997C951C7C291900B2F376 /* StringsGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D997C941C7C291900B2F376 /* StringsGenerator.swift */; };
13+
5D997C971C7C2BEE00B2F376 /* LocalizableStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D997C961C7C2BEE00B2F376 /* LocalizableStrings.swift */; };
1014
D5646DE41BE2016E0034F4D7 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE11BE2016E0034F4D7 /* Extensions.swift */; };
1115
D5646DE51BE2016E0034F4D7 /* PBXObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE21BE2016E0034F4D7 /* PBXObject.swift */; };
1216
D5646DE61BE2016E0034F4D7 /* Serialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5646DE31BE2016E0034F4D7 /* Serialization.swift */; };
@@ -92,13 +96,17 @@
9296
D5F97E4C1C1819160066D7C0 /* Image.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F97E411C1816360066D7C0 /* Image.swift */; };
9397
D5F97E4D1C1819160066D7C0 /* ResourceFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F97E431C1816790066D7C0 /* ResourceFile.swift */; };
9498
D5F97E4E1C1819160066D7C0 /* Nib.swift in Sources */ = {isa = PBXBuildFile; fileRef = D5F97E451C18169E0066D7C0 /* Nib.swift */; };
99+
E2156B8E1CC5254A00F341DC /* FormatSpecifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2156B8D1CC5254A00F341DC /* FormatSpecifier.swift */; };
100+
E2156B8F1CC5255000F341DC /* FormatSpecifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2156B8D1CC5254A00F341DC /* FormatSpecifier.swift */; };
95101
E22D43631C9582CA00692FFF /* ColorGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22D43621C9582CA00692FFF /* ColorGenerator.swift */; };
96102
E22D43651C95845200692FFF /* ColorPalette.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22D43641C95845200692FFF /* ColorPalette.swift */; };
97103
E24720CB1C96B6A600DF291D /* ColorPalette.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22D43641C95845200692FFF /* ColorPalette.swift */; };
98104
E24720CC1C96B6AB00DF291D /* ColorGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22D43621C9582CA00692FFF /* ColorGenerator.swift */; };
99105
/* End PBXBuildFile section */
100106

101107
/* Begin PBXFileReference section */
108+
5D997C941C7C291900B2F376 /* StringsGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringsGenerator.swift; sourceTree = "<group>"; };
109+
5D997C961C7C2BEE00B2F376 /* LocalizableStrings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalizableStrings.swift; sourceTree = "<group>"; };
102110
D5646DE11BE2016E0034F4D7 /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
103111
D5646DE21BE2016E0034F4D7 /* PBXObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PBXObject.swift; sourceTree = "<group>"; };
104112
D5646DE31BE2016E0034F4D7 /* Serialization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Serialization.swift; sourceTree = "<group>"; };
@@ -145,6 +153,7 @@
145153
D5F97E411C1816360066D7C0 /* Image.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Image.swift; sourceTree = "<group>"; };
146154
D5F97E431C1816790066D7C0 /* ResourceFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ResourceFile.swift; sourceTree = "<group>"; };
147155
D5F97E451C18169E0066D7C0 /* Nib.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Nib.swift; sourceTree = "<group>"; };
156+
E2156B8D1CC5254A00F341DC /* FormatSpecifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FormatSpecifier.swift; sourceTree = "<group>"; };
148157
E22D43621C9582CA00692FFF /* ColorGenerator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorGenerator.swift; sourceTree = "<group>"; };
149158
E22D43641C95845200692FFF /* ColorPalette.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPalette.swift; sourceTree = "<group>"; };
150159
/* End PBXFileReference section */
@@ -193,6 +202,7 @@
193202
D5B7996B1C1993F8009EA901 /* ReuseIdentifierGenerator.swift */,
194203
D5B799651C19939D009EA901 /* SegueGenerator.swift */,
195204
D5B799671C1993B7009EA901 /* StoryboardGenerator.swift */,
205+
5D997C941C7C291900B2F376 /* StringsGenerator.swift */,
196206
);
197207
path = Generators;
198208
sourceTree = "<group>";
@@ -269,7 +279,9 @@
269279
D5F97E3F1C1815E70066D7C0 /* AssetFolder.swift */,
270280
E22D43641C95845200692FFF /* ColorPalette.swift */,
271281
D5F97E391C1812AE0066D7C0 /* Font.swift */,
282+
E2156B8D1CC5254A00F341DC /* FormatSpecifier.swift */,
272283
D5F97E411C1816360066D7C0 /* Image.swift */,
284+
5D997C961C7C2BEE00B2F376 /* LocalizableStrings.swift */,
273285
D5F97E451C18169E0066D7C0 /* Nib.swift */,
274286
D5F97E431C1816790066D7C0 /* ResourceFile.swift */,
275287
D5F97E371C178A4F0066D7C0 /* Resources.swift */,
@@ -410,6 +422,7 @@
410422
D5B799811C1B0943009EA901 /* ErrorOutput.swift in Sources */,
411423
D5F12D431BDACB87009A2C88 /* ResourceGenerator.swift in Sources */,
412424
D5F97E4A1C1819160066D7C0 /* Storyboard.swift in Sources */,
425+
5D45C2191CA12913000B8DC9 /* StringsGenerator.swift in Sources */,
413426
D5B129B21C3BA75A00A1C5FC /* Property.swift in Sources */,
414427
D59F72401C19651F0089767C /* ReusableContainer.swift in Sources */,
415428
D5B799741C199755009EA901 /* ResourceFileGenerator.swift in Sources */,
@@ -422,6 +435,7 @@
422435
D5F97E4B1C1819160066D7C0 /* AssetFolder.swift in Sources */,
423436
D5F12D421BDACB87009A2C88 /* input.swift in Sources */,
424437
D5F795C11BB9983900844EA2 /* MainTests.swift in Sources */,
438+
5D45C21B1CA1294A000B8DC9 /* LocalizableStrings.swift in Sources */,
425439
D59F723D1C1964B20089767C /* Typealias.swift in Sources */,
426440
D5F12D481BDACB8E009A2C88 /* XCProjectFile.swift in Sources */,
427441
D5B7998A1C1B91A9009EA901 /* Module.swift in Sources */,
@@ -437,6 +451,7 @@
437451
D5B799721C199755009EA901 /* ImageGenerator.swift in Sources */,
438452
D5B7997A1C19C1BD009EA901 /* WhiteListedExtensionsResourceType.swift in Sources */,
439453
D59F722A1C1963EA0089767C /* Struct.swift in Sources */,
454+
E2156B8F1CC5255000F341DC /* FormatSpecifier.swift in Sources */,
440455
D5A0A82E1C4793C20089ED2C /* TypePrinter.swift in Sources */,
441456
D5A0A82D1C4793C20089ED2C /* SwiftCodeConverible.swift in Sources */,
442457
D5B799731C199755009EA901 /* NibGenerator.swift in Sources */,
@@ -456,7 +471,9 @@
456471
isa = PBXSourcesBuildPhase;
457472
buildActionMask = 2147483647;
458473
files = (
474+
5D997C951C7C291900B2F376 /* StringsGenerator.swift in Sources */,
459475
D5B799831C1B8C78009EA901 /* Module.swift in Sources */,
476+
E2156B8E1CC5254A00F341DC /* FormatSpecifier.swift in Sources */,
460477
D5C5A8EF1BB7196000163E71 /* Core.swift in Sources */,
461478
D58672491C21FC9700A760EC /* TypeSequenceProvider.swift in Sources */,
462479
D5F97E461C18169E0066D7C0 /* Nib.swift in Sources */,
@@ -491,6 +508,7 @@
491508
D5B7996A1C1993E3009EA901 /* NibGenerator.swift in Sources */,
492509
E22D43631C9582CA00692FFF /* ColorGenerator.swift in Sources */,
493510
D5646DE51BE2016E0034F4D7 /* PBXObject.swift in Sources */,
511+
5D997C971C7C2BEE00B2F376 /* LocalizableStrings.swift in Sources */,
494512
D56DC76D1C41758800623437 /* AccessModifier.swift in Sources */,
495513
D5B129AF1C3BA5F900A1C5FC /* Let.swift in Sources */,
496514
D5B7997D1C1B07C3009EA901 /* SanitizedSwiftName.swift in Sources */,

R.swift/Generators/ColorGenerator.swift

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ struct ColorGenerator: Generator {
1515

1616
init(colorPalettes palettes: [ColorPalette]) {
1717
externalStruct = Struct(
18+
comments: ["This `R.color` struct is generated, and contains static references to \(palettes.count) color palettes."],
1819
type: Type(module: .Host, name: "color"),
1920
implements: [],
2021
typealiasses: [],
@@ -27,6 +28,7 @@ struct ColorGenerator: Generator {
2728
private static func colorStructFromPalette(palette: ColorPalette) -> Struct? {
2829
if palette.colors.isEmpty { return nil }
2930

31+
let name = sanitizedSwiftName(palette.filename)
3032
let groupedColors = palette.colors.groupBySwiftNames { $0.0 }
3133

3234
for (sanitizedName, duplicates) in groupedColors.duplicates {
@@ -42,7 +44,8 @@ struct ColorGenerator: Generator {
4244
}
4345

4446
return Struct(
45-
type: Type(module: .Host, name: sanitizedSwiftName(palette.filename)),
47+
comments: ["This `R.color.\(name)` struct is generated, and contains static references to \(groupedColors.uniques.count) colors."],
48+
type: Type(module: .Host, name: name),
4649
implements: [],
4750
typealiasses: [],
4851
properties: groupedColors.uniques.map(ColorGenerator.colorLet),

R.swift/Generators/ResourceGenerator.swift

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func generateResourceStructsWithResources(resources: Resources, bundleIdentifier
4242
NibGenerator(nibs: resources.nibs),
4343
ReuseIdentifierGenerator(reusables: resources.reusables),
4444
ResourceFileGenerator(resourceFiles: resources.resourceFiles),
45+
StringsGenerator(localizableStrings: resources.localizableStrings),
4546
]
4647

4748
var generatorResults = GeneratorResults()

0 commit comments

Comments
 (0)