Skip to content

Instantly share code, notes, and snippets.

@jeremiegirault
Last active May 24, 2017 13:44
Show Gist options
  • Save jeremiegirault/62134393e3765199929b0e99219a4500 to your computer and use it in GitHub Desktop.
Save jeremiegirault/62134393e3765199929b0e99219a4500 to your computer and use it in GitHub Desktop.
My custom swiftgen templates for strings and fonts
// Generated using SwiftGen, template by @jeremiegirault — https://github.com/SwiftGen/SwiftGen
#if os(iOS) || os(tvOS) || os(watchOS)
import UIKit.UIFont
typealias Font = UIFont
#elseif os(OSX)
import AppKit.NSFont
typealias Font = NSFont
#endif
struct FontFamily {
private let family: String
private let name: String
init(family: String, name: String) {
self.family = family
self.name = name
registerIfNeeded()
}
//
// MARK: Font Registration
//
private final class BundleToken {}
private func registerIfNeeded() {
#if os(iOS) || os(tvOS) || os(watchOS)
let fontNames = UIFont.fontNames(forFamilyName: family)
#elseif os(OSX)
let availableFonts = NSFontManager.shared().availableMembers(ofFontFamily: family)
let fontNames = availableFonts.flatMap { $0.first as? String } ?? []
#endif
if !fontNames.contains(name) {
register()
}
}
private func register() {
let extensions = ["otf", "ttf"]
let bundle = Bundle(for: BundleToken.self)
func registrationError(_ message: String) -> Never {
fatalError("Unable to register font named \(name): \(message)")
}
let fileUrl = extensions.flatMap { bundle.url(forResource: name, withExtension: $0) }.first
guard let url = fileUrl else { registrationError("Could not find font file url") }
var error: Unmanaged<CFError>?
let result = CTFontManagerRegisterFontsForURL(url as CFURL, .none, &error)
if !result {
let bridgedError = error!.takeUnretainedValue() as Error
registrationError(bridgedError.localizedDescription)
}
}
//
// MARK: Operations
//
func font(size: CGFloat) -> UIFont {
return UIFont(name: name, size: size)!
}
}
// swiftlint:disable file_length
// swiftlint:disable line_length
extension FontFamily {
{% for family in families %}
enum {{family.name|swiftIdentifier|snakeToCamelCaseNoPrefix|escapeReservedKeywords}} {
{% for font in family.fonts %}
static let {{font.style|swiftIdentifier|snakeToCamelCaseNoPrefix|lowerFirstWord|escapeReservedKeywords}} = FontFamily(family: "{{family.name|swiftIdentifier|snakeToCamelCaseNoPrefix|escapeReservedKeywords}}", name: "{{font.name}}")
{% endfor %}
}
{% endfor %}
}
// Generated using SwiftGen, template by @jeremiegirault — https://github.com/SwiftGen/SwiftGen
{% if tables.first.levels %}
import Foundation
{% set typeName %}{{param.typeName|default:"Translation"}}{% endset %}
struct {{typeName}}: RawRepresentable, CustomStringConvertible {
//
// MARK: RawRepresentable
//
let rawValue: String
init(rawValue: String) { self.rawValue = rawValue }
//
// MARK: Localization Helpers
//
private final class BundleToken {}
init(key: String) {
self.rawValue = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "")
}
init(key: String, _ args: CVarArg...) {
let format = NSLocalizedString(key, bundle: Bundle(for: BundleToken.self), comment: "")
self.rawValue = String(format: format, locale: Locale.current, arguments: args)
}
//
// MARK: CustomStringConvertible
//
var description: String { return rawValue }
}
//
// MARK: Translations
//
{% macro parametersBlock types %}{% for type in types %}_ p{{forloop.counter}}: {{type}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %}
{% macro argumentsBlock types %}{% for type in types %}p{{forloop.counter}}{% if not forloop.last %}, {% endif %}{% endfor %}{% endmacro %}
{% macro recursiveBlock item %}
{% for string in item.strings %}
/// {{string.translation}}
{% if string.types %}
static func {{string.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}}({% call parametersBlock string.types %}) -> {{typeName}} {
return {{typeName}}(key: "{{string.key}}", {% call argumentsBlock string.types %})
}
{% else %}
static let {{string.name|swiftIdentifier|snakeToCamelCase|lowerFirstWord|escapeReservedKeywords}} = {{typeName}}(key: "{{string.key}}")
{% endif %}
{% endfor %}
{% for child in item.children %}
{% call recursiveBlock child %}
{% endfor %}
{% endmacro %}
// swiftlint:disable file_length
// swiftlint:disable line_length
extension Translation {
{% call recursiveBlock tables.first.levels %}
}
//
// MARK: Translation autocomplete helper
//
func tr(_ translation: Translation) -> String {
return translation.rawValue
}
{% else %}
// No string found
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment