-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from abhidsm/master
Vapor 4 Support
- Loading branch information
Showing
4 changed files
with
53 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
// swift-tools-version:4.0 | ||
// swift-tools-version:5.2 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "LingoVapor", | ||
platforms: [ | ||
.macOS(.v10_15) | ||
], | ||
products: [ | ||
.library(name: "LingoVapor", targets: ["LingoVapor"]) | ||
], | ||
dependencies: [ | ||
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"), | ||
.package(url: "https://github.com/vapor/vapor.git", from: "4.27.0"), | ||
.package(url: "https://github.com/miroslavkovac/Lingo.git", from: "3.0.5") | ||
], | ||
targets: [ | ||
.target(name: "LingoVapor", dependencies: ["Vapor", "Lingo"], path: "Sources/"), | ||
.testTarget(name: "LingoVaporTests", dependencies: ["LingoVapor", "Vapor"]) | ||
.target(name: "LingoVapor", dependencies: [ | ||
.product(name: "Vapor", package: "vapor"), | ||
.product(name: "Lingo", package: "Lingo") | ||
], path: "Sources/"), | ||
.testTarget(name: "LingoVaporTests", dependencies: [ | ||
.target(name: "LingoVapor") | ||
]) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,45 @@ | ||
import Vapor | ||
import Lingo | ||
|
||
extension Lingo: Service {} | ||
extension Application { | ||
public var lingoVapor: LingoProvider { | ||
.init(application: self) | ||
} | ||
} | ||
|
||
extension Container { | ||
public struct LingoProvider { | ||
let application: Application | ||
|
||
public func lingo() throws -> Lingo { | ||
return try self.make(Lingo.self) | ||
public init(application: Application) { | ||
self.application = application | ||
} | ||
|
||
public func lingo() throws -> Lingo { | ||
let directory = DirectoryConfiguration.detect().workingDirectory | ||
let workDir = directory.hasSuffix("/") ? directory : directory + "/" | ||
let rootPath = workDir + (configuration?.localizationsDir ?? "") | ||
return try Lingo(rootPath: rootPath, defaultLocale: (configuration?.defaultLocale ?? "")) | ||
} | ||
} | ||
|
||
public struct LingoProvider: Vapor.Provider { | ||
extension LingoProvider { | ||
struct ConfigurationKey: StorageKey { | ||
typealias Value = LingoConfiguration | ||
} | ||
|
||
let defaultLocale: String | ||
let localizationsDir: String | ||
public var configuration: LingoConfiguration? { | ||
get { application.storage[ConfigurationKey.self] } | ||
nonmutating set { application.storage[ConfigurationKey.self] = newValue } | ||
} | ||
} | ||
|
||
public struct LingoConfiguration { | ||
let defaultLocale, localizationsDir: String | ||
|
||
public init(defaultLocale: String, localizationsDir: String = "Localizations") { | ||
self.defaultLocale = defaultLocale | ||
self.localizationsDir = localizationsDir | ||
} | ||
|
||
public func register(_ services: inout Services) throws { | ||
services.register(Lingo.self) { (container) -> (Lingo) in | ||
let dirConfig = try container.make(DirectoryConfig.self) | ||
let workDir = dirConfig.workDir.hasSuffix("/") ? dirConfig.workDir : dirConfig.workDir + "/" | ||
let rootPath = workDir + self.localizationsDir | ||
return try Lingo(rootPath: rootPath, defaultLocale: self.defaultLocale) | ||
} | ||
} | ||
|
||
public func didBoot(_ container: Container) throws -> EventLoopFuture<Void> { | ||
return container.eventLoop.newSucceededFuture(result: ()) | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters