Swiftpack.co - Package - nodes-vapor/nstack

NStack 🛠

Swift Version Vapor Version CircleCI codebeat badge codecov Readme Score GitHub license

This package is a wrapper around the NStack.io API.

Supports the following NStack modules:

  • Translate

📦 Installation

Package.swift

Add NStack to the Package dependencies:

dependencies: [
    // ...,
    .package(url: "https://github.com/nodes-vapor/nstack.git", .upToNextMinor(from: "3.0.0"))
]

as well as to your target (e.g. "App"):

targets: [
    .target(name: "App", dependencies: [..., "NStack", ...]),
    // ...
]

Getting started 🚀

Import NStack where needed:

import NStack

Config

Create NStack.Config to configure NStack, your Applications as well as the default Translate.Config.

let nstackConfig = NStack.Config(
    applicationConfigs: [
        Application.Config.init(
            name: "my app name",
            applicationId: "NEVER_PUT_API_IDS_IN_SOURCE_CODE",
            restKey: "NEVER_PUT_API_KEYS_IN_SOURCE_CODE",
            masterKey: "DEFINITELY_NEVER_EXPOSTE_THE_MASTER_KEY_IN_SOURCE_CODE"
        )
    ],
    defaultTranslateConfig: TranslateController.Config(
        defaultPlatform: .backend,
        defaultLanguage: "en-EN",
        cacheInMinutes: 1
    ),
    log: false
)

If you set log to true you will receive helpful logs in case anything goes wrong.

Adding the Service

Instantiate and register NStackProvider with config created in the previous step.

In configure.swift:

try services.register(NStackProvider(config: nstackConfig))

Usage

func getProductName(req: Request) throws -> Future<String> {

    // ...

    let nstack = try req.make(NStack.self)
    let translation = nstack.application.translate.get(on: req, section: "products", key: "nstackForSale")

    return translation
}

You can also provide searchReplacePairs:

func getProductName(req: Request, owner: String) throws -> Future<String> {

    let nstack = try req.make(NStack.self)
    let translation = nstack.application.translate.get(
        on: req,
        section: "products",
        key: "nstackForSale",
        searchReplacePairs: [
            "productOwner" : owner
        ]
    )

    return translation
}

If you are using multiple NStack applications within your project you can switch them with getApplication():

let nstack = try req.make(NStack.self)
let translation = nstack.getApplication("my app name").translate.get(on: req, section: "products", key: "nstackForSale")

Note: you can specify the get() call further in case you don't want to go with the values provided in defaultTranslateConfig:

let translation = nstack.application.translate.get(
    on: req,
    platform: .backend,
    language: "dk-DK",
    section: "products",
    key: "nstackForSale",
    searchReplacePairs: [
        "productOwner" : "Christian"
    ]
)

Caching

NStack uses the KeyedCache registered with Vapor. If you don't register any Cache, this should be the KeyedMemory Cache. If you configure Vapor to prefer another Cache, NStack will use this one instead. Example for Redis:

In configure.swift:

config.prefer(DatabaseKeyedCache<ConfiguredDatabase<RedisDatabase>>.self, for: KeyedCache.self)

Leaf Tag

In order to render the NStack Leaf tags, you will need to add them first:

public func configure(_ config: inout Config, _ env: inout Environment, _ services: inout Services) throws {
    services.register { container -> LeafTagConfig in
        var tags = LeafTagConfig.default()
        try tags.useNStackLeafTags(container)
        return tags
    }
}

NStack comes with a built-in Leaf tag. The tag yields a translated string or the given key if translation fails

// Get translation for camelCasedSection.camelCasedKey
#nstack:translate("camelCasedSection", "camelCasedKey")

// Get translation for camelCasedSection.camelCasedKey and replace searchString1 with replaceString1 etc
#nstack:translate("camelCasedSection", "camelCasedKey", "searchString1", "replaceString1", "searchString2", "replaceString2", ...)

Please note that the leaf tag always uses the current application with the default translate config that you have provided.

🏆 Credits

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Christian.

📄 License

This package is open-sourced software licensed under the MIT license

Github

link
Stars: 5
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

3.0.0-beta.2 - Dec 11, 2018

Changed

  • Remove dependency to Sugar and MutableLeafTagConfig

2.0.5 - Nov 15, 2018

Added

  • Section fetch now takes language parameter (Locale string)

3.0.0-beta.1 - Oct 19, 2018

Changed

  • Rewritten for Vapor 3 🎉

2.0.4 - Oct 10, 2018

Added

  • get(section: String) -method in translate to return all translations in a given section for the default platform and language

2.0.3 - Sep 28, 2018

Changed

  • #nstack() leaf tag now accepts search/replace parameters for placeholders in translations

Added

  • Docs for the improved tag functionality