Test iOS Language Selection and Fallback on a Remote Mac

You add the missing translations on Windows, hand the project over to a remote Mac to build, and still see the UI in a different language. Before reimporting resources, check the preference list, regional variant matching, and whether views refreshed after the switch. Separating “choose a language” from “fetch a string” into two testable steps makes the problem easier to isolate than repeatedly changing system settings.

Define the three inputs to language selection

Language selection has at least three inputs: the languages actually bundled with the app, the user’s language preferences in priority order, and the product’s default language. Tests must supply all three explicitly. Reading the remote machine’s current language settings directly can make the result change simply because you moved to another machine.

This article supports en, fr, fr-CA, zh-Hans, and zh-Hant, with English as the fallback when nothing matches. This is a set of test resources, not a service node configuration. Let Foundation match language identifiers rather than reducing them to their first two characters.

A language-matching failure and a missing string key are different problems. First establish which resource directory was selected, then check the keys inside it. Otherwise, a missing translation can look like a fallback error.

Also define how user choices map to preferences: when the user selects “Follow System,” pass the system’s preferred language list. When the user selects a specific language, pass only that language, followed by the product default. Do not leave earlier selections at the front of the list.

Test selection and lookup with isolated resources

On the remote Mac, save the following as LanguageCheck.swift from the command line. You need a working Swift toolchain, but no application project, and the script does not change the machine’s preferences. It creates temporary language directories, uses distinct markers to identify the resources actually loaded, and removes the fixtures when it exits.

import Foundation

let supported = ["en", "fr", "fr-CA", "zh-Hans", "zh-Hant"]
let root = FileManager.default.temporaryDirectory
    .appendingPathComponent(UUID().uuidString)

try FileManager.default.createDirectory(
    at: root,
    withIntermediateDirectories: true
)
defer { try? FileManager.default.removeItem(at: root) }

func require(_ condition: Bool, _ message: String) throws {
    if !condition {
        throw NSError(
            domain: "LanguageCheck",
            code: 1,
            userInfo: [NSLocalizedDescriptionKey: message]
        )
    }
}

for language in supported {
    let directory = root.appendingPathComponent("\(language).lproj")
    try FileManager.default.createDirectory(
        at: directory,
        withIntermediateDirectories: true
    )
    try "\"greeting\" = \"\(language)\";\n".write(
        to: directory.appendingPathComponent("Localizable.strings"),
        atomically: true,
        encoding: .utf8
    )
}

func selectedLanguage(_ preferences: [String]) -> String {
    Bundle.preferredLocalizations(
        from: supported,
        forPreferences: preferences + ["en"]
    ).first ?? "en"
}

let cases: [([String], String)] = [
    (["fr-CA", "en"], "fr-CA"),
    (["fr"], "fr"),
    (["zh-Hans"], "zh-Hans"),
    (["zh-Hant"], "zh-Hant"),
    (["es-MX"], "en"),
    ([], "en")
]

for (preferences, expected) in cases {
    let language = selectedLanguage(preferences)
    try require(language == expected, "Unexpected language selection")
    let path = root.appendingPathComponent("\(language).lproj").path
    guard let resource = Bundle(path: path) else {
        throw NSError(domain: "LanguageCheck", code: 2)
    }
    let marker = resource.localizedString(
        forKey: "greeting",
        value: "__MISSING__",
        table: "Localizable"
    )
    try require(marker == expected, "Unexpected resource lookup")
    let missing = resource.localizedString(
        forKey: "unknown",
        value: "__MISSING__",
        table: "Localizable"
    )
    try require(missing == "__MISSING__", "Unexpected missing-key policy")
}

Run:

xcrun swift LanguageCheck.swift

On success, the script produces no output and exits with code zero. If an assertion fails, it throws an error and exits. The fixtures deliberately use markers instead of real translations: two languages may happen to use the same text, which would not prove that the correct resource path was read.

Turn regional variants into explicit test cases

Once exact matches pass, add the combinations that matter to your product. For example, with only fr available, check whether a fr-CA preference resolves to generic French. When regional resources are also available, verify that explicitly selecting a regional variant gives that version priority.

Test Simplified and Traditional Chinese separately; do not collapse them into a single zh. For preferences that specify a region but no script, first record Foundation’s result on the target system, then decide whether the product needs an override. Do not turn an assumed mapping directly into an assertion.

At a minimum, record the preference array, the available language set, the final selection, and the system version. Rerun the same cases after upgrading the toolchain or operating system so you can distinguish changes in platform matching from resources missing in the build.

Refresh the lookup path when switching languages in the app

Switching languages inside an app is not the same as changing the system language. A useful approach is to encapsulate the current language and its resource object in a replaceable service, then have the UI retrieve strings through that service. Do not change global preferences and expect views that already exist to update automatically.

Keep the acceptance sequence fixed: open the page, switch languages, trigger a state refresh, retrieve the strings again, then return to the previous page and check the existing views. Cover navigation titles, dialog buttons, and messages generated in the background—not just the home page’s body text.

Pay particular attention to caching. If a cache key includes only the string key and not the language, a lookup after switching will still return the old string. Include the language in the cache key, or clear the string cache after a successful switch. Recreate date and number formatters according to the new display policy as well, but do not let the display language silently change business calculation rules.

Bring the script into the iOS acceptance workflow

The command-line script runs on macOS and is only a quick way to check your matching assumptions. After moving the same cases into an iOS test target, also read the actual compiled resources in the test bundle. This catches cases where the script’s fixtures pass but the app’s resources were never included in the build target.

Keep four final checks: exact language matching, an explicit fallback when nothing matches, missing keys surfaced as specified, and no stale language left on existing pages after a switch. Use fixed preferences in the test environment, and maintain a separate set of “Follow System” integration tests. The two must not share mutable global state.

A missing key should fail a test immediately. Whether production code should retry the lookup in the default language is a separate policy that needs its own implementation and tests. Do not treat a returned key as a valid translation, and do not assume that language selection is correct merely because English eventually appears.

Frequently asked questions

How can an app explicitly fall back to English?

Include en in the supported languages and append en to the user's preference list. Use the first result from Bundle.preferredLocalizations rather than treating resource array order as the fallback policy.

Does passing the Mac script replace simulator testing?

No. The script checks the current macOS Foundation implementation. Run the same cases in an iOS test target and verify that changing the language also refreshes existing views.

Move your next build to a cloud Mac

Rent a dedicated physical Mac and use its macOS desktop in your browser. Choose a daily, weekly, monthly, or quarterly plan; it stops at the end of your term unless you renew.

Get a Mac

Get a Mac and get back to your project

A dedicated physical Mac, accessible in your browser. Choose a daily, weekly, monthly, or quarterly rental; it stops at the end of your term unless you renew.