Package 本地化
发布
更新
字数
134
阅读
1 分钟
阅读量
1034
首先在 package.swift
里增加对应的参数 defaultLocalizaiton
,并为 targets
设置 resources
let package = Package(
// ...,
defaultLocalization: "en",
// ...,
targets: [
.target(
name: "YOUR_TARGET_NAME",
// ...,
resources: [.process("Resources")]
),
]
)
然后在 Sources
目录下对应的文件夹增加一个 Resources
文件夹用来放置 Localizable.strings
文件,目录结构类似:
- YOUR_PACKAGE_FOLDER
Package.swift
- Sources
- YOUR_TARGET_NAME
- Resources
- en.lproj
Localizable.strings
...
在 package 使用 NSLocalizedString
并设置 bundle
为 .moudle
即可:
let title = NSLocalizedString("TITLE", bundle: .moudle, comment:"My package title")
参考
- https://stackoverflow.com/questions/57656798/add-localizable-strings-to-swift-package
- WWDC 2019: Adopting Swift Packages in Xcode https://developer.apple.com/videos/play/wwdc2019/408/
- WWDC 2019: Creating Swift Packages https://developer.apple.com/videos/play/wwdc2019/410/
- https://docs.swift.org/package-manager/PackageDescription/PackageDescription.html