美文网首页iOS Developer
PermissionScope 本地化的问题

PermissionScope 本地化的问题

作者: wzbdroid | 来源:发表于2017-02-08 15:25 被阅读581次

    最近这篇文章可火了。33个2017年你必须关注的框架

    所以尝鲜使用了一下 ** PermissionScope **这个框架,好用是挺好用的,但是有一些地方没有加上本地化。比如打开某个权限后权限按钮会变成 下图这样子。这中英文的搭配肯定是不行的啦,伪装高大上。但是看了下代码和说明,我们只能自定义部分 Label 的文字样式等等。反正就是无法修改权限按钮的文本。但是颜色、字体什么的是可以的。

    PermissionScope 本地化的问题

    那么怎么修改权限按钮呢。在代码里搜索一下 ALLOWED ,定位到 PermissionScope.swift 的 viewWillLayoutSubviews 方法,里面有一句

    button.setTitle("Allowed \(prettyDescription)".localized.uppercased(), for: .normal)
    

    可以看到肯定是这行代码来给权限按钮赋值,然后我们点击 localized 看看是什么情况,会发现这是个 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" = "";
    

    好了,赋值吧。
    我们再看看下图。。。其中那个『注意了、我们需要做一些事情、我们会使用这个权限』这些字符串是可以在其他的地方进行直接赋值的,这里就不展开了。


    PermissionScope 本地化的问题

    相关文章

      网友评论

        本文标题:PermissionScope 本地化的问题

        本文链接:https://www.haomeiwen.com/subject/thovittx.html