diff --git a/README.md b/README.md index e1f1761..0b9e07a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/Sources/POEditorParser/Lib/LiteralsParser.swift b/Sources/POEditorParser/Lib/LiteralsParser.swift index bbebafb..49fb068 100644 --- a/Sources/POEditorParser/Lib/LiteralsParser.swift +++ b/Sources/POEditorParser/Lib/LiteralsParser.swift @@ -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 "" @@ -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}" } } @@ -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) @@ -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 @@ -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 { diff --git a/Sources/POEditorParser/main.swift b/Sources/POEditorParser/main.swift index 30aad34..78856ca 100644 --- a/Sources/POEditorParser/main.swift +++ b/Sources/POEditorParser/main.swift @@ -11,8 +11,9 @@ command( Argument("language", description: "The language code"), Option("swiftfile", default: "${SRCROOT}/${TARGET_NAME}/Literals.swift", description: "The output Swift file directory."), Option("stringsfile", default: "${SRCROOT}/${TARGET_NAME}/Localizable.strings", description: "The output Strings file directory."), - Option("access", default: "public", description: "The access modifier.") -) { (token: String, id: Int, language: String, swiftfile: String, stringsfile: String, access: String) in + Option("access", default: "public", description: "The access modifier."), + Option("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) @@ -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)