XCResourceλ xcassets 리μμ€ λ‘λ©κ³Ό λ€κ΅μ΄ μ§μμ λμμ£Όλ 컀맨λλΌμΈ ν΄μ λλ€.
μ΄λ₯Ό μ΄μ©ν΄ μ΄λ―Έμ§, 컬λ¬, λ€κ΅μ΄ λ¬Έμμ΄μ μ½κ² μμ±ν μ μμ΅λλ€:
let image = UIImage.named(.settings)
let color = UIColor.named(.coralPink)
let string = String.localized(.done)
let text = String.formatted(.alert_deleteFile(fileName: fileName))
xcresource
λ λ€μμ νμ 컀맨λλ₯Ό κ°μ§κ³ μμ΅λλ€:
xcassets2swift
: xcassetsμ μν Swift μ½λλ₯Ό μμ±ν©λλ€.fonts2swift
: fontλ₯Ό μν Swift μ½λλ₯Ό μμ±ν©λλ€.strings2swift
: strings νμΌλ‘ Swift enumμ μμ±ν©λλ€.swift2strings
: Swift enumμΌλ‘ strings νμΌμ μμ±ν©λλ€.key2form
: Swift enumμΌλ‘ format string μ½λλ₯Ό μμ±ν©λλ€.strings2csv
: strings νμΌλ‘ CSV νμΌμ μμ±ν©λλ€.csv2strings
: CSV νμΌλ‘ strings νμΌμ μμ±ν©λλ€.init
: Manifest νμΌμ μμ±ν©λλ€.run (default)
: Manifest νμΌμ λμ΄λ νμ 컀맨λλ€μ μ€νν©λλ€.mint install nearfri/XCResource
Make
git clone https://github.com/nearfri/XCResource.git
cd XCResource
make build install
https://user-images.githubusercontent.com/323940/202911680-3bb7bed7-ccaf-40c2-b136-439ff05b983b.mov
xcresource xcassets2swift
λ₯Ό μ€νν©λλ€:
xcrun --sdk macosx mint run xcresource xcassets2swift \
--xcassets-path ../SampleApp/Assets.xcassets \
--asset-type imageset \
--swift-path ../SampleApp/ResourceKeys/ImageKey.swift \
--key-type-name ImageKey
μλμ κ°μ μ½λκ° μμ±λ©λλ€:
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)
https://user-images.githubusercontent.com/323940/209695591-92fd68f8-141e-4766-ab6d-bcfdd072ac74.mov
xcresource fonts2swift
λ₯Ό μ€νν©λλ€:
xcrun --sdk macosx mint run xcresource fonts2swift \
--fonts-path ../SampleApp/Fonts \
--swift-path ../SampleApp/ResourceKeys/FontKey.swift \
--key-type-name FontKey
μλμ κ°μ μ½λκ° μμ±λ©λλ€:
public struct FontKey: Hashable {
public var fontName: String
public var familyName: String
public var style: String
public var path: String
public init(fontName: String, familyName: String, style: String, path: String) {
self.fontName = fontName
self.familyName = familyName
self.style = style
self.path = path
}
}
public extension FontKey {
static let allKeys: [FontKey] = [
// Open Sans
.openSans_bold,
.openSans_regular,
]
}
public extension FontKey {
// MARK: Open Sans
static let openSans_bold: FontKey = .init(
fontName: "OpenSans-Bold",
familyName: "Open Sans",
style: "Bold",
path: "OpenSans/OpenSans-Bold.ttf")
...
UIFont
μ μμ±μλ₯Ό μΆκ°ν΄μ€λλ€:
extension UIFont {
static func named(_ key: FontKey, size: CGFloat) -> UIFont {
return UIFont(name: key.fontName, size: size)!
}
}
μ΄μ μλμμ±κ³Ό ν¨κ» ν°νΈλ₯Ό μμ±ν μ μμ΅λλ€:
label.font = .named(.openSans_bold)
https://user-images.githubusercontent.com/323940/202911792-bc48ef57-0ff3-404b-84b4-94931350e847.mov
enum
νμ
μ λΉ StringKey
λ₯Ό λ§λ€μ΄μ€λλ€:
enum StringKey: String, CaseIterable {
}
String
μ μμ±μλ₯Ό μΆκ°ν©λλ€:
extension String {
static func localized(_ key: StringKey) -> String {
return NSLocalizedString(key.rawValue, bundle: .module, comment: "")
}
}
strings νμΌμ μ€λΉν©λλ€:
"cancel" = "μ·¨μ";
"confirm" = "νμΈ";
xcresource strings2swift
λ₯Ό μ€νν©λλ€:
xcrun --sdk macosx mint run xcresource strings2swift \
--resources-path ../SampleApp \
--language ko \
--swift-path ../SampleApp/ResourceKeys/StringKey.swift \
μλμ κ°μ΄ StringKey
κ° μ
λ°μ΄νΈ λ©λλ€:
enum StringKey: String, CaseIterable {
/// μ·¨μ
case cancel
/// νμΈ
case confirm
}
μ΄μ μλμμ±κ³Ό ν¨κ» μ§μνλ λ¬Έμμ΄μ μμ±ν μ μμ΅λλ€:
label.text = .localized(.cancel)
https://user-images.githubusercontent.com/323940/202911866-cbd49782-05c8-4908-8e34-5187ad867331.mov
swift2strings
λ strings2swift
μλ λ°λλ‘ enum
μ stringsλ‘ λ³νν΄μ€λλ€.
xcrun --sdk macosx mint run xcresource swift2strings \
--swift-path ../SampleApp/ResourceKeys/StringKey.swift \
--resources-path ../SampleApp \
--language-config ko:comment
μλμ κ°μ΄ strings νμΌμ΄ μ λ°μ΄νΈ λ©λλ€:
/* μ·¨μ */
"cancel" = "μ·¨μ";
/* νμΈ */
"confirm" = "νμΈ";
https://user-images.githubusercontent.com/323940/202911913-b9603b3b-cac7-40c2-8573-75e7617edd9c.mov
StringKey
μ case
μ format string νμμ μ£Όμμ μΆκ°ν©λλ€:
enum StringKey: String, CaseIterable {
/// "%{fileName}" νμΌμ μμ λ©λλ€.\nμ΄ λμμ μ·¨μν μ μμ΅λλ€.
case alert_deleteFile
}
xcresource key2form
μ μ€νν©λλ€:
xcrun --sdk macosx mint run xcresource key2form \
--key-file-path ../SampleApp/ResourceKeys/StringKey.swift \
--form-file-path ../SampleApp/ResourceKeys/StringForm.swift \
--form-type-name StringForm \
--issue-reporter xcode
μλμ κ°μ μ½λκ° μμ±λ©λλ€:
struct StringForm {
var key: String
var arguments: [CVarArg]
}
extension StringForm {
/// "%{fileName}" νμΌμ μμ λ©λλ€.\nμ΄ λμμ μ·¨μν μ μμ΅λλ€.
static func alert_deleteFile(fileName: String) -> StringForm {
return StringForm(key: StringKey.alert_deleteFile.rawValue, arguments: [fileName])
}
}
String
μ μμ±μλ₯Ό μΆκ°ν΄μ€λλ€:
extension String {
static func formatted(_ form: StringForm) -> String {
let format = NSLocalizedString(form.key, bundle: .module, comment: "")
return String(format: format, locale: .current, arguments: form.arguments)
}
}
μ΄μ μλμμ±κ³Ό ν¨κ» μ§μνλ λ¬Έμμ΄μ μμ±ν μ μμ΅λλ€:
label.text = .formatted(.alert_deleteFile(fileName: fileName))
https://user-images.githubusercontent.com/323940/202911933-e1041967-9fd1-4eb5-9c73-999cdbbb6a13.mov
xcresource strings2csv
λ₯Ό μ€νν©λλ€:
mint run xcresource strings2csv \
--resources-path ../SampleApp \
--development-language ko \
--csv-path ./localizations.csv \
--header-style long-ko \
--write-bom
μλμ κ°μ csv νμΌμ΄ λ§λ€μ΄μ§λλ€:
Key | Comment | νκ΅μ΄ (ko) | μμ΄ (en) |
---|---|---|---|
cancel | μ·¨μ | μ·¨μ | |
confirm | νμΈ | νμΈ |
https://user-images.githubusercontent.com/323940/202911964-00ebcb96-90d8-430d-8385-e0cecbe8b181.mov
xcresource csv2strings
λ₯Ό μ€νν©λλ€:
mint run xcresource csv2strings \
--csv-path ./localizations.csv \
--header-style long-ko \
--resources-path ../SampleApp
μλμ κ°μ΄ strings νμΌμ΄ λ§λ€μ΄μ§λλ€:
/* μ·¨μ */
"cancel" = "μ·¨μ";
/* νμΈ */
"confirm" = "νμΈ";
XCResourceSample.xcworkspace
μμ μ μ© μμ λ₯Ό λ³Ό μ μμ΅λλ€.
XCResourceλ MIT λΌμ΄μ μ€μ λ°λΌ λ°°ν¬λ©λλ€. μμΈν λ΄μ©μ LICENSEλ₯Ό μ°Έμ‘°νμμμ€.
link |
Stars: 6 |
Last commit: 7 weeks ago |
Full Changelog: https://github.com/nearfri/XCResource/compare/0.9.24...0.9.25
8294ff8872584a3c41ef991fb24193004774013a9b41a421433f6c28ee5fc92a
b4a297dea6b6c8df93dc7149d7d548e38ec699cdcfd2477b33c013da52fd7249
.binaryTarget(
name: "xcresource",
url: "https://github.com/nearfri/XCResource/releases/download/0.9.25/xcresource.artifactbundle.zip",
checksum: "b4a297dea6b6c8df93dc7149d7d548e38ec699cdcfd2477b33c013da52fd7249"
)
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics