Skip to content

Commit

Permalink
Use type CustomStringConvertible for arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Mar 2, 2024
1 parent f498347 commit edf10b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions Sources/GenerationLibrary/Generation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 += ")"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions Tests/PluginTests/Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

0 comments on commit edf10b1

Please sign in to comment.