From b6c11c7fc4a741adc3741e0684d47fe2d617e226 Mon Sep 17 00:00:00 2001 From: Abhilash M A Date: Thu, 6 Aug 2020 09:36:01 +0530 Subject: [PATCH 1/4] Vapor 4 support --- Package.swift | 16 +++++-- README.md | 11 +++-- Sources/LingoProvider.swift | 50 ++++++++++++--------- Tests/LingoVaporTests/LingoVaporTests.swift | 5 ++- 4 files changed, 49 insertions(+), 33 deletions(-) diff --git a/Package.swift b/Package.swift index f7ae95a..5070073 100644 --- a/Package.swift +++ b/Package.swift @@ -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") + ]) ] ) diff --git a/README.md b/README.md index d96747c..a724f70 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Add LingoProvider as a dependancy in your `Package.swift` file: ```swift dependencies: [ ..., - .package(url: "https://github.com/vapor-community/lingo-vapor.git", from: "3.0.0")] + .package(url: "https://github.com/vapor-community/lingo-vapor.git", from: "4.0.0")] ] ``` @@ -25,10 +25,9 @@ In the `configure.swift` simply initialize the `LingoVapor` with a default local ```swift import LingoVapor ... -public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws { +public func configure(_ app: Application) throws { ... - let lingoProvider = LingoProvider(defaultLocale: "en", localizationsDir: "Localizations") - try services.register(lingoProvider) + app.lingoVapor.configuration = .init(defaultLocale: "en", localizationsDir: "Localizations") } ``` @@ -36,10 +35,10 @@ public func configure(_ config: inout Config, _ env: inout Environment, _ servic ## Use -After you have registered the provider, you can use any `Container` to create `Lingo`: +After you have configured the provider, you can use `lingoVapor` service to create `Lingo`: ```swift -let lingo = try someContainer.make(Lingo.self) +let lingo = try app.lingoVapor.lingo() ... let localizedTitle = lingo.localize("welcome.title", locale: "en") ``` diff --git a/Sources/LingoProvider.swift b/Sources/LingoProvider.swift index 5d91941..908b15a 100644 --- a/Sources/LingoProvider.swift +++ b/Sources/LingoProvider.swift @@ -1,37 +1,45 @@ import Vapor import Lingo -extension Lingo: Service {} +extension Application { + 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] } + 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 { - return container.eventLoop.newSucceededFuture(result: ()) - } - } + + diff --git a/Tests/LingoVaporTests/LingoVaporTests.swift b/Tests/LingoVaporTests/LingoVaporTests.swift index a7dfb83..329b6ad 100644 --- a/Tests/LingoVaporTests/LingoVaporTests.swift +++ b/Tests/LingoVaporTests/LingoVaporTests.swift @@ -5,8 +5,9 @@ import XCTest class LingoVaporTests: XCTestCase { func testInitialization() throws { - let lingoProvider = LingoProvider(defaultLocale: "en", localizationsDir: "Localizations") - XCTAssertEqual(lingoProvider.defaultLocale, "en") + var lingoProvider = LingoProvider(application: Application()) + lingoProvider.configuration = .init(defaultLocale: "en", localizationsDir: "Localizations") + XCTAssertEqual(lingoProvider.configuration?.defaultLocale, "en") } static var allTests: [(String, (LingoVaporTests) -> () throws -> Void)] = [ From f8b5cadbf716478067a43d57d621f07a0e015361 Mon Sep 17 00:00:00 2001 From: Abhilash M A Date: Thu, 6 Aug 2020 09:43:01 +0530 Subject: [PATCH 2/4] Fixed the public variable for LingoVapor --- Sources/LingoProvider.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LingoProvider.swift b/Sources/LingoProvider.swift index 908b15a..b958cc4 100644 --- a/Sources/LingoProvider.swift +++ b/Sources/LingoProvider.swift @@ -2,7 +2,7 @@ import Vapor import Lingo extension Application { - var lingoVapor: LingoProvider { + public var lingoVapor: LingoProvider { .init(application: self) } } From 4faa6857f4c5e86b581a69d210fd9744c3992b41 Mon Sep 17 00:00:00 2001 From: Abhilash M A Date: Thu, 6 Aug 2020 10:10:56 +0530 Subject: [PATCH 3/4] Fixed the nonmutating property issue --- Sources/LingoProvider.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/LingoProvider.swift b/Sources/LingoProvider.swift index b958cc4..0b4aba7 100644 --- a/Sources/LingoProvider.swift +++ b/Sources/LingoProvider.swift @@ -29,7 +29,7 @@ extension LingoProvider { public var configuration: LingoConfiguration? { get { application.storage[ConfigurationKey.self] } - set { application.storage[ConfigurationKey.self] = newValue } + nonmutating set { application.storage[ConfigurationKey.self] = newValue } } } From 2262b21b66196d9e864f26a93ffc2f9bb12992a4 Mon Sep 17 00:00:00 2001 From: Abhilash M A Date: Thu, 6 Aug 2020 10:25:34 +0530 Subject: [PATCH 4/4] Updated the README file --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a724f70..9130f3c 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,11 @@ Add LingoProvider as a dependancy in your `Package.swift` file: ```swift dependencies: [ ..., - .package(url: "https://github.com/vapor-community/lingo-vapor.git", from: "4.0.0")] -] + .package(name: "LingoVapor", url: "https://github.com/vapor-community/lingo-vapor.git", from: "4.0.0")] +], +targets: [ + .target(name: "App", dependencies: [ + .product(name: "LingoVapor", package: "LingoVapor") ``` ### Add the Provider