How to add the LanguageCoordinator to Swift Testing

Add the targets to the files, modify the initializer and generator. Then initialize the LanguageCoordinator with a isTestingEnvironment flag in the tests.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 11/08/2024 at 16:17
Last Updated on 11/08/2024 at 17:23
<p>Three flowers that represent Swift, Swift Testing and XCode side by side. Beneath them sits the text "Language Coordinator".</p>

Add the targets to the files, modify the initializer and generator. Then initialize the LanguageCoordinator with a isTestingEnvironment flag in the tests.

Subscribe

The following tutorial can be found in the main branch of our Open Source Swift and SwiftUI projects linked below.

Download SwiftUI Starter ProjectDownload Swift Starter Project

Step One: Add the Language Coordinator

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

If you haven't already, add the Language Coordinator and relevant files to the project by following one of the two tutorials below.

How to localize iOS, VisionOS MacOS, TVOS & WatchOS apps in SwiftUIHow to localize iOS, MacOS, TVOS & WatchOS applications in XCode and Swift

Step Two: Add the Testing Targets to All Files

<p>A screenshot of XCode showing the files we selected and how you can add a target using the + button on the inspector on the right hand side.</p>

Select all the LanguageCoordinator, UIContent, Language and String files and add the testing targets to them.

Please note that we have also added targets for our debugging identifiers.

Debugging recommendations for XCode & Swift (iOS)

Make sure you add all relevant testing targets.

<p>A screenshot of XCode showing the modal that allows you to add targets. We have highlighted the test targets and the save button.</p>

Step Four: Update the Initializer

<p>A screenshot of XCode showing the LanguageCoordinator.swift file. Highlighted is how we have added a isTestingEnvironment boolean constant, how it is offered as an optional in the initializer and how its initialized within the initializer with a default of false.</p>

Add an isTestEnvironment boolean constant, and within the initializer set it as an optional parameter that defaults to false.

Please note that we did not use a launch argument as the app must launch with the standard bundle path, whilst the test requires a different path. 

If you use a launch argument, both will use the different path, which will cause the App to not find the files - resulting in empty strings.

Step Five: Update the Generator

<p>A screenshot of XCode showing the LanguageCoordinator+Generate.swift file. Highlighted is how we have adapted the path for the string json files to take in different values if its within the testing environment.</p>

Update the LanguageCoordinator+Generate.swift file to use a different path if it uses the testing environment.

Step Six: Initialize the LanguageCoordinator

<p>A screenshot of XCode showing how we have initialized the LanguageCoordinator in the test files. The initializers set the isTestingEnvironment to true in the initializer.</p>

In the UI Testing or Unit Testing swift file, initialize the LanguageCoordinator and in the initializer set the isTestEnvironment variable to true.

Step Seven: Test

<p>A screenshot of a UITest that has successfully passed.</p>

Write a test that verifies that the LanguageCoordinator text is working within the UI Test or Unit Test.

Run it and verify it works.

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

Search our blog to learn more about Swift, SwiftUI, Swift Testing, 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