【发布时间】:2020-05-30 06:44:22
【问题描述】:
我有多种语言的 Localizable.strings 文件。
我想在应用程序中逐行阅读当前的 Localizable.strings 以提供翻译帮助。
let bpath:String = Bundle.main.path(forResource: "es", ofType: "lproj")! as String
if let filepath = Bundle.main.path(forResource: bpath + "/Localizable.strings", ofType: nil) {
do {
let contents = try String(contentsOfFile: filepath)
print(contents)
} catch {
// contents could not be loaded
let alert = UIAlertController(title: "Erreur".localized, message: "Fichier \(bpath + "/Localizable.strings") non accessible".localized, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Ok".localized, style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
// example.txt not found!
let alert = UIAlertController(title: "Erreur".localized, message: "Fichier \(bpath + "/Localizable.strings") non trouvé".localized, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Ok".localized, style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
找不到文件。
PS:这段代码完美运行,但目标是读取所有“键”。
let bpath:String = Bundle.main.path(forResource: "es", ofType: "lproj")! as String
let bundle = Bundle(path: bpath as String)
let thisWord="Erreur"
let ourWord=NSLocalizedString(thisWord, bundle: bundle!, comment: "")
【问题讨论】:
-
使用菜单编辑器 > 导出本地化...
-
谢谢,但这不是目标。我想从应用程序访问 localizable.strings 文件。
标签: swift file-io localizable.strings