【问题标题】:Change UI based on System Language - Swift根据系统语言更改 UI - Swift
【发布时间】:2019-08-28 10:52:23
【问题描述】:

我有一个应用程序,我想支持 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)
        }
    }

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

【问题讨论】:

  • 你试过调试它吗?你使用断点吗?您的语言可能与 el_US 不同

标签: ios swift xcode localization


【解决方案1】:

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

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

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

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

【讨论】:

  • 感谢您的回答。我将如何在 Localizable.strings 文件中定义图像。
  • 另外我不得不提一下,这些图片在 assets 文件夹中。
  • 可以将它们放在 assets 文件夹中。例如,您需要在Localizable.strings (Greek) 中有一行如"scanBtn" = "scanBtnGr";。在此之前,您需要确保正确设置本地化。您可以通过此链接了解有关本地化的更多信息。 medium.com/lean-localization/…
  • 效果很好。谢谢你。我也没有将setupUI 放在viewWillAppear 中。它在viewDidLoad 中运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多