简体   繁体   English

iOS静态tableview单元无法本地化

[英]IOS Static tableview cell cannot be localized

Let say, the storyboard is Main.storyboard and I have a TableViewController . 可以说,故事板是Main.storyboard ,我有一个TableViewController I have a Static Table View with 2 sections, each with 1 row. 我有一个包含2个部分的静态表视图,每个部分都有1行。

I created the localization file, so my project structure looks like this: 我创建了本地化文件,因此我的项目结构如下所示:

  • Main.Storyboard (base) 主板(底盘)
  • Main.Strings (FR) Main.Strings(FR)

Main.Strings contains all the localized string in French. Main.Strings包含所有法语的本地化字符串。 The problem is, when I run the device in FR, the content did not get localized. 问题是,当我在FR中运行设备时,内容未本地化。

Here is a image of the storyboard that I'm talking about. 这是我正在谈论的情节提要的图像。 (You can replace label with any text). (您可以用任何文本替换label )。

Example

Note: It worked before in Xcode 8 with Swift 3.0. 注意:以前在带有Swift 3.0的Xcode 8中可以使用。 However, once I upgraded to Xcode 9 with Swift 4.0, it doesn't seem to work. 但是,一旦我使用Swift 4.0升级到Xcode 9,它似乎就不起作用了。

Fixed: 固定:

  • Xcode bugged. Xcode错误。 Started a new storyboard and everything seems to work perfectly... 开始了一个新的故事板,一切似乎都正常运行...

Localizing storyboard files is difficult to maintain to my opinion. 我认为很难对情节提要文件进行本地化。 I recommend placing all your translations in a single Localizable.strings file and setting the localizations programmatically in the view controllers. 我建议将所有翻译都放在一个Localizable.strings文件中,并在视图控制器中以编程方式设置本地化。

You can make things easier with the following extension: 您可以使用以下扩展程序使事情变得更容易:

extension String {

    var localized: String {
        return NSLocalizedString(self, comment: "")
    }

}

If you then define your translations as global constants like: 如果您随后将翻译定义为全局常量,例如:

class Texts {

    class MyViewController {

        static let title = "My Title".localized
        static let editLabel = "Edit your text".localized
        static let editTextFieldPlaceholder = "Sample text".localized
        static let editButtonTitle = "Edit".localized

    }

}

You can simply set your localizations for a view controller like this: 您可以像这样简单地为视图控制器设置本地化:

override func viewDidLoad() {
    super.viewDidLoad()
    // Localization
    self.title = Texts.MyViewController.title
    self.editLabel.text = Texts.MyViewController.editLabel
    self.editTextField.placeholder = Texts.MyViewController.editTextFieldPlaceholder
    self.editButton.setTitle(Texts.MyViewController.editButtonTitle, for: .normal)
}

This way, your localizations will become much more structured and maintainable. 这样,您的本地化将变得更加结构化和可维护。 Also, you do not need to retranslate the whole storyboard any time you change a single element. 另外,您在更改单个元素时无需重新翻译整个情节提要。 You can even localize plurals , which is just not possible with simple storyboard localization. 您甚至可以将复数形式本地化 ,这只是使用简单的情节提要面板本地化是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM