Chapters

Hide chapters

Auto Layout by Tutorials

First Edition · iOS 13 · Swift 5.1 · Xcode 11

Section II: Intermediate Auto Layout

Section 2: 10 chapters
Show chapters Hide chapters

Section III: Advanced Auto Layout

Section 3: 6 chapters
Show chapters Hide chapters

12. Internationalization & Localization
Written by Libranner Santos

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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.
© 2024 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now