最近這篇文章可火了。33個2017年你必須關注的框架
所以嘗鮮使用了一下 ** PermissionScope **這個框架胞得,好用是挺好用的肩榕,但是有一些地方沒有加上本地化。比如打開某個權限后權限按鈕會變成 下圖這樣子陆盘。這中英文的搭配肯定是不行的啦,偽裝高大上败明。但是看了下代碼和說明隘马,我們只能自定義部分 Label 的文字樣式等等。反正就是無法修改權限按鈕的文本妻顶。但是顏色酸员、字體什么的是可以的。
那么怎么修改權限按鈕呢讳嘱。在代碼里搜索一下 ALLOWED 幔嗦,定位到 PermissionScope.swift 的 viewWillLayoutSubviews 方法,里面有一句
button.setTitle("Allowed \(prettyDescription)".localized.uppercased(), for: .normal)
可以看到肯定是這行代碼來給權限按鈕賦值呢燥,然后我們點擊 localized 看看是什么情況崭添,會發(fā)現(xiàn)這是個 String 的 Extension
extension String {
/// NSLocalizedString shorthand
var localized: String {
return NSLocalizedString(self, comment: "")
}
}
那答案就呼之欲出了,自然是 本地化 啦叛氨。
本地化的步驟和以前我們做的基本上是一樣的呼渣。
1、新建一個 Strings File寞埠,并命名為 Localizable.strings屁置。
2、點擊該文件仁连,在 Xcode 的右側的 File inspection 中點擊 Localize蓝角,這里我們先只點開 Chinese
3阱穗、后面就是賦值了,此處我直接附上所有會用到的 key使鹅,此處參考了 https://github.com/nickoneill/PermissionScope/pull/12#issuecomment-96428580returnpermissionScope
"Allow Contacts" = "";
"Allow Events" = "";
"Enable Location" = "";
"Allow Notifications" = "";
"Allow Microphone" = "";
"Allow Camera" = "";
"Allow Photos" = "";
"Allow Reminders" = "";
"Allow Bluetooth" = "";
"Allow Motion" = "";
//
"Contacts" = "";
"Events" = "";
"LocationAlways" = "";
"LocationInUse" = "";
"Notifications" = "";
"Microphone" = "";
"Camera" = "";
"Photos" = "";
"Reminders" = "";
"Bluetooth" = "";
"Motion" = "";
//
"Allowed Contacts" = "";
"Allowed Events" = "";
"Allowed Location" = "";
"Allowed Notifications" = "";
"Allowed Microphone" = "";
"Allowed Camera" = "";
"Allowed Photos" = "";
"Allowed Reminders" = "";
"Allowed Bluetooth" = "";
"Allowed Motion" = "";
//
"Denied Contacts" = "";
"Denied Events" = "";
"Denied Location" = "";
"Denied Notifications" = "";
"Denied Microphone" = "";
"Denied Camera" = "";
"Denied Photos" = "";
"Denied Reminders" = "";
"Denied Bluetooth" = "";
"Denied Motion" = "";
//
"Contacts Disabled" = "";
"Events Disabled" = "";
"Location Disabled" = "";
"Notifications Disabled" = "";
"Microphone Disabled" = "";
"Camera Disabled" = "";
"Photos Disabled" = "";
"Reminders Disabled" = "";
"Bluetooth Disabled" = "";
"Motion Disabled" = "";
//
"Close" = "";
"Hey, listen!" = "";
"We need a couple things\r\nbefore you get started." = "";
"Permission for Contacts was denied." = "";
"Permission for Events was denied." = "";
"Permission for LocationAlways was denied." = "";
"Permission for LocationInUse was denied." = "";
"Permission for Notifications was denied." = "";
"Permission for Microphone was denied." = "";
"Permission for Camera was denied." = "";
"Permission for Photos was denied." = "";
"Permission for Reminders was denied." = "";
"Permission for Bluetooth was denied." = "";
"Permission for Motion was denied." = "";
//
"Please enable access to Contacts in the Settings app" = "";
"Please enable access to Events in the Settings app" = "";
"Please enable access to LocationAlways in the Settings app" = "";
"Please enable access to LocationInUse in the Settings app" = "";
"Please enable access to Notifications in the Settings app" = "";
"Please enable access to Microphone in the Settings app" = "";
"Please enable access to Camera in the Settings app" = "";
"Please enable access to Photos in the Settings app" = "";
"Please enable access to Reminders in the Settings app" = "";
"Please enable access to Bluetooth in the Settings app" = "";
"Please enable access to Motion in the Settings app" = "";
//
"OK" = "";
"Show me" = "";
//
"Contacts is currently disabled." = "";
"Events is currently disabled." = "";
"LocationAlways is currently disabled." = "";
"LocationInUse is currently disabled." = "";
"Notifications is currently disabled." = "";
"Microphone is currently disabled." = "";
"Camera is currently disabled." = "";
"Photos is currently disabled." = "";
"Reminders is currently disabled." = "";
"Bluetooth is currently disabled." = "";
"Motion is currently disabled." = "";
//
"Please enable access to Contacts in Settings" = "";
"Please enable access to Events in Settings" = "";
"Please enable access to LocationAlways in Settings" = "";
"Please enable access to LocationInUse in Settings" = "";
"Please enable access to Notifications in Settings" = "";
"Please enable access to Microphone in Settings" = "";
"Please enable access to Camera in Settings" = "";
"Please enable access to Photos in Settings" = "";
"Please enable access to Reminders in Settings" = "";
"Please enable access to Bluetooth in Settings" = "";
"Please enable access to Motion in Settings" = "";
好了揪阶,賦值吧。
我們再看看下圖患朱。鲁僚。。其中那個『注意了裁厅、我們需要做一些事情冰沙、我們會使用這個權限』這些字符串是可以在其他的地方進行直接賦值的,這里就不展開了执虹。