Skip to content

Commit

Permalink
Add bundle options to set bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayne Wanbok Choi committed Apr 14, 2022
1 parent 75fad74 commit e9fc465
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Options:
--swiftfile [default: ${SRCROOT}/${TARGET_NAME}/Literals.swift] - The output Swift file directory.
--stringsfile [default: ${SRCROOT}/${TARGET_NAME}/Localizable.strings] - The output Strings file directory.
--access [default: public] - The access modifier.
--bundle [default: main] - The bundle modifier. It can be only `main` or `module`.
```

## Authors & Collaborators
Expand Down
38 changes: 28 additions & 10 deletions Sources/POEditorParser/Lib/LiteralsParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,30 @@ public struct Translation {
return rawKey.capitalized.replacingOccurrences(of: "_", with: "")
}

func swiftCode(accessModifier: AccessModifier = .public) -> String {
func swiftCode(accessModifier: AccessModifier = .public, bundleModifier: BundleModifier = .main) -> String {
if variables.isEmpty {
return generateVariableLessSwiftCode(accessModifier: accessModifier)
return generateVariableLessSwiftCode(accessModifier: accessModifier, bundleModifier: bundleModifier)
} else {
return generateVariableSwiftCode(accessModifier: accessModifier)
return generateVariableSwiftCode(accessModifier: accessModifier, bundleModifier: bundleModifier)
}
}

private func generateVariableLessSwiftCode(accessModifier: AccessModifier = .public) -> String {
private func generateVariableLessSwiftCode(
accessModifier: AccessModifier,
bundleModifier: BundleModifier
) -> String {
/*
static var Welcome: String {
return NSLocalizedString()
}
*/
return "\t\(accessModifier.rawValue) static var \(prettyKey): String {\n\t\treturn \(localizedStringFunction)(\"\(rawKey)\", comment: \"\")\n\t}\n"
return "\t\(accessModifier.rawValue) static var \(prettyKey): String {\n\t\treturn \(localizedStringFunction)(\"\(rawKey)\", bundle: \(bundleModifier.rawValue), comment: \"\")\n\t}\n"
}

private func generateVariableSwiftCode(accessModifier: AccessModifier = .public) -> String {
private func generateVariableSwiftCode(
accessModifier: AccessModifier,
bundleModifier: BundleModifier
) -> String {
/*
static func ReadBooksKey(readNumber: Int) -> String {
return ""
Expand All @@ -141,7 +147,7 @@ public struct Translation {
.map { $0.parameterKey }
.map { $0.snakeCased() }
.joined(separator: ", ")
return "\t\(accessModifier.rawValue) static func \(prettyKey)(\(parameters)) -> String {\n\t\treturn String(format: \(localizedStringFunction)(\"\(rawKey)\", comment: \"\"), \(localizedArguments))\n\t}"
return "\t\(accessModifier.rawValue) static func \(prettyKey)(\(parameters)) -> String {\n\t\treturn String(format: \(localizedStringFunction)(\"\(rawKey)\", bundle: \(bundleModifier.rawValue), comment: \"\"), \(localizedArguments))\n\t}"
}

}
Expand Down Expand Up @@ -182,12 +188,15 @@ public class FileCodeGenerator: SwiftCodeGenerator {

let fileHandle: FileHandle
let accessModifier: AccessModifier
let bundleModifier: BundleModifier
public init(
fileHandle: FileHandle,
access: String
access: String,
bundle: String
) {
self.fileHandle = fileHandle
self.accessModifier = AccessModifier(accessString: access)
self.accessModifier = .init(accessString: access)
self.bundleModifier = .init(bundleName: bundle)
}

// TODO: Generalize!!! += (same code as in string)
Expand All @@ -196,7 +205,7 @@ public class FileCodeGenerator: SwiftCodeGenerator {

for t in translations {
fileHandle += SwiftCodeGeneratorConstants.methodOrVariableHeader
fileHandle += t.swiftCode(accessModifier: accessModifier)
fileHandle += t.swiftCode(accessModifier: accessModifier, bundleModifier: bundleModifier)
}

fileHandle += SwiftCodeGeneratorConstants.rootObjectFooter
Expand Down Expand Up @@ -364,6 +373,15 @@ enum AccessModifier: String {
}
}

enum BundleModifier: String {
case main
case module

init(bundleName: String) {
self = BundleModifier(rawValue: bundleName) ?? .main
}
}


extension String {
var first: String {
Expand Down
7 changes: 4 additions & 3 deletions Sources/POEditorParser/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ command(
Argument<String>("language", description: "The language code"),
Option<String>("swiftfile", default: "${SRCROOT}/${TARGET_NAME}/Literals.swift", description: "The output Swift file directory."),
Option<String>("stringsfile", default: "${SRCROOT}/${TARGET_NAME}/Localizable.strings", description: "The output Strings file directory."),
Option<String>("access", default: "public", description: "The access modifier.")
) { (token: String, id: Int, language: String, swiftfile: String, stringsfile: String, access: String) in
Option<String>("access", default: "public", description: "The access modifier."),
Option<String>("bundle", default: "main", description: "The bundle modifier.")
) { (token: String, id: Int, language: String, swiftfile: String, stringsfile: String, access: String, bundle: String) in

print("Fetching contents of strings at POEditor...".blue)

Expand Down Expand Up @@ -61,7 +62,7 @@ command(
print("Fatal error: Couldn't write to file located at \(swiftfile)".red)
return
}
let fileCodeGenerator = FileCodeGenerator(fileHandle: swiftHandle, access: access)
let fileCodeGenerator = FileCodeGenerator(fileHandle: swiftHandle, access: access, bundle: bundle)
fileCodeGenerator.generateCode(translations: translations)
print("Success! Literals generated at \(swiftfile)".green)

Expand Down

0 comments on commit e9fc465

Please sign in to comment.