Skip to content

Use curly bracket placeholders within strings and be able to use the same localization phrases as on Android.

License

Notifications You must be signed in to change notification settings

nextbike/phrase-swift

Repository files navigation

PhraseSwift

Description: Use curly bracket placeholders within strings just like on Android. We created this in order to use the same translation phrases for both our Android and iOS apps.

This is a lightweight port of Phrase - Android string formatting into Swift 5 and used in production for our iOS customer app. It has no other dependencies and can be installed using Swift Package Manager or CocoaPods.

Features

  • Replace placeholders with curly brackets within strings with corresponding content ("Hello, my name is {name}" becomes "Hello, my name is Ada")
  • Supports multiple placeholders per string
  • When using Phrase for Android and PhraseSwift for iOS, you can use the same translation sources for both platforms
  • Licence: Apache 2.0

Installation

Swift Package Manager (SPM)

This is the recommended way to install and use this library.

Within an Xcode Project

In Project Settings, on the tab "Package Dependencies", click "+" and add github.com/nextbike/phrase-swift

Package.swift-Based SPM Project

  1. Add a dependency in Package.swift:
    dependencies: [
        .package(url: "https://github.com/nextbike/phrase-swift", from: "1.0.1")
    ]
  2. For each relevant target, add a dependency
    .target(
        name: "Example",
        dependencies: [
            .product(name: "PhraseSwift", package: "phrase-swift"),
        ]
    )

Also check out Editing a package dependency as a local package to work in local changes within your existing project that uses this package.

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website.

pod 'PhraseSwift', :git => 'https://github.com/nextbike/phrase-swift.git', '~> 1.0.1

Usage

import PhraseSwift

let source = "Hello {world}"
let output = Phrase.localize(source, keyValues: ["world": "nextbike"])

// output: "Hello nextbike"

How to test

Includes a suite of unit tests to cover frequent cases of placeholder location and handling within strings. Run swift test or open up the Swift Package within Xcode.

About

Use curly bracket placeholders within strings and be able to use the same localization phrases as on Android.

Topics

Resources

License

Stars

Watchers

Forks