如何在应用程序中读取 Localizable.strings
How to read Localizable.strings in the app
我有 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 :此代码完美运行,但目标是读取所有 "keys".
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: "")
使用以下路径查找路径:
let filepath = Bundle.main.path(forResource: "Localizable",
ofType: "strings", inDirectory: "es.lproj")
我有 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 :此代码完美运行,但目标是读取所有 "keys".
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: "")
使用以下路径查找路径:
let filepath = Bundle.main.path(forResource: "Localizable",
ofType: "strings", inDirectory: "es.lproj")