Swiftpack.co - seatcode/Unstringify as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by seatcode.
seatcode/Unstringify 0.4.3
Code generator for strong-typing localizable strings
⭐️ 3
🕓 3 years ago
.package(url: "https://github.com/seatcode/Unstringify.git", from: "0.4.3")

Unstringify

PRs Welcome

☝️ What?

  • unstringify (CLI): Code generator for strong-typing localizable strings.
  • Unstringified (module): Strong-typed localizable strings static source code.

🤔 Why?

It makes your localized strings code:

  • compile time checked,
  • strong typed,
  • autocompletable by Xcode.

🛠 How?

Installation

There are several methods to install Unstringify:

CocoaPods

Add unstringify specs to your Podfile:

pod 'Unstringify'

Add Unstringified as dependency in the podspec of the module that contains the strings files:

s.dependency 'Unstringified'

Note: If your app is not modularized, simply add pod 'Unstringified' to your Podfile too.

Add a Build Phase that generates the code for the Unstringified enumerations:

"$PODS_ROOT/Unstringify/unstringify" "$SRCROOT/path/to/module/en.lproj/Localizable.strings" "$SRCROOT/path/to/module/Unstringified.generated.swift"

Swift Package Manager

Declare Unstringify dependency in your Package.swift file:

dependencies: [
    .package(url: "https://github.com/metrolab/Unstringify", from: "0.1.0"),
]

Execute the CLI passing as arguments the input strings file and the path to generated output file:

swift run unstringify path/to/module/en.lproj/Localizable.string path/to/module/Unstringified.generated.swift

In order to use the generated file, you will need to link Unstringified to the module that contains the generated file and the original strings files.

Usage

CLI usage:

$ ./unstringify
Usage: ./unstringify inputPath outputPath [templatePath]

Note: templatePath is optional.

Input (e.g. en.lproj/Localizable.strings):

"form_title" = "Contact";
"form_name_field" = "Name:";
"form_name_field_max_length" = "Maximum length is %d characters.";
"form_name_field_description" = "Enter your <b>full name</b>";

Output (e.g. Unstringified.generated.swift):

//  Generated by Unstringify.
//  DO NOT EDIT!

import Foundation
import Unstringified

private final class _Unstringified {}

extension Unstringified {
    public var localizableStringsTableName: String? {
        return nil
    }

    public var localizableStringsBundle: Bundle? {
        let _UnstringifiedBundle = Bundle(for: _Unstringified.self)
        guard _UnstringifiedBundle.bundleIdentifier != Bundle.main.bundleIdentifier else {
            return Bundle.main
        }
        let bundleURL = _UnstringifiedBundle.bundleURL
        let bundleName = bundleURL.lastPathComponent
        let resource = (bundleName as NSString).deletingPathExtension
        guard let path = _UnstringifiedBundle.path(forResource: resource, ofType: "bundle") else {
            return nil
        }
        return Bundle(path: path)
    }
}

public enum Text: String, Unstringified {
    public typealias StringType = String
    case form_title, form_name_field
}

public enum Format: Unstringified {
    public typealias StringType = String
    case form_name_field_max_length(Int)
}

public enum RichText: String, Unstringified {
    public typealias StringType = NSAttributedString
    case form_name_field_description
}

public enum RichFormat: Unstringified {
    public typealias StringType = NSAttributedString
    case 👻(Void)
}

Customization

You can customize localizableStringsTableName and localizableStringsBundle for each enum (Text, Format, RichText, RichFormat) by reimplementing them in an extension:

extension Text {
    public var localizableStringsTableName: String? {
        return "xxx"
    }
}

Template

You can also customize the generated code by using a custom template.

Your template must contain the following variables:

  • $KEYS_ARRAY
  • $FORMATED_KEYS_ARRAY
  • $RICH_KEYS_ARRAY
  • $FORMATED_RICH_KEYS_ARRAY

Examples

See Examples directory.

📖 License

Unstringify is released under the MIT license. See LICENSE for details.

Alternatives

GitHub

link
Stars: 3
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

4 years ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics