unstringify(CLI): Code generator for strong-typing localizable strings.Unstringified(module): Strong-typed localizable strings static source code.
It makes your localized strings code:
- compile time checked,
- strong typed,
- autocompletable by Xcode.
There are several methods to install Unstringify:
- Via CocoaPods
- Via Swift Package Manager
- Others:
- Download the ZIP for the latest release
- Via Mint (system-wide installation)
- Compile from source (only recommended if you need features from master or want to test a PR)
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"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.swiftIn 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.
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)
}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"
}
}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
See Examples directory.
Unstringify is released under the MIT license. See LICENSE for details.