Sample CodeiOS, iPadOS, macOSReviewed 2026-07-21View on Apple Developer

Translating text within your app

At a glance

Item Summary
Purpose Display simple system translations and create custom translation experiences.
App architecture A Swift sample with the source-visible chain TranslatingTextAppContentViewViewModelTranslation APIs.
Main patterns Model-View-ViewModel
Project style 12 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: @MainActor, Task closure isolated to MainActor, Task, await suspension point; none alone proves a background thread.
State/event model Source-visible mechanisms: SwiftUI state property wrapper, @Observable.
Key frameworks/packages SwiftUI, Translation, Foundation; these are source dependencies, not architecture labels.

Project structure

Source bundle/
└── TranslatingText/
    ├── TranslatingTextApp.swift
    ├── ViewModel/
    │   ├── ViewModel.swift
    │   └── AvailableLanguage.swift
    ├── Views/
    │   ├── Availability/
    │   │   ├── LanguageAvailability/
    │   │   │   └── LanguageAvailabilityView.swift
    │   │   └── PrepareTranslation/
    │   │       └── PrepareTranslationView.swift
    │   ├── CustomUI/
    │   │   ├── Batch/
    │   │   │   └── BatchOfStringsView.swift
    │   │   ├── BatchAsSequence/
    │   │   │   └── BatchAsSequenceView.swift
    │   │   └── SingleString/
    │   │       └── SingleStringView.swift
    │   ├── RowView.swift
    │   └── SystemUI/
    │       ├── ReplaceTranslationView.swift
    │       └── ViewTranslationView.swift
    └── ContentView.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 4 project/configuration file(s) and 13 source declaration(s).

Overall architecture

Reference code

TranslatingText/TranslatingTextApp.swift:10 — architecture anchor

@main
struct TranslatingTextApp: App {
    @State private var model = ViewModel()

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(model)
        }
    }
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Translation.

Ownership and state

Ownership evidence

TranslatingText/TranslatingTextApp.swift:12 — stored dependency or nearest verified ownership anchor

@main
struct TranslatingTextApp: App {
    @State private var model = ViewModel()
    // ...
}
Owner Object or state Relationship Mutation authority
TranslatingTextApp ViewModel (model) owns wrapper-managed state Owning lexical scope
ViewModel Bool (isTranslationSupported) owns value state App/module collaborators
ViewModel Array (availableLanguages) owns value state App/module collaborators
LanguageAvailabilityView Language (selectedFrom) owns wrapper-managed state Owning lexical scope

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Main isolation @MainActor The cited annotation marks its attached declaration or closure as main-actor isolated. TranslatingText/ViewModel/ViewModel.swift:31
Main isolation Task closure isolated to MainActor The cited operation explicitly enters a main-actor-isolated region. TranslatingText/ViewModel/ViewModel.swift:31
Task creation Task The source creates an unstructured task; surrounding context determines inherited actor isolation. TranslatingText/ViewModel/ViewModel.swift:31
Suspension boundary await suspension point The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. TranslatingText/ViewModel/ViewModel.swift:32

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

TranslatingText/ViewModel/ViewModel.swift:31 — representative execution boundary

        Task { @MainActor in
            let supportedLanguages = await LanguageAvailability().supportedLanguages
            availableLanguages = supportedLanguages.map {
                AvailableLanguage(locale: $0)
            }.sorted()
        }

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
State propagation SwiftUI state property wrapper A SwiftUI property wrapper supplies or observes UI state. TranslatingText/TranslatingTextApp.swift:12
State propagation @Observable Observation macro publishes source-visible changes. TranslatingText/ViewModel/ViewModel.swift:11
Source import SwiftUI The cited file imports this module; runtime use and architectural role are not inferred. TranslatingText/ContentView.swift:8
Source import Translation The cited file imports this module; runtime use and architectural role are not inferred. TranslatingText/ViewModel/ViewModel.swift:9
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. TranslatingText/ViewModel/AvailableLanguage.swift:8

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

TranslatingText/TranslatingTextApp.swift:11 — representative type boundary

@main
struct TranslatingTextApp: App {
    @State private var model = ViewModel()
    // ...
}
Type Responsibility Depends on or conforms to
TranslatingTextApp Application entry and top-level composition App
ViewModel UI-facing state and feature coordination Concrete collaborators/imported frameworks
LanguageAvailabilityView User-interface presentation and input forwarding View
ContentView User-interface presentation and input forwarding View
PrepareTranslationView User-interface presentation and input forwarding View
BatchOfStringsView User-interface presentation and input forwarding View
BatchAsSequenceView User-interface presentation and input forwarding View
SingleStringView User-interface presentation and input forwarding View
RowView User-interface presentation and input forwarding View
ReplaceTranslationView User-interface presentation and input forwarding View

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
model (TranslatingText/TranslatingTextApp.swift:12) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
shortName (TranslatingText/ViewModel/AvailableLanguage.swift:25) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: hide an implementation step that is not part of the collaboration surface.
selectedFrom (TranslatingText/Views/Availability/LanguageAvailability/LanguageAvailabilityView.swift:15) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.
selectedTo (TranslatingText/Views/Availability/LanguageAvailability/LanguageAvailabilityView.swift:16) private Use is restricted to the lexical declaration and same-file extensions allowed by Swift. Inference: keep state mutation or dependency lifetime inside the owning implementation.

Reference code

TranslatingText/TranslatingTextApp.swift:12 — representative boundary

@main
struct TranslatingTextApp: App {
    @State private var model = ViewModel()
    // ...
}

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Application entry and top-level composition TranslatingTextApp The source’s App suffix makes this role explicit.
User-interface presentation and input forwarding BatchAsSequenceView, BatchOfStringsView, ContentView, LanguageAvailabilityView The source’s View suffix makes this role explicit.
UI-facing state and feature coordination ViewModel The source’s ViewModel suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
Model-View-ViewModel TranslatingText/ViewModel/ViewModel.swift:12 Role-named view models keep UI-facing state or coordination outside view declarations.

Naming conventions

  • Types: App: TranslatingTextApp; View: BatchAsSequenceView, BatchOfStringsView, ContentView, LanguageAvailabilityView, PrepareTranslationView; ViewModel: ViewModel.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: reset, prepareSupportedLanguages, translate, translateAllAtOnce, translateSequence, checkLanguageSupport, performCheck, triggerTranslation.
  • Files: TranslatingText/TranslatingTextApp.swift, TranslatingText/ViewModel/ViewModel.swift, TranslatingText/Views/Availability/LanguageAvailability/LanguageAvailabilityView.swift, TranslatingText/ContentView.swift, TranslatingText/Views/Availability/PrepareTranslation/PrepareTranslationView.swift, TranslatingText/Views/CustomUI/Batch/BatchOfStringsView.swift.

Architecture takeaways

  • TranslatingTextApp is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches SwiftUI, Translation through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
TranslatingText/TranslatingTextApp.swift Cited implementation, TranslatingTextApp, SwiftUI state property wrapper
TranslatingText/ViewModel/AvailableLanguage.swift Cited implementation, Foundation, AvailableLanguage
TranslatingText/Views/Availability/LanguageAvailability/LanguageAvailabilityView.swift Cited implementation, LanguageAvailabilityView, LanguagePair
TranslatingText/ViewModel/ViewModel.swift ViewModel, @MainActor, Task closure isolated to MainActor, Task, await suspension point, @Observable, Translation
TranslatingText/ContentView.swift SwiftUI, ContentView
TranslatingText/Views/Availability/PrepareTranslation/PrepareTranslationView.swift PrepareTranslationView
TranslatingText/Views/CustomUI/Batch/BatchOfStringsView.swift BatchOfStringsView
TranslatingText/Views/CustomUI/BatchAsSequence/BatchAsSequenceView.swift BatchAsSequenceView
TranslatingText/Views/CustomUI/SingleString/SingleStringView.swift SingleStringView
TranslatingText/Views/RowView.swift RowView
TranslatingText/Views/SystemUI/ReplaceTranslationView.swift ReplaceTranslationView
TranslatingText/Views/SystemUI/ViewTranslationView.swift ViewTranslationView