在更改为第三种语言(SWIFT)时不加载本地html文件

时间:2018-08-20 12:15:34

标签: ios swift xcode9

我在xcode项目的 lproj 文件夹中有3种以3种不同语言保存的本地html文件。在每个Localize.strings文件中,每个html文件名都有键:1)“ history_file” =“ history_en”; 2)“ history_file” =“ history_kz”; 3)“ history_file” =“ history_ru”;也有AboutCompanyViewController带有3个tableViewCells的表视图。在选择单元格时,在AboutCompanyDetailVC中应打开html文件。

当我更改语言时,只能使用两种语言。我的意思是,如果我将应用程序语言从英语更改为哈萨克语,然后再向后更改,则一切正常,但是如果我将语言更改为俄语,则html文件不会打开。更改语言无关紧要。如果仅会更改两种语言,它将打开html文件;在更改为第三种语言时,它将打印(“文件读取错误”)。有什么问题的想法吗?

AboutCompanyViewController中:

import UIKit
import InteractiveSideMenu

class AboutCompanyViewController: UIViewController, SideMenuItemContent, Storyboardable {

@IBOutlet weak var tableView: UITableView!

var cellImages = ["about_company-4.png", "about_company-2.png", "about_company-3.png"]
var cellLabels = ["history".localized, "production".localized, "partners".localized]
var webPages = ["history_file", "production_file", "partners_file"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "ABOUT COMPANY".localized
 }


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func openMenu(_ sender: UIButton) {
    showSideMenu()
}

}
 extension AboutCompanyViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return min(cellImages.count, cellLabels.count)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AboutCompanyTableViewCell
    cell.label.text = cellLabels[indexPath.row]
    cell.cellImage?.image = UIImage(named: cellImages[indexPath.row])
    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "goTo" {
        let view = segue.destination as! AboutCompanyDetailVC
        let indexPath = self.tableView.indexPathForSelectedRow!
        view.selected = self.webPages[indexPath.row]
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    let height: CGFloat = 100.0
    return height
}

}

关于CompanyDetailVC:

import UIKit
import WebKit

 class AboutCompanyDetailVC: UIViewController,  UIWebViewDelegate {

@IBOutlet weak var webView: WKWebView!

var selected: String!

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "ABOUT COMPANY".localized
    loadHtmlFile()

}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

 func loadHtmlFile() {
    let preferredLanguage = NSLocale.preferredLanguages[0]

    print("\(selected.localized) \(preferredLanguage)")
    if let url = Bundle.main.url(forResource: selected!.localized, withExtension: "html") {
        let request = URLRequest.init(url: url)
        webView.load(request)
    } else {
        print ("File reading error")
    }
   }
}

0 个答案:

没有答案