Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added doc comments for default translation of strings, fixes #58 #60

Merged
merged 1 commit into from
Nov 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# SwiftGen CHANGELOG

---
## 0.7.2

#### Enhancements

* The default translation of strings are now added as doc comments to the enum cases. ([@MrAlek](https://github.com/MrAlek), [#58](https://github.com/AliSoftware/SwiftGen/issues/58))
* You can add translations to your own templates by using the `string.translation` variable.

## 0.7.1

#### Fixes
Expand All @@ -10,7 +17,7 @@

## 0.7.0

#### Enhencements
#### Enhancements

* Allow using **custom templates by name**. ([#42](https://github.com/AliSoftware/SwiftGen/issues/42), [#50](https://github.com/AliSoftware/SwiftGen/pull/50))
* Now the `-t` flag expect a template name (defaults to `default`), and will search a matching template in `Application Support` first, then in the templates bundled with SwiftGen.
Expand Down
9 changes: 6 additions & 3 deletions GenumKit/Parsers/StringsFileParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ public final class StringsFileParser {

public struct Entry {
let key: String
let translation: String
let types: [PlaceholderType]

public init(key: String, types: [PlaceholderType]) {
public init(key: String, translation: String, types: [PlaceholderType]) {
self.key = key
self.translation = translation
self.types = types
}

public init(key: String, types: PlaceholderType...) {
public init(key: String, translation: String, types: PlaceholderType...) {
self.key = key
self.translation = translation
self.types = types
}

Expand All @@ -86,7 +89,7 @@ public final class StringsFileParser {
let translation = (line as NSString).substringWithRange(match.rangeAtIndex(2))
let types = PlaceholderType.fromFormatString(translation)

self = Entry(key: key, types: types)
self = Entry(key: key, translation: translation, types: types)
} else {
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions GenumKit/Stencil/Contexts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ extension StoryboardParser {
- `enumName`: `String`
- `strings`: `Array`
- `key`: `String`
- `translation`: `String`
- `params`: `Dictionary` — defined only if localized string has parameters, and in such case contains the following entries:
- `count`: `Int` — number of parameters
- `types`: `Array<String>` containing types like `"String"`, `"Int"`, etc
Expand All @@ -106,9 +107,9 @@ extension StringsFileParser {
"declarations": (0..<entry.types.count).map { "let p\($0)" },
"names": (0..<entry.types.count).map { "p\($0)" }
]
return ["key": entry.key, "params":params]
return ["key": entry.key, "translation": entry.translation, "params": params]
} else {
return ["key": entry.key]
return ["key": entry.key, "translation": entry.translation]
}
}
return Context(dictionary: ["enumName":enumName, "strings":strings])
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,15 @@ The generated code will contain this:

```swift
enum L10n {
/// Title of the alert
case AlertTitle
/// Some alert body there
case AlertMessage
/// Hello, my name is %@ and I'm %d
case Greetings(String, Int)
/// You have %d apples
case ApplesCount(Int)
/// Those %d bananas belong to %@.
case BananasOwner(Int, String)
}

Expand Down Expand Up @@ -186,7 +191,7 @@ let title = L10n.AlertTitle.string

// Alternative syntax, shorter
let msg = tr(.AlertMessage)
// -> "Body of the Alert"
// -> "Some alert body there"

// Strings with parameters
let nbApples = tr(.ApplesCount(5))
Expand Down
4 changes: 4 additions & 0 deletions SwiftGen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
4761E0381BDE753000B9B05F /* Storyboards-Empty.swift.out in Resources */ = {isa = PBXBuildFile; fileRef = 4761E0341BDE753000B9B05F /* Storyboards-Empty.swift.out */; };
4761E0391BDE753000B9B05F /* Strings-Empty.swift.out in Resources */ = {isa = PBXBuildFile; fileRef = 4761E0351BDE753000B9B05F /* Strings-Empty.swift.out */; };
91554FDE6EC0E514DECD71FA /* Pods_UnitTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B594462FF573E83D0DEF1C64 /* Pods_UnitTests.framework */; };
93DA731D1BF34E26000A3C84 /* Strings-File-UTF8-Defaults.swift.out in Resources */ = {isa = PBXBuildFile; fileRef = 93DA731C1BF34E26000A3C84 /* Strings-File-UTF8-Defaults.swift.out */; };
/* End PBXBuildFile section */

/* Begin PBXBuildRule section */
Expand Down Expand Up @@ -162,6 +163,7 @@
4761E0341BDE753000B9B05F /* Storyboards-Empty.swift.out */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Storyboards-Empty.swift.out"; sourceTree = "<group>"; };
4761E0351BDE753000B9B05F /* Strings-Empty.swift.out */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Strings-Empty.swift.out"; sourceTree = "<group>"; };
682F146ADE25633DEBA0841F /* Pods_swiftgen_cli.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_swiftgen_cli.framework; sourceTree = BUILT_PRODUCTS_DIR; };
93DA731C1BF34E26000A3C84 /* Strings-File-UTF8-Defaults.swift.out */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "Strings-File-UTF8-Defaults.swift.out"; sourceTree = "<group>"; };
98E9147EC3C2F7F479CCF739 /* Pods-UnitTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-UnitTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-UnitTests/Pods-UnitTests.debug.xcconfig"; sourceTree = "<group>"; };
B594462FF573E83D0DEF1C64 /* Pods_UnitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_UnitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B85742845DA08FA0DD5D741C /* Pods_SwiftGenKitTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SwiftGenKitTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -258,6 +260,7 @@
09030C261B6D492E00275BF5 /* Strings-Entries-Defaults.swift.out */,
095685EF1B6DA32800BF300C /* Strings-Lines-Defaults.swift.out */,
09030C2A1B6D4D9A00275BF5 /* Strings-File-Defaults.swift.out */,
93DA731C1BF34E26000A3C84 /* Strings-File-UTF8-Defaults.swift.out */,
095685EB1B6DA16200BF300C /* Strings-File-CustomName.swift.out */,
);
path = expected;
Expand Down Expand Up @@ -455,6 +458,7 @@
09EB0A271BDC60F000B2CF79 /* Colors-File-CustomName.swift.out in Resources */,
0911503F1BD4136E00EBC803 /* storyboards-default.stencil in Resources */,
470A14E11BEA63D30052EAC8 /* storyboards-lowercase.stencil in Resources */,
93DA731D1BF34E26000A3C84 /* Strings-File-UTF8-Defaults.swift.out in Resources */,
09EB0A2F1BDC60F000B2CF79 /* Storyboards-Message-Defaults.swift.out in Resources */,
09EB0A2C1BDC60F000B2CF79 /* Images-File-Defaults.swift.out in Resources */,
);
Expand Down
1 change: 1 addition & 0 deletions Templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Below are the variables in the context generated by each SwiftGen subcommand. Th
- `enumName`: `String`
- `strings`: `Array`
- `key`: `String`
- `translation`: `String` - the default translation from the strings file provided
- `params`: `Dictionary` — defined only if localized string has parameters, and in such case contains the following entries:
- `count`: `Int` — number of parameters
- `types`: `Array<String>` containing types like `"String"`, `"Int"`, etc
Expand Down
6 changes: 3 additions & 3 deletions UnitTests/TestSuites/StringsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class StringsTests: XCTestCase {

func testEntriesWithDefaults() {
let parser = StringsFileParser()
parser.addEntry(StringsFileParser.Entry(key: "Title"))
parser.addEntry(StringsFileParser.Entry(key: "Greetings", types: .Object, .Int))
parser.addEntry(StringsFileParser.Entry(key: "Title", translation: "My awesome title"))
parser.addEntry(StringsFileParser.Entry(key: "Greetings", translation: "Hello, my name is %@ and I'm %d", types: .Object, .Int))

let template = GenumTemplate(templateString: fixtureString("strings-default.stencil"))
let result = try! template.render(parser.stencilContext())
Expand Down Expand Up @@ -71,7 +71,7 @@ class StringsTests: XCTestCase {
let template = GenumTemplate(templateString: fixtureString("strings-default.stencil"))
let result = try! template.render(parser.stencilContext())

let expected = self.fixtureString("Strings-File-Defaults.swift.out")
let expected = self.fixtureString("Strings-File-UTF8-Defaults.swift.out")
XCTDiffStrings(result, expected)
}

Expand Down
2 changes: 2 additions & 0 deletions UnitTests/expected/Strings-Entries-Defaults.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import Foundation

enum L10n {
/// My awesome title
case Title
/// Hello, my name is %@ and I'm %d
case Greetings(String, Int)
}

Expand Down
6 changes: 6 additions & 0 deletions UnitTests/expected/Strings-File-CustomName.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import Foundation

enum XCTLoc {
/// Title of the alert
case AlertTitle
/// Some alert body there
case AlertMessage
/// Hello, my name is %@ and I'm %d
case Greetings(String, Int)
/// You have %d apples
case ApplesCount(Int)
/// Those %d bananas belong to %@.
case BananasOwner(Int, String)
/// These are %3$@'s %1$d %2$@.
case ObjectOwnership(Int, String, String)
}

Expand Down
6 changes: 6 additions & 0 deletions UnitTests/expected/Strings-File-Defaults.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import Foundation

enum L10n {
/// Title of the alert
case AlertTitle
/// Some alert body there
case AlertMessage
/// Hello, my name is %@ and I'm %d
case Greetings(String, Int)
/// You have %d apples
case ApplesCount(Int)
/// Those %d bananas belong to %@.
case BananasOwner(Int, String)
/// These are %3$@'s %1$d %2$@.
case ObjectOwnership(Int, String, String)
}

Expand Down
48 changes: 48 additions & 0 deletions UnitTests/expected/Strings-File-UTF8-Defaults.swift.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Generated using SwiftGen, by O.Halligon — https://github.com/AliSoftware/SwiftGen

import Foundation

enum L10n {
/// Titre de l'alerte
case AlertTitle
/// Contenu détaillé de l'alerte
case AlertMessage
/// Bonjour, mon nom est %@ et j'ai %d ans
case Greetings(String, Int)
/// Vous avez %d pommes
case ApplesCount(Int)
/// Ces %d bananes appartiennent à %@.
case BananasOwner(Int, String)
/// Ce sont les %1$d %2$@ de %3$@.
case ObjectOwnership(Int, String, String)
}

extension L10n : CustomStringConvertible {
var description : String { return self.string }

var string : String {
switch self {
case .AlertTitle:
return L10n.tr("alert_title")
case .AlertMessage:
return L10n.tr("alert_message")
case .Greetings(let p0, let p1):
return L10n.tr("greetings", p0, p1)
case .ApplesCount(let p0):
return L10n.tr("apples.count", p0)
case .BananasOwner(let p0, let p1):
return L10n.tr("bananas.owner", p0, p1)
case .ObjectOwnership(let p0, let p1, let p2):
return L10n.tr("object.ownership", p0, p1, p2)
}
}

private static func tr(key: String, _ args: CVarArgType...) -> String {
let format = NSLocalizedString(key, comment: "")
return String(format: format, arguments: args)
}
}

func tr(key: L10n) -> String {
return key.string
}
2 changes: 2 additions & 0 deletions UnitTests/expected/Strings-Lines-Defaults.swift.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import Foundation

enum L10n {
/// My awesome title
case AppTitle
/// My name is %@, I am %d
case GreetingsAndAge(String, Int)
}

Expand Down
1 change: 1 addition & 0 deletions templates/strings-default.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Foundation

enum {{enumName}} {
{% for string in strings %}
/// {{string.translation}}
case {{string.key|swiftIdentifier|snakeToCamelCase}}{% if string.params %}({{string.params.types|join}}){% endif %}
{% endfor %}
}
Expand Down