XCResource는 xcassets 리소스 로딩과 다국어 지원을 도와주는 커맨드라인 툴입니다.
이를 이용해 이미지, 컬러, 다국어 문자열을 쉽게 생성할 수 있습니다:
let image = UIImage.named(.settings)
let color = UIColor.named(.coralPink)
let font = UIFont(.openSans_bold, size: 12)
let string = String(localized: .done)
let text = String(localized: .alert_delete_file(named: filename))
let data = Data(.model)
xcresource
는 다음의 하위 커맨드를 가지고 있습니다:
xcassets2swift
: xcassets을 위한 Swift 코드를 생성합니다.files2swift
: 파일을 리스팅하는 Swift 코드를 생성합니다.fonts2swift
: font를 리스팅하는 Swift 코드를 생성합니다.xcstrings2swift
: xcstrings 파일로LocalizedStringResource
코드를 생성합니다.strings2swift
: strings 파일로 Swift enum을 생성합니다.swift2strings
: Swift enum으로 strings 파일을 생성합니다.key2form
: Swift enum으로 format string 코드를 생성합니다.strings2csv
: strings 파일로 CSV 파일을 생성합니다.csv2strings
: CSV 파일로 strings 파일을 생성합니다.init
: Manifest 파일을 생성합니다.run (default)
: Manifest 파일에 나열된 하위 커맨드들을 실행합니다.
dependencies: [
.package(url: "https://github.com/nearfri/XCResource-plugin", from: "0.11.0"),
],
xcassets2swift.mp4
xcresource.json
에 아래와 같은 커맨드를 추가하고 RunXCResource
플러그인을 실행합니다:
{
"commands": [
{
"commandName": "xcassets2swift",
"xcassetsPaths": [
"Sources/Resource/Resources/Assets.xcassets"
],
"assetTypes": ["imageset"],
"swiftPath": "Sources/Resource/Keys/ImageKey.swift",
"keyTypeName": "ImageKey",
"accessLevel": "public"
}
]
}
아래와 같은 코드가 생성됩니다:
struct ImageKey: ExpressibleByStringLiteral, Hashable {
var rawValue: String
init(_ rawValue: String) {
self.rawValue = rawValue
}
init(stringLiteral value: String) {
self.rawValue = value
}
}
// MARK: - Assets.xcassets
extension ImageKey {
// MARK: Places
static let places_authArrow: ImageKey = "Places/authArrow"
static let places_authClose: ImageKey = "Places/authClose"
// MARK: Settings
static let settings: ImageKey = "settings"
static let settingsAppearance: ImageKey = "settingsAppearance"
...
UIImage
에 생성자를 추가해줍니다:
extension UIImage {
static func named(_ key: ImageKey) -> UIImage {
return UIImage(named: key.rawValue, in: .module, compatibleWith: nil)!
}
}
이제 자동완성과 함께 이미지를 생성할 수 있습니다:
imageView.image = .named(.settings)
fonts2swift.mp4
xcresource.json
에 아래와 같은 커맨드를 추가하고 RunXCResource
플러그인을 실행합니다:
{
"commands": [
{
"commandName": "fonts2swift",
"resourcesPath": "Sources/Resource/Resources",
"swiftPath": "Sources/Resource/Keys/FontResource.swift",
"keyTypeName": "FontResource",
"keyListName": "all",
"preservesRelativePath": true,
"bundle": "Bundle.module",
"accessLevel": "public"
}
]
}
아래와 같은 코드가 생성됩니다:
public struct FontResource: Hashable {
public var fontName: String
public var familyName: String
public var style: String
public var relativePath: String
public var bundle: Bundle
public init(
fontName: String,
familyName: String,
style: String,
relativePath: String,
bundle: Bundle
) {
self.fontName = fontName
self.familyName = familyName
self.style = style
self.relativePath = relativePath
self.bundle = bundle
}
public var url: URL {
return URL(filePath: relativePath, relativeTo: bundle.resourceURL).standardizedFileURL
}
public var path: String {
return url.path(percentEncoded: false)
}
}
public extension FontResource {
static let all: [FontResource] = [
// Open Sans
.openSans_bold,
.openSans_regular,
]
}
public extension FontResource {
// MARK: Open Sans
static let openSans_bold: FontResource = .init(
fontName: "OpenSans-Bold",
familyName: "Open Sans",
style: "Bold",
relativePath: "Fonts/OpenSans/OpenSans-Bold.ttf",
bundle: Bundle.module)
...
UIFont
에 생성자를 추가해줍니다:
extension UIFont {
convenience init(_ resource: FontResource, size: CGFloat) {
self.init(name: resource.fontName, size: size)!
}
}
이제 자동완성과 함께 폰트를 생성할 수 있습니다:
label.font = UIFont(.openSans_bold, size: 12)
xcstrings2swift.mp4
xcresource.json
에 아래와 같은 커맨드를 추가하고 RunXCResource
플러그인을 실행합니다:
{
"commands": [
{
"commandName": "xcstrings2swift",
"catalogPath": "Sources/Resource/Resources/Localizable.xcstrings",
"bundle": "at-url:Bundle.module.bundleURL",
"swiftPath": "Sources/Resource/Keys/LocalizedStringResource+.swift"
}
]
}
아래와 같은 코드가 생성됩니다:
public extension LocalizedStringResource {
/// \"\\(param1)\" will be deleted.\
/// This action cannot be undone.
static func alert_delete_file(_ param1: String) -> Self {
.init("alert_delete_file",
defaultValue: """
\"\(param1)\" will be deleted.
This action cannot be undone.
""",
bundle: .atURL(Bundle.module.bundleURL))
}
/// Done
static var common_done: Self {
.init("common_done",
defaultValue: "Done",
bundle: .atURL(Bundle.module.bundleURL))
}
}
함수 시그니처와 다국어 키가 동일하다면 함수명이나 파라미터명은 변경하더라도 계속 유지됩니다.
이제 자동완성과 함께 지역화된 문자열을 생성할 수 있습니다:
label.text = String(localized: .common_done)
XCResourceSample.xcworkspace
에서 적용 예제를 볼 수 있습니다.
XCResource는 MIT 라이선스에 따라 배포됩니다. 자세한 내용은 LICENSE를 참조하십시오.