How to localize iOS, VisionOS MacOS, TVOS & WatchOS apps in SwiftUI

A step by step tutorial describing our method for creating multilingual apps in SwiftUI. Github repository included.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 08/15/2024 at 13:13
Last Updated on 11/08/2024 at 17:11
<p>Two flowers that represent SwiftUI and XCode side by side. Beneath them sits the text "Custom Localization."</p>

A step by step tutorial describing our method for creating multilingual apps in SwiftUI. Github repository included.

SubscribeDownload Open Source SwiftUI Starter ProjectDownload Open Source VisionOS Starter Project

The following tutorial is based on our localization tutorial for Swift and has been adapted to work with SwiftUI, previews and a custom macro that we coded to save hours of time when developing localizable content.

Please consult the posts below to learn about the way this was initially setup, how to create localizable previews as well as how the custom macro works.

How to localize iOS, MacOS, TVOS & WatchOS apps in XCode and SwiftHow to create a complex member Macro in SwiftHow to create Localizable SwiftUI Previews

The example can be found on the main branch of our SwiftUI Open Source Starter Project linked below.

Download Open Source SwiftUI Starter Project

The key differences from our Swift example are the following:

UIContent

A screenshot showing you how to create the UIContent file & the first struct.

Without the StringJSONMacros

<p>A screenshot of XCode showing how the StringJSONMacros reduce the amount of code.</p>

With the StringJSONMacros

UIContent now uses @StringJSON macros to write initializers and variables, saving hours of time.

The StringJSON macros have been tested so they only works within the specific conditions that are required - the test cases and errors that are produced can be found within the StringJSONMacro folder of our project.

To learn more, consult the links below.

How to create a complex member Macro in SwiftWhat is a Macro in Swift?

LanguageCoordinator

<p>A screenshot of Xcode that shows that the initializer of the LanguageCoordinator has been updated from the prior tutorial.</p>

We have made the languageCode a constant for the application, which is set at the initializer.

The initializer has been updated to include an languageCode parameter to allow for Swift Previews to set the locale.

Separately:

  • The coordinator is no longer a singleton as we are using it as an environment variable.
  • The coordinator uses the @Observable macro, meaning it no longer requires notifications as SwiftUI manages this for us.
  • The availableLanguages have been made public so that they are accessible to SwiftUI previews.

To learn more, consult the links below.

SwiftUI: Observable MacroHow to create Localizable SwiftUI Previews

SampleScreen

<p>A screenshot of XCode showing the SampleScreen code for this tutorial.</p>

We are now using a SampleScreen View struct instead of the CustomUIView.

Other changes include:

  • This struct takes in a LanguageCoordinator environment variable.
  • The strings are no longer optional, as the StringJSON macros create the necesary structure to remove this functionality.
  • We no longer need notifications as SwiftUI manages this for us.
  • There are previews for English and Spanish available. This was achieved by passing the language code to the environment variable for each preview.
SwiftUI: Environment Property WrapperHow to create Localizable SwiftUI PreviewsHow to create multiple SwiftUI previews within a single view

How do I add it to Swift Testing?

Follow our guide linked below to get the LanguageCoordinator working with Swift Testing.

How to add the LanguageCoordinator to Swift Testing

Looking to learn more about SwiftUI, Swift, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe