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
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)

View File

@ -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

View File

@ -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