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

2023 EssentialsSwiftUI & UI FrameworksDeveloper Tools

WWDC23 · 32 min · Essentials / SwiftUI & UI Frameworks / Developer Tools

Discover String Catalogs

Discover how Xcode 15 makes it easy to localize your app by managing all of your strings in one place. We’ll show you how to extract, edit, export, and build strings in your project using String Catalogs. We’ll also share how you can adopt String Catalogs in existing projects at your own pace by choosing which files to migrate.

Watch at developer.apple.com ↗

Transcript all transcripts

Chapters

Code shown on screen · 15 snippets

Localizable string swift · at 4:30 ↗
String(localized: "Welcome to WWDC!")
Localizable string with default value swift · at 4:42 ↗
String(localized: "WWDC_NOTIFICATION_TITLE",
       defaultValue: "Welcome to WWDC!")
Localizable string with comment swift · at 5:05 ↗
String(localized: "Welcome to WWDC!",
       comment: "Notification banner title")
Localizable string with table and comment swift · at 5:22 ↗
String(localized: "Welcome to WWDC!",
       table: "WWDCNotifications",
       comment: "Notification banner title")
Localizable strings in SwiftUI swift · at 7:36 ↗
// Localizable strings in SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Label("Thanks for shopping with us!", systemImage: "bag")
                .font(.title)

            HStack {
                Button("Clear Cart") { }

                Button("Checkout") { }
            }
        }
    }
}
Localizable strings in SwiftUI with LocalizedStringKey swift · at 8:01 ↗
// Localizable strings in SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            // init(_ titleKey: LocalizedStringKey, systemImage name: String)
            Label("Thanks for shopping with us!", systemImage: "bag")
                .font(.title)
            
            HStack {
                Button("Clear Cart") { }

                Button("Checkout") { }
            }
        }
    }
}
Localizable strings in SwiftUI text view swift · at 8:08 ↗
// Localizable strings in SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Label {
                Text("Thanks for shopping with us!", comment: "Label above checkout button")
            } icon: {
                Image(systemName: "bag")
            }
            .font(.title)
            
            HStack {
                Button("Clear Cart") { }
                Button("Checkout") { }
            }
        }
    }
}
Localizable strings in SwiftUI custom view swift · at 8:16 ↗
// Localizable strings in SwiftUI

struct CardView: View {
    let title: LocalizedStringResource
    let subtitle: LocalizedStringResource

    var body: some View {
        ZStack {
            RoundedRectangle(cornerRadius: 10.0)
            VStack {
                Text(title)
                Text(subtitle)
            }
            .padding()
        }
    }
}

CardView(title: "Recent Purchases", subtitle: "Items you’ve ordered in the past week.")
Localizable strings in Swift displayed at runtime swift · at 9:03 ↗
// Localizable strings in Swift

import Foundation

func stringsToPresent() -> (String, AttributedString) {
    let deferredString = LocalizedStringResource("Title")
  
    
  
    return (
        String(localized: deferredString),
        AttributedString(localized: "**Attributed** _Subtitle_")
    )
}
Localizable strings in Objective-C objectivec · at 9:44 ↗
// Localizable strings in Objective-C

#import <Foundation/Foundation.h>

- (NSString *)stringForDisplay {
    return NSLocalizedString(@"Recent Purchases", @"Button Title");
}

#define MyLocalizedString(key, comment) \
    [myBundle localizedStringForKey:key value:nil table:nil]
Localizable strings in C cpp · at 10:04 ↗
// Localizable strings in C

#include <CoreFoundation/CoreFoundation.h>

CFStringRef stringForDisplay(void) {
    return CFCopyLocalizedString(CFSTR("Recent Purchases"), CFSTR("Button Title"));
}

#define MyLocalizedString(key, comment) \
    CFBundleCopyLocalizedString(myBundle, key, NULL, NULL)
App Shortcut phrases swift · at 11:23 ↗
// App Shortcut phrases

struct FoodTruckShortcuts: AppShortcutsProvider {
    static var appShortcuts: [AppShortcut] {
        AppShortcut(
            intent: ShowTopDonutsIntent(),
            phrases: [
                "\(.applicationName) Trends for \(\.$timeframe)",
                "Show trending donuts for \(\.$timeframe) in \(.applicationName)",
                "Give me trends for \(\.$timeframe) in \(.applicationName)"
            ]
        )
    }
}
Stringsdict in XLIFF xml · at 23:53 ↗
// Stringsdict in XLIFF

<trans-unit id="/%lld Recent Visitors:dict/NSStringLocalizedFormatKey:dict/:string">
    <source>%#@recentVisitors@</source>
    <target>%#@recentVisitors@</target>
</trans-unit>

<trans-unit id="/%lld Recent Visitors:dict/recentVisitors:dict/one:dict/:string">
    <source>%lld Recent Visitor</source>
    <target>%lld Visitante Recente</target>
</trans-unit>

<trans-unit id="/%lld Recent Visitors:dict/recentVisitors:dict/other:dict/:string">
    <source>%lld Recent Visitors</source>
    <target>%lld Visitantes Recentes</target>
</trans-unit>
String Catalog in XLIFF xml · at 24:08 ↗
// String Catalog in XLIFF

<trans-unit id="%lld Recent Visitors|==|plural.one">
    <source>%lld Recent Visitor</source>
    <target>%lld Visitante Recente</target>
</trans-unit>

<trans-unit id="%lld Recent Visitors|==|plural.other">
    <source>%lld Recent Visitors</source>
    <target>%lld Visitantes Recentes</target>
</trans-unit>
String Catalog variations in XLIFF xml · at 24:58 ↗
// Overriding variation in XLIFF

<trans-unit id="Bird Food Shop|==|device.applewatch">
    <source>Bird Food Shop</source>
    <target>Loja de Comida</target>
</trans-unit>

<trans-unit id="Bird Food Shop|==|device.other">
    <source>Bird Food Shop</source>
    <target>Loja de Comida de Passarinho</target>
</trans-unit>

Resources