Home iOS & Swift Books Auto Layout by Tutorials

12
Internationalization & Localization Written by Libranner Santos

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

You can unlock the rest of this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Apps should look and feel local to the user’s region, so if you’re aiming to create apps that users can use globally, you need to keep internationalization and localization in mind.

As it turns out, Interfaces created with Auto Layout can support internationalization, and Xcode provides the tools to help you localize all of your resources.

In this chapter, you’ll learn how to test if your app is internationalization-ready. You’ll also learn about the things you need to consider while creating your constraints, so they can properly handle different languages.

What’s internationalization and localization?

Internationalization refers to creating apps that are agnostic to the language, which means they can handle changes in the content size — for example, displaying text in different languages. Localization, on the other hand, means translating the resources into different languages.

These two concepts are crucial to providing a good user experience, especially when users expect their apps to use their preferred language. As a developer, you must prepare your layouts to support internationalization and localization. Thankfully, with Auto Layout, developing your app for internationalization and localization support is reasonably easy to achieve.

Auto Layout and internationalization

Go to the starter project and open the TodoApp project. Now, build and run.

private func setupConstrainsts() {
  contentView.addSubview(taskNameLabel)
  contentView.addSubview(favoriteButton)
  
  taskNameLabel.translatesAutoresizingMaskIntoConstraints =
    false
  favoriteButton.translatesAutoresizingMaskIntoConstraints =
    false
  
  let constraints = [
    favoriteButton.leadingAnchor.constraint(
      equalTo: contentView.leadingAnchor, 
      constant: 20),
    favoriteButton.centerYAnchor.constraint(
      equalTo: contentView.centerYAnchor),
    
    taskNameLabel.leadingAnchor.constraint(
      equalTo: favoriteButton.trailingAnchor, 
      constant: 20),
    taskNameLabel.trailingAnchor.constraint(
      lessThanOrEqualTo: contentView.trailingAnchor, 
      constant: -20),
    taskNameLabel.centerYAnchor.constraint(
      equalTo: contentView.centerYAnchor)
  ]
  
  NSLayoutConstraint.activate(constraints)
}

Previewing languages in Interface Builder

Interface Builder helps you preview screens in different languages. For this project, the localization files already exist.

Key points

  • Unless strictly necessary, don’t use rightAnchor or leftAnchor to create constraints; instead, use leadingAnchor and trailingAnchor.
  • Use the preview on Interface Builder to test your apps for internationalization.
  • Don’t use fixed constraints on elements that can contain text unless it’s absolutely necessary.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.

Have feedback to share about the online reading experience? If you have feedback about the UI, UX, highlighting, or other features of our online readers, you can send them to the design team with the form below:

© 2021 Razeware LLC

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a raywenderlich.com Professional subscription.

Unlock Now

To highlight or take notes, you’ll need to own this book in a subscription or purchased by itself.