Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2021 EssentialsSwiftUI & UI FrameworksSystem ServicesAccessibility & Inclusion

WWDC21 · 27 min · Essentials / SwiftUI & UI Frameworks / System Services / Accessibility & Inclusion

Streamline your localized strings

When you localize the text within your app, you can help make your app more accessible to a worldwide audience. Discover best practices for building your localization workflow, including how to write and format strings accurately, and learn how to prepare strings for localization in different languages using Xcode.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 20 snippets

Declaring a string swift · at 3:30 ↗
Button("Order")
Declaring a string anywhere else swift · at 3:44 ↗
button.title = NSLocalizedString("Order", comment: "…")
button.title = String(localized: "Order")
Declaring a string in a SwiftUI view 2 swift · at 4:20 ↗
Text("Your order is ready.")
Button("Order") {
  // Action…
}
Declare a string in SwiftUI view with verbatim swift · at 4:33 ↗
Text(verbatim: "Sample data")
Button to place an order swift · at 4:54 ↗
// SwiftUI
Button("Order") {  }


// Swift
button.title = String(localized: "Order")
Button to place an order with a variable swift · at 5:15 ↗
let count = 3


// SwiftUI
Button("Order \(count) Tickets") {  }


// Swift
button.title = String(localized: "Order \(count) Tickets")
Button to place an order with a variable 2 swift · at 5:36 ↗
let count = 3

// Supports user’s preferred numbers,
// pluralization, RTL variables isolation…
// Previously: .localizedStringWithFormat()
String(localized: "Order \(count) Tickets")
Use 2 separate strings swift · at 6:21 ↗
// Recommended for all languages
String(localized: "Order Now")
String(localized: "Order Later")
Button to place an order 2 swift · at 6:57 ↗
// SwiftUI
Text("Order")


// Swift
String(localized: "Order")
Button to place an order with comment swift · at 7:09 ↗
// SwiftUI
Text("Order", comment: "Button: confirms concert tickets booking”)


// Swift
String(localized: "Order", comment: "Button: confirms concert tickets booking")
What makes a good comment swift · at 7:36 ↗
Text("Order", comment: "Button: confirms concert tickets booking")
Text("Order", comment: "Button: confirms concert tickets booking")
Text("\(ticketCount) Ordered", comment: "Order summary: total number of tickets ordered")
Request server strings in the user's language swift · at 10:52 ↗
Bundle.preferredLocalizations(from: allServerLanguages).first
Declare a string with a variable, customized table name, and a comment swift · at 11:37 ↗
Text("\(ticketCount) Ordered",
     tableName: "UserProfile",
     comment: "Profile subtitle: total number of tickets ordered")
Using a framework swift · at 13:49 ↗
/* —-----------—------------—-—---- In TicketKit Framework —---------—------------—-—---- */

// TicketKit/OrderStatus.swift
public enum OrderStatus {
    case pending, processing, complete, canceled, invalid(Error)

    var displayName: String {
        switch self {
        case .complete: return String(localized: "Complete",
                                      bundle: Bundle(for: AnyClassInTicketKit.self),
                                      comment: "Standalone ticket status: order finalized")
          
/* —-----------—-----------—---—---       In Host App      —---------—------------—-—---- */

import TicketKit
Text(OrderStatus.complete.displayName)
Import translated strings catalogs bash · at 17:43 ↗
xcodebuild -exportLocalizations -workspace VacationPlanet.xcworkspace -localizationPath ~/Documents
xcodebuild -importLocalizations -workspace VacationPlanet.xcworkspace -localizationPath ~/Documents/de.xcloc
Localized attributed strings swift · at 18:28 ↗
AttributedString(localized: "Your order is **complete**!",
                 comment: "Ticket order confirmation title")
Plural with stringsdict swift · at 19:22 ↗
String(localized: "Order \(ticketCount) Ticket(s)")
Plural for strings without a number swift · at 22:46 ↗
if ticketCount == 1 {
    button.text = String(localized: "Order This Ticket")
} else if ticketCount == 2 { // If needed
    button.text = String(localized: "Order Both Tickets")
} else {
    button.text = String(localized: "Order All Tickets")
}
Automatic grammar agreement swift · at 23:31 ↗
AttributedString(localized: "Order ^[\(ticketsCount) Ticket](inflect: true)")
Format data in strings swift · at 25:45 ↗
["pop", "rock", "electronic"].formatted(.list(type: .or)) // pop, rock, or electronic

Text("Total: \(price, format: .currency(code: "USD"))", // Total: $9.41
     comment: "Order subtitle: total price of all tickets")

Resources