NSLocalizedString not defaulting to Base language

后端 未结 4 2128
情话喂你
情话喂你 2021-02-20 09:20

I have the following problem with a small iOS 7 project on which I\'m testing the localisation capabilities.

  • I have a default project, with one VC, in which I have
4条回答
  •  梦毁少年i
    2021-02-20 09:41

    Using Dirk's answer here is the Swift equivalent implemented as a computed property in a String extension:

    extension String {
    
    var localized: String {
    
        var localizedString = NSLocalizedString(self, comment: "")
    
        // If a localized string was found then return it.
        // This check is based on the fact that if no localized string
        // exists then NSLocalized() returns the key itself.
        if self != localizedString {
            return localizedString
        }
    
        // No localized string exists.  Retrieve the display string
        // from the base strings file.
        var bundleForString: Bundle
        if let path = Bundle.main.path(forResource: "Base", ofType: "lproj"),
            let bundle = Bundle(path: path) {
            bundleForString = bundle
        } else {
            bundleForString = Bundle.main
        }
    
        localizedString = bundleForString.localizedString(forKey: self, value: self, table: nil)
    
        return localizedString
    }
    
    } 
    

提交回复
热议问题