我有一个应用,我想支持2种语言。英文和希腊文。
我已经本地化了整个应用程序,除了UI之外,其他所有东西都可以正常运行。
我想根据用户使用的系统语言来更改图像。它在模拟器上可以正常工作,但是当我使用真实的设备时,应用会更改其语言(图片除外)的语言。

这是我的更改代码。

let language = NSLocale.current.identifier

override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
}

func setupUI() {

        if language == "el_US" {
            print(language)
            scanBtn.setBackgroundImage(UIImage(named: "scanBtnGr"), for: .normal)
            detoxBtn.setBackgroundImage(UIImage(named: "detoxGr"), for: .normal)
            bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
            searchBtn.setBackgroundImage(UIImage(named: "browseGr"), for: .normal)
            captureBtn.setBackgroundImage(UIImage(named: "scanGr"), for: .normal)
            uploadBtn.setBackgroundImage(UIImage(named: "uploadGr"), for: .normal)
        }else{
            print(language)
            scanBtn.setBackgroundImage(UIImage(named: "scanBtn"), for: .normal)
            detoxBtn.setBackgroundImage(UIImage(named: "detoxBtn"), for: .normal)
            bioBtn.setBackgroundImage(UIImage(named: "bioBtn"), for: .normal)
            searchBtn.setBackgroundImage(UIImage(named: "browseBtn"), for: .normal)
            captureBtn.setBackgroundImage(UIImage(named: "scan"), for: .normal)
            uploadBtn.setBackgroundImage(UIImage(named: "upload"), for: .normal)
        }
    }

我真的不知道问题出在哪里,因为在Simulator上运行完美。

最佳答案

如下更改设置这些图像的方式会有所帮助。

scanBtn.setBackgroundImage(UIImage(named: NSLocalizedString(key: "scanBtnGr", comment: ""), for: .normal)

这样,您也不必担心目标语言代码。您可以删除if语句并在Localizable.strings文件中定义相应的名称。

此外,如果您在不终止应用程序和不更改设备语言的情况下更改应用程序的语言,请确保在切换语言后再次调用setupUI()函数。 viewWillAppear可能是适合的地方。

关于ios - 根据系统语言更改用户界面-Swift,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57690758/

10-14 16:51
查看更多