如何在不更改设备语言的情况下更改 iOS 应用程序 运行 时间的语言(比如说用按钮)
How to change language of iOS app run time( let say with a button) without changing device language
我已经尝试了互联网上所有可用的方法,但我仍然遗漏了一些东西
两个 .strings 文件中的键值也相同,添加了西班牙语,本地化了我的 LocalizeAble 文件
(我不想为模拟器调整模式)
任何回复都将是 appreciated.Thanks
我的ViewController
class ViewController: UIViewController
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
override func viewDidLoad() {super.viewDidLoad() }
@IBAction func selectLanguageBtn(_ sender: UIButton) {
if sender.tag == 1 {
//sended button tag 1 for selecting English
firstNameLabel.text = "FirstLabel".localizeableString(loc: "en")
} else if sender.tag == 2 {
//sended button tag 2 for selecting Spanish
firstNameLabel.text = "FirstLabel".localizeableString(loc: "es")
}}}
extension String {
func localizeableString(loc:String) -> String {
let path = Bundle.main.path(forResource: loc, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
} [enter image description here][1]
[1]: https://i.stack.imgur.com/mrS0s.png
当您可以直接从包中检索字符串时,为什么还要使用 NSLocalizedString
?
还要确保文件在 en.lproj
和 es.lproj
以及您支持的任何其他语言中都被命名为 Localizable.strings
。
如果文件名为Test.strings
,则需要为localizedString
指定table: "Test"
参数。
extension String {
public static func localized(_ key: String, language: String? = nil) -> String {
let language = language ?? Locale.preferredLanguages.first?.components(separatedBy: "-").first ?? "en"
guard let path = Bundle.main.path(forResource: language, ofType: "lproj"), let bundle = Bundle(path: path) else {
return Bundle.main.localizedString(forKey: key, value: nil, table: nil)
}
return bundle.localizedString(forKey: key, value: nil, table: nil)
}
}
用法:
firstNameLabel.text = .localized("FirstName") //default
firstNameLabel.text = .localized("FirstName", language: "es") //specified
我找到了这个,它类似于 Brandon 的答案,但更方便。
你必须遵循相同的步骤,在分配字符串时只写 .localized()
,它将得到字符串作为键
快乐编码:)
let LANGUAGE = "es" // Choose your language
extension String {
func localized() -> String{
let path = Bundle.main.path(forResource: LANGUAGE, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
}
我已经尝试了互联网上所有可用的方法,但我仍然遗漏了一些东西 两个 .strings 文件中的键值也相同,添加了西班牙语,本地化了我的 LocalizeAble 文件 (我不想为模拟器调整模式) 任何回复都将是 appreciated.Thanks
我的ViewController
class ViewController: UIViewController
@IBOutlet weak var firstNameLabel: UILabel!
@IBOutlet weak var lastNameLabel: UILabel!
override func viewDidLoad() {super.viewDidLoad() }
@IBAction func selectLanguageBtn(_ sender: UIButton) {
if sender.tag == 1 {
//sended button tag 1 for selecting English
firstNameLabel.text = "FirstLabel".localizeableString(loc: "en")
} else if sender.tag == 2 {
//sended button tag 2 for selecting Spanish
firstNameLabel.text = "FirstLabel".localizeableString(loc: "es")
}}}
extension String {
func localizeableString(loc:String) -> String {
let path = Bundle.main.path(forResource: loc, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
} [enter image description here][1]
[1]: https://i.stack.imgur.com/mrS0s.png
当您可以直接从包中检索字符串时,为什么还要使用 NSLocalizedString
?
还要确保文件在 en.lproj
和 es.lproj
以及您支持的任何其他语言中都被命名为 Localizable.strings
。
如果文件名为Test.strings
,则需要为localizedString
指定table: "Test"
参数。
extension String {
public static func localized(_ key: String, language: String? = nil) -> String {
let language = language ?? Locale.preferredLanguages.first?.components(separatedBy: "-").first ?? "en"
guard let path = Bundle.main.path(forResource: language, ofType: "lproj"), let bundle = Bundle(path: path) else {
return Bundle.main.localizedString(forKey: key, value: nil, table: nil)
}
return bundle.localizedString(forKey: key, value: nil, table: nil)
}
}
用法:
firstNameLabel.text = .localized("FirstName") //default
firstNameLabel.text = .localized("FirstName", language: "es") //specified
我找到了这个,它类似于 Brandon 的答案,但更方便。
你必须遵循相同的步骤,在分配字符串时只写 .localized()
,它将得到字符串作为键
快乐编码:)
let LANGUAGE = "es" // Choose your language
extension String {
func localized() -> String{
let path = Bundle.main.path(forResource: LANGUAGE, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
}