Use type CustomStringConvertible for arguments

This commit is contained in:
david-swift 2024-03-02 17:09:19 +01:00
parent f498347485
commit edf10b111a
3 changed files with 16 additions and 9 deletions

View File

@ -41,7 +41,7 @@ Then, access the localized strings safely in your code:
// Use the system language // Use the system language
print(Loc.hello(name: "Peter")) print(Loc.hello(name: "Peter"))
print(Loc.house) print(Loc.house)
print(Loc.houses(count: "1")) print(Loc.houses(count: 1))
// Access the translation for a specific language // Access the translation for a specific language
print(Localized.hello(name: "Peter").en) print(Localized.hello(name: "Peter").en)

View File

@ -89,7 +89,7 @@ public enum Generation {
} else { } else {
var line = "case \(key.0)(" var line = "case \(key.0)("
for argument in key.1 { for argument in key.1 {
line += "\(argument): String, " line += "\(argument): CustomStringConvertible, "
} }
line.removeLast(", ".count) line.removeLast(", ".count)
line += ")" line += ")"
@ -117,7 +117,7 @@ public enum Generation {
} else { } else {
var line = "static func \(key.0)(" var line = "static func \(key.0)("
for argument in key.1 { for argument in key.1 {
line += "\(argument): String, " line += "\(argument): CustomStringConvertible, "
} }
line.removeLast(", ".count) line.removeLast(", ".count)
line += ") -> String {\n" + indent("Localized.\(key.0)(", by: indentOne) line += ") -> String {\n" + indent("Localized.\(key.0)(", by: indentOne)
@ -190,21 +190,24 @@ public enum Generation {
var value = "\n" var value = "\n"
let conditionTranslations = translations.filter { $0.key.hasPrefix(language + "(") } let conditionTranslations = translations.filter { $0.key.hasPrefix(language + "(") }
let lastTranslation = parse(translation: defaultTranslation, arguments: arguments) let lastTranslation = parse(translation: defaultTranslation, arguments: arguments)
for argument in arguments {
value += "let \(argument) = \(argument).description\n"
}
if conditionTranslations.isEmpty { if conditionTranslations.isEmpty {
return indent("\n\"\(lastTranslation)\"", by: indentThree) return indent(value + "return \"\(lastTranslation)\"", by: indentThree)
} }
for translation in conditionTranslations { for translation in conditionTranslations {
var condition = translation.key.split(separator: "(")[1] var condition = translation.key.split(separator: "(")[1]
condition.removeLast() condition.removeLast()
value.append(indent(""" value.append(indent("""
if \(condition) { if \(condition) {
\"\(parse(translation: translation.value, arguments: arguments))\" return \"\(parse(translation: translation.value, arguments: arguments))\"
} else } else
""", by: indentThree)) """, by: indentThree))
} }
value.append(""" value.append("""
{ {
\"\(lastTranslation)\" return \"\(lastTranslation)\"
} }
""") """)
return value return value

View File

@ -5,6 +5,8 @@
// Created by david-swift on 27.02.2024. // Created by david-swift on 27.02.2024.
// //
// swiftlint:disable no_magic_numbers
import Foundation import Foundation
/// Test cases for the `GenerateLocalized` plugin. /// Test cases for the `GenerateLocalized` plugin.
@ -20,9 +22,11 @@ enum Tests {
print("DE_CH: \(Localized.house.string(for: "de_CH"))") print("DE_CH: \(Localized.house.string(for: "de_CH"))")
print("SYSTEM: \(Localized.house.string)") print("SYSTEM: \(Localized.house.string)")
print("EN: \(Localized.helloPair(name1: "Max", name2: "Ruedi").en)") print("EN: \(Localized.helloPair(name1: "Max", name2: "Ruedi").en)")
print(Loc.houses(count: "0")) print(Loc.houses(count: 0))
print(Loc.houses(count: "1")) print(Loc.houses(count: 1))
print(Loc.houses(count: "2")) print(Loc.houses(count: 2))
} }
} }
// swiftlint:enable no_magic_numbers