我正在尝试为一家大公司的应用程序打开相机(因此该应用程序支持多种语言)。一切正常,但即使应用程序已完全本地化,相机屏幕上的按钮(“照片”,“视频”,“重拍”,“取消”)也未本地化。但是,在我转到iPhone设置并降低了对摄像头的访问权限之后,就会以正确的语言显示摄像头按钮。照片库也会发生同样的问题。导航栏上的“取消”按钮未本地化。我已经尝试了几乎在网上看到的所有内容,但是找不到蚂蚁的工作解决方案。
import Foundation
import Photos
class CameraPhotoAuthenticationManager {
static let shared = CameraPhotoAuthenticationManager()
func checkCameraAuthorization(viewController: UIViewController, picker: UIImagePickerController) {
let status = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
switch status {
case .authorized:
viewController.present(picker, animated: true, completion: nil)
case .notDetermined:
requestPermissionForCamera()
case .denied, .restricted:
self.showCameraNeedAccessMessage(viewController: viewController)
}
}
func checkPhotoAuthorization(viewController: UIViewController, picker: UIImagePickerController) {
if PHPhotoLibrary.authorizationStatus() == .authorized {
viewController.present(picker, animated: true, completion: nil)
} else {
PHPhotoLibrary.requestAuthorization {
status in
DispatchQueue.main.async {
if status == PHAuthorizationStatus.authorized {
viewController.present(picker, animated: true, completion: nil)
} else {
self.showNeedPhotoAccessMessage(viewController: viewController)
}
}
}
}
}
private func showNeedPhotoAccessMessage(viewController: UIViewController) {
let alertVC = UIAlertController(title: nil, message: "create.group.photo.auth".localized, preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "create.group.alert.close".localized, style: .cancel, handler: nil))
alertVC.addAction(UIAlertAction(title: "create.group.settings".localized, style: .default, handler: { (action: UIAlertAction) -> Void in
UIApplication.shared.openURL(URL(string: UIApplication.openSettingsURLString)!)
}))
viewController.present(alertVC, animated: true, completion: nil)
}
private func showCameraNeedAccessMessage(viewController: UIViewController) {
let alertVC = UIAlertController(title: nil, message: "create.group.camera.auth".localized, preferredStyle: .alert)
alertVC.addAction(UIAlertAction(title: "create.group.alert.close".localized, style: .cancel, handler: nil))
alertVC.addAction(UIAlertAction(title: "create.group.settings".localized, style: .default, handler: {
action in
UIApplication.shared.openURL(URL(string: UIApplication.openSettingsURLString)!)
}))
viewController.present(alertVC, animated: true, completion: nil)
}
private func requestPermissionForCamera(){
AVCaptureDevice.requestAccess(for: .video, completionHandler: {accessGranted in
guard accessGranted == true else { return }
})
}
}
上面您可以看到我的代码。有人遇到同样的问题吗?任何帮助都会很棒。谢谢!
编辑:我仍然找不到可靠的解决方案,但是如果您不以调试模式运行应用程序,它将可以正常工作。
答案 0 :(得分:0)
我认为“取消”按钮的本地化是iOS的一部分,并将自动翻译成用户的语言。您可以通过UIKit访问字符串。看一下这个问题:How to get localized Cancel, Done and etc?
“这是我创建的一个小宏,用于获取系统UIKit字符串: #define UIKitLocalizedString(key)[[NSBundle bundleWithIdentifier:@“ com.apple.UIKit”] localizedStringForKey:key值:@“”表:无]
Use it like this:
UIKitLocalizedString(@“ Search”); UIKitLocalizedString(@“ Done”); UIKitLocalizedString(@“ Cancel”); ...”(信用:Stephan Helinar)
(在移动设备上和匆忙中进行可怕格式化的道歉。)