第一个 swizzling 捆绑包
private func swizzle(_ bundle: Bundle.Type) {
[(#selector(bundle.localizedString(forKey:value:table:)),
#selector(bundle.rd_localizedString(forKey:value:table:)))]
.forEach { original, swizzled in
guard let originalMethod = class_getInstanceMethod(bundle, original),
let swizzledMethod = class_getInstanceMethod(bundle, swizzled) else { return }
let didAddMethod = class_addMethod(
bundle,
original,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod)
)
if didAddMethod {
class_replaceMethod(
bundle,
swizzled,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod)
)
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
private var hasSwizzled = false
extension Bundle {
final public class func
doBadSwizzleStuff() {
guard !hasSwizzled else { return }
hasSwizzled = true
swizzle(self)
}
@objc internal func rd_localizedString(forKey: String, value: String?, table: String?) -> String {
let bundle = Bundle.main
return bundle.rd_localizedString(forKey: forKey, value: value, table: table)
}
}
其次为 UIImagePickerController 创建本地化字符串
下面是我见过的带有键的文件的内容(带有标准英文值),它们非常自我解释
CameraUI.strings
"PHOTO" = "PHOTO";
"AEAF_LOCK_TEXT" = "AE/AF LOCK";
"API_CANCEL_TITLE" = "Cancel";
"HDR_AUTO" = "Auto";
"HDR_ON" = "On";
"HDR_OFF" = "Off";
"TIMER_OFF_TEXT" = "Off";
"USE_PHOTO" = "Use Photo";
PhotoLibraryServices.strings
"PHOTOS" = "Photos";
"CAMERA_ROLL" = "Camera roll";
"ALL_SCREENSHOTS" = "Screenshots";
PhotoLibrary.strings
"CANCEL" = "Cancel";
"RETAKE" = "Retake";
"STREAM_SHARED_BY_ME_SUBTITLE" = "From You";
"STREAM_SHARED_BY_SUBTITLE" = "From %@";
"ALBUM_IMAGE_COUNT_FORMAT" = "%@ Photos";
"ALBUM_VIDEO_COUNT_FORMAT" = "%@ Videos";
"1_ALBUM_PHOTO" = "1 Photo";
"1_ALBUM_VIDEO" = "1 Video";
"ALBUM_TWO_TYPES_LABEL_COMMAS" = "%@, %@";
照片UI.strings
"ALL_PHOTOS_IN_LIBRARY" = "Moments";
"PXUserCollectionsSectionTitle" = "My Albums";
"FULL_PHOTOS_GRID_ZOOM_LEVEL_TITLE" = "Moments";
"NO_PHOTOS_OR_VIDEOS" = "No Photos or Videos";
第三次实现你的本地化类
internal enum L10n {
internal enum CameraUI {
/// AE/AF LOCK
internal static let aeafLockText = L10n.tr("CameraUI", "AEAF_LOCK_TEXT")
/// Cancel
internal static let apiCancelTitle = L10n.tr("CameraUI", "API_CANCEL_TITLE")
/// Auto
internal static let hdrAuto = L10n.tr("CameraUI", "HDR_AUTO")
/// Off
internal static let hdrOff = L10n.tr("CameraUI", "HDR_OFF")
/// On
internal static let hdrOn = L10n.tr("CameraUI", "HDR_ON")
/// PHOTO
internal static let photo = L10n.tr("CameraUI", "PHOTO")
/// Off
internal static let timerOffText = L10n.tr("CameraUI", "TIMER_OFF_TEXT")
/// Use test
internal static let usePhoto = L10n.tr("CameraUI", "USE_PHOTO")
}
// MARK: - Implementation Details
extension L10n {
private static func tr(_ table: String, _ key: String, _ args: CVarArg...) -> String {
// swiftlint:disable:next nslocalizedstring_key
let format = NSLocalizedString(key, tableName: table, bundle: Bundle(for: BundleToken.self), comment: "")
return String(format: format, locale: Locale.current, arguments: args)
}
}
private final class BundleToken {}