Skip to content

Commit

Permalink
Merge pull request #10 from abhidsm/master
Browse files Browse the repository at this point in the history
Vapor 4 Support
  • Loading branch information
miroslavkovac authored Aug 6, 2020
2 parents 326f6ae + 2262b21 commit 20c805c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
16 changes: 12 additions & 4 deletions Package.swift
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")
])
]
)
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: "3.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
Expand All @@ -25,21 +28,20 @@ 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")
}
```

> The `localizationsDir` can be omitted, as the _Localizations_ is also the default path. Note that this folder should exist under the _workDir_.

## 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")
```
Expand Down
50 changes: 29 additions & 21 deletions Sources/LingoProvider.swift
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: ())
}

}


5 changes: 3 additions & 2 deletions Tests/LingoVaporTests/LingoVaporTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)] = [
Expand Down

0 comments on commit 20c805c

Please sign in to comment.