Search code examples
iosswiftlocalization

lproj file not loaded yet


I have created 3 files with translations:

  • localizable.strings (English)
  • localizable.strings (Swedish)
  • localizable.strings (Finnish)

And I have this method to get string by key:

static func localized(key: String) -> String? {
        if let path = Bundle.main.path(forResource: currentLanguage, ofType: "lproj") {
            if let bundle = Bundle(path: path) {
                return NSLocalizedString(key, tableName: nil, bundle: bundle, value: "", comment: "")
            }
        }

        return nil;
    }

but I got "NSBundle < /var/containers/Bundle/Application/213B1469-9751-4459-ABED-80879B80EFBE/Dicken.app/en.lproj> (not yet loaded)"

Why isn't it loaded and where is the problem?


Solution

  • Made extension As this

    extension String {
    func localized(lang:String) -> String {
    
        let path = Bundle.main.path(forResource: lang, ofType: "lproj")
        let bundle = Bundle(path: path!)
    
        return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
    
    }
    
    let label: UILabel
    var language: String?
    label.text  = “Hello”.localized(lang:  self.language! )