Remove deprecated Swift macro
This commit is contained in:
parent
3b5837a44e
commit
8135771fc0
@ -24,9 +24,7 @@ let package = Package(
|
|||||||
)
|
)
|
||||||
],
|
],
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.package(url: "https://github.com/apple/swift-syntax", from: "509.0.0"),
|
.package(url: "https://github.com/jpsim/Yams", from: "5.0.6")
|
||||||
.package(url: "https://github.com/jpsim/Yams", from: "5.0.6"),
|
|
||||||
.package(url: "https://github.com/stackotter/swift-macro-toolkit", from: "0.3.1")
|
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
.target(
|
.target(
|
||||||
@ -41,15 +39,6 @@ let package = Package(
|
|||||||
"GenerationLibrary"
|
"GenerationLibrary"
|
||||||
]
|
]
|
||||||
),
|
),
|
||||||
.macro(
|
|
||||||
name: "LocalizedMacros",
|
|
||||||
dependencies: [
|
|
||||||
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
|
|
||||||
.product(name: "MacroToolkit", package: "swift-macro-toolkit"),
|
|
||||||
.product(name: "SwiftCompilerPlugin", package: "swift-syntax"),
|
|
||||||
"GenerationLibrary"
|
|
||||||
]
|
|
||||||
),
|
|
||||||
.plugin(
|
.plugin(
|
||||||
name: "GenerateLocalized",
|
name: "GenerateLocalized",
|
||||||
capability: .buildTool(),
|
capability: .buildTool(),
|
||||||
@ -58,17 +47,7 @@ let package = Package(
|
|||||||
]
|
]
|
||||||
),
|
),
|
||||||
.target(
|
.target(
|
||||||
name: "Localized",
|
name: "Localized"
|
||||||
dependencies: [
|
|
||||||
"LocalizedMacros"
|
|
||||||
]
|
|
||||||
),
|
|
||||||
.executableTarget(
|
|
||||||
name: "MacroTests",
|
|
||||||
dependencies: [
|
|
||||||
"Localized"
|
|
||||||
],
|
|
||||||
path: "Tests/MacroTests"
|
|
||||||
),
|
),
|
||||||
.executableTarget(
|
.executableTarget(
|
||||||
name: "PluginTests",
|
name: "PluginTests",
|
||||||
|
|||||||
25
README.md
25
README.md
@ -99,31 +99,6 @@ to the target in the `Package.swift` file.
|
|||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
|
||||||
|
|
||||||
<summary> Use the Swift macro alternatively </summary>
|
|
||||||
|
|
||||||
If you don't want to have a separate `Localized.yml` resource, you can use the
|
|
||||||
YML syntax directly in your Swift code using a Swift macro.
|
|
||||||
Leave out the `resources` and `plugins` lines in the target definition, and
|
|
||||||
instead of creating a `Localized.yml` file, use the following macro in a Swift file.
|
|
||||||
|
|
||||||
```swift
|
|
||||||
#localized(default: "en", yml: """
|
|
||||||
export:
|
|
||||||
en: Export Document
|
|
||||||
de: Exportiere das Dokument
|
|
||||||
|
|
||||||
send(message, name):
|
|
||||||
en: Send (message) to (name).
|
|
||||||
de: Sende (message) to (name).
|
|
||||||
""")
|
|
||||||
```
|
|
||||||
|
|
||||||
You cannot have a `defaultLanguage` set in the YML, instead, use the macro parameter.
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
### Usage
|
### Usage
|
||||||
|
|
||||||
In most cases, you want to get the translated string in the system language.
|
In most cases, you want to get the translated string in the system language.
|
||||||
|
|||||||
@ -1,13 +0,0 @@
|
|||||||
//
|
|
||||||
// Localized.swift
|
|
||||||
// Localized
|
|
||||||
//
|
|
||||||
// Created by david-swift on 27.02.2024.
|
|
||||||
//
|
|
||||||
|
|
||||||
/// A macro that takes the YML syntax and converts it into enumerations.
|
|
||||||
@freestanding(declaration, names: named(Localized), named(Loc))
|
|
||||||
public macro localized(default defaultLanguage: String, yml: String) = #externalMacro(
|
|
||||||
module: "LocalizedMacros",
|
|
||||||
type: "LocalizedMacro"
|
|
||||||
)
|
|
||||||
@ -1,51 +0,0 @@
|
|||||||
//
|
|
||||||
// LocalizedMacro.swift
|
|
||||||
// Localized
|
|
||||||
//
|
|
||||||
// Created by david-swift on 27.02.2024.
|
|
||||||
//
|
|
||||||
|
|
||||||
import GenerationLibrary
|
|
||||||
import MacroToolkit
|
|
||||||
import SwiftSyntax
|
|
||||||
import SwiftSyntaxMacros
|
|
||||||
|
|
||||||
/// Implementation of the `localized` macro, which takes YML
|
|
||||||
/// as a string and converts it into two enumerations.
|
|
||||||
/// Access a specific language using `Localized.key.language`, or use `Localized.key.string`
|
|
||||||
/// which automatically uses the system language on Linux, macOS and Windows.
|
|
||||||
/// Use `Loc.key` for a quick access to the automatically localized value.
|
|
||||||
public struct LocalizedMacro: DeclarationMacro {
|
|
||||||
|
|
||||||
/// The errors the expansion can throw.
|
|
||||||
public enum LocalizedError: Error {
|
|
||||||
|
|
||||||
/// The string literal syntax is invalid.
|
|
||||||
case invalidStringLiteral
|
|
||||||
/// The default language syntax is invalid.
|
|
||||||
case invalidDefaultLanguage
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Expand the `localized` macro.
|
|
||||||
/// - Parameters:
|
|
||||||
/// - node: Information about the macro call.
|
|
||||||
/// - context: The expansion context.
|
|
||||||
/// - Returns: The enumerations `Localized` and `Loc`.
|
|
||||||
public static func expansion(
|
|
||||||
of node: some SwiftSyntax.FreestandingMacroExpansionSyntax,
|
|
||||||
in context: some SwiftSyntaxMacros.MacroExpansionContext
|
|
||||||
) throws -> [SwiftSyntax.DeclSyntax] {
|
|
||||||
guard let `default` = node.argumentList.first?.expression.as(StringLiteralExprSyntax.self),
|
|
||||||
let defaultLanguage = StringLiteral(`default`).value?.description else {
|
|
||||||
throw LocalizedError.invalidDefaultLanguage
|
|
||||||
}
|
|
||||||
guard let syntax = node.argumentList.last?.expression.as(StringLiteralExprSyntax.self),
|
|
||||||
var yml = StringLiteral(syntax).value else {
|
|
||||||
throw LocalizedError.invalidStringLiteral
|
|
||||||
}
|
|
||||||
yml.append("\n\ndefault: \"\(defaultLanguage)\"")
|
|
||||||
return try Generation.getCode(yml: yml).map { "\(raw: $0)" }
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,20 +0,0 @@
|
|||||||
//
|
|
||||||
// LocalizedPlugin.swift
|
|
||||||
// Localized
|
|
||||||
//
|
|
||||||
// Created by david-swift on 27.02.2024.
|
|
||||||
//
|
|
||||||
|
|
||||||
import SwiftCompilerPlugin
|
|
||||||
import SwiftSyntaxMacros
|
|
||||||
|
|
||||||
/// The compiler plugin offering the `localized` macro.
|
|
||||||
@main
|
|
||||||
struct LocalizedPlugin: CompilerPlugin {
|
|
||||||
|
|
||||||
/// The macros.
|
|
||||||
let providingMacros: [Macro.Type] = [
|
|
||||||
LocalizedMacro.self
|
|
||||||
]
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,43 +0,0 @@
|
|||||||
//
|
|
||||||
// Tests.swift
|
|
||||||
// Localized
|
|
||||||
//
|
|
||||||
// Created by david-swift on 27.02.2024.
|
|
||||||
//
|
|
||||||
|
|
||||||
import Foundation
|
|
||||||
import Localized
|
|
||||||
|
|
||||||
#localized(default: "en", yml: """
|
|
||||||
hello(name):
|
|
||||||
en: Hello, (name)!
|
|
||||||
de: Hallo, (name)!
|
|
||||||
fr: Salut, (name)!
|
|
||||||
|
|
||||||
house:
|
|
||||||
en: House
|
|
||||||
de: Haus
|
|
||||||
fr: Maison
|
|
||||||
|
|
||||||
helloPair(name1, name2):
|
|
||||||
en: Hello, (name1) and (name2)!
|
|
||||||
de: Hallo, (name1) und (name2)!
|
|
||||||
fr: Salut, (name1) et (name2)!
|
|
||||||
""")
|
|
||||||
|
|
||||||
/// Test cases for the `localized` macro.
|
|
||||||
@main
|
|
||||||
enum Tests {
|
|
||||||
|
|
||||||
/// Test the `localized` macro.
|
|
||||||
static func main() {
|
|
||||||
print("EN: \(Localized.hello(name: "Peter").en)")
|
|
||||||
print("DE: \(Localized.hello(name: "Ruedi").de)")
|
|
||||||
print("SYSTEM: \(Loc.hello(name: "Sams"))")
|
|
||||||
print("FR: \(Localized.house.fr)")
|
|
||||||
print("DE_CH: \(Localized.house.string(for: "de_CH"))")
|
|
||||||
print("SYSTEM: \(Localized.house.string)")
|
|
||||||
print("EN: \(Localized.helloPair(name1: "Max", name2: "Ruedi").en)")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
x
Reference in New Issue
Block a user