简体   繁体   中英

IOS Static tableview cell cannot be localized

Let say, the storyboard is Main.storyboard and I have a TableViewController . I have a Static Table View with 2 sections, each with 1 row.

I created the localization file, so my project structure looks like this:

  • Main.Storyboard (base)
  • Main.Strings (FR)

Main.Strings contains all the localized string in French. The problem is, when I run the device in FR, the content did not get localized.

Here is a image of the storyboard that I'm talking about. (You can replace label with any text).

Example

Note: It worked before in Xcode 8 with Swift 3.0. However, once I upgraded to Xcode 9 with Swift 4.0, it doesn't seem to work.

Fixed:

  • Xcode bugged. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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