iOS本地化:动态本地化视图,而不会弄乱情节提要/笔尖字符串文件 [英] iOS Localization: Localize views dynamically without messing with the storyboard/nib strings files

查看:81
本文介绍了iOS本地化:动态本地化视图,而不会弄乱情节提要/笔尖字符串文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我遇到了这个问题,我想本地化我的应用程序中的所有视图.已为我提供了可本地化的字符串文件,包括所有翻译.但是,当我尝试使用字符串文件对情节提要进行本地化时,它显示为如下代码:

So I had this problem, I wanted to localize all views in my app. I have been provided with Localizable strings files including all translations. However, when I tried to localize the storyboard using strings files, it shows as code like this:

/* Class = "UILabel"; text = "Name:"; ObjectID = "21M-2X-4Pf"; */
"21M-2X-4Pf.text" = "Some text to localize";

而且我已经在可本地化的字符串文件中将翻译成一些要本地化的文本.但是,为所有语言手动交叉引用它们似乎很痛苦.特别是在情节提要发生变化时,我必须重新导出它们并添加新的情节.

And I already have the translation to Some Text to localize in the localizable strings files. But manually cross-referencing them for all languages seemed like a pain. Especially when the storyboard changes and I have to re-export them and add the new ones.

推荐答案

我已经有一个 Language Manager 类,可以为我本地化字符串.这是一个非常简单的类,其中最重要的方法是

I already have a class Language Manager that localises strings for me. It is a very simple class which most important method is this

func localizeString(stringToLocalize:String) -> String
{
    // Get the corresponding bundle path.
    let selectedLanguage = self.getLanguage()
    let path = Bundle.main.path(forResource: selectedLanguage, ofType: "lproj")

    // Get the corresponding localized string.
    let languageBundle = Bundle(path: path!)
    return languageBundle!.localizedString(forKey: stringToLocalize, value: "", table: nil)
}

我通过编写一种在视图控制器中递归遍历所有视图的方法扩展了这一点,并对其进行了本地化.我之所以决定分享此信息,是因为我认为它非常有用,可以在任何视图控制器中即插即用,并且可以避免导出Storyboardy字符串文件,添加它们以及在发生任何更改时重新集成它们的过程.这样,您只需将其添加到Localizable.strings文件中,一切便会自动为您处理.

I expanded upon this by writing a method to loop recursively through all views in a view controller, localizing them as it goes. I decided to share this because I think it is quite useful, will work as plug and play in any view controller, and will avoid the cycle of exporting Storyboardy strings files, adding to them, and reintegrating them whenever there is a change. This way you just add to the Localizable.strings file(s) and everything is automatically handled for you.

func localizeUI(parentView:UIView)
{
    for view:UIView in parentView.subviews
    {
        if let potentialButton = view as? UIButton
        {
            if let titleString = potentialButton.titleLabel?.text {
                potentialButton.setTitle(localizeString(stringToLocalize: titleString), for: .normal)
            }
        }

        else if let potentialLabel = view as? UILabel
        {
            if potentialLabel.text != nil {
                potentialLabel.text = localizeString(stringToLocalize: potentialLabel.text!)
            }
        }

        localizeUI(parentView: view)
    }
}

这篇关于iOS本地化:动态本地化视图,而不会弄乱情节提要/笔尖字符串文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆