From edf10b111a7f38c4a98846434ee6518dbd960caa Mon Sep 17 00:00:00 2001 From: david-swift Date: Sat, 2 Mar 2024 17:09:19 +0100 Subject: [PATCH] Use type CustomStringConvertible for arguments --- README.md | 2 +- Sources/GenerationLibrary/Generation.swift | 13 ++++++++----- Tests/PluginTests/Tests.swift | 10 +++++++--- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4305dbd..c26ec33 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Then, access the localized strings safely in your code: // Use the system language print(Loc.hello(name: "Peter")) print(Loc.house) -print(Loc.houses(count: "1")) +print(Loc.houses(count: 1)) // Access the translation for a specific language print(Localized.hello(name: "Peter").en) diff --git a/Sources/GenerationLibrary/Generation.swift b/Sources/GenerationLibrary/Generation.swift index 5594330..44eeb33 100644 --- a/Sources/GenerationLibrary/Generation.swift +++ b/Sources/GenerationLibrary/Generation.swift @@ -89,7 +89,7 @@ public enum Generation { } else { var line = "case \(key.0)(" for argument in key.1 { - line += "\(argument): String, " + line += "\(argument): CustomStringConvertible, " } line.removeLast(", ".count) line += ")" @@ -117,7 +117,7 @@ public enum Generation { } else { var line = "static func \(key.0)(" for argument in key.1 { - line += "\(argument): String, " + line += "\(argument): CustomStringConvertible, " } line.removeLast(", ".count) line += ") -> String {\n" + indent("Localized.\(key.0)(", by: indentOne) @@ -190,21 +190,24 @@ public enum Generation { var value = "\n" let conditionTranslations = translations.filter { $0.key.hasPrefix(language + "(") } let lastTranslation = parse(translation: defaultTranslation, arguments: arguments) + for argument in arguments { + value += "let \(argument) = \(argument).description\n" + } if conditionTranslations.isEmpty { - return indent("\n\"\(lastTranslation)\"", by: indentThree) + return indent(value + "return \"\(lastTranslation)\"", by: indentThree) } for translation in conditionTranslations { var condition = translation.key.split(separator: "(")[1] condition.removeLast() value.append(indent(""" if \(condition) { - \"\(parse(translation: translation.value, arguments: arguments))\" + return \"\(parse(translation: translation.value, arguments: arguments))\" } else """, by: indentThree)) } value.append(""" { - \"\(lastTranslation)\" + return \"\(lastTranslation)\" } """) return value diff --git a/Tests/PluginTests/Tests.swift b/Tests/PluginTests/Tests.swift index 920be26..0ec8a54 100644 --- a/Tests/PluginTests/Tests.swift +++ b/Tests/PluginTests/Tests.swift @@ -5,6 +5,8 @@ // Created by david-swift on 27.02.2024. // +// swiftlint:disable no_magic_numbers + import Foundation /// Test cases for the `GenerateLocalized` plugin. @@ -20,9 +22,11 @@ enum Tests { print("DE_CH: \(Localized.house.string(for: "de_CH"))") print("SYSTEM: \(Localized.house.string)") print("EN: \(Localized.helloPair(name1: "Max", name2: "Ruedi").en)") - print(Loc.houses(count: "0")) - print(Loc.houses(count: "1")) - print(Loc.houses(count: "2")) + print(Loc.houses(count: 0)) + print(Loc.houses(count: 1)) + print(Loc.houses(count: 2)) } } + +// swiftlint:enable no_magic_numbers