2022 Accessibility & InclusionSwiftUI & UI Frameworks
WWDC22 · 22 min · Accessibility & Inclusion / SwiftUI & UI Frameworks
Build global apps: Localization by example
Learn how you can run your apps on devices around the world and help everyone have a great experience — regardless of the language they speak. We’ll explore how Apple APIs can provide a solid foundation when creating apps for diverse audiences, and we’ll share examples, challenges, and best practices from our own experiences.
Watch at developer.apple.com ↗Code shown on screen · 15 snippets
Declare strings using String(localized)
let windPerceptionLabelText = String(
localized: "Wind is making it feel cooler",
comment: "Explains the wind is lowering the apparent temperature"
) Translation example 1
let filter = String(localized: "Archive.label",
defaultValue: "Archive",
comment: "Name of the Archive folder in the sidebar")
let filter = String(localized: "Archive.menuItem",
defaultValue: "Archive",
comment: "Menu item title for moving the email into the Archive folder") Translation example 2
String(localized: "Show weather in \(locationName)",
comment: "Title for a user activity to show weather at a specific city")
String(localized: "Show weather in My Location",
comment: "Title for a user activity to show weather at the user's current location") Comment example
String(localized: "Show weather in \(locationName)",
comment: "Title for a user activity to show weather at a specific city") Localized remote content example
let allServerLanguages = ["bg", "de", "en", "es", "kk", "uk"]
let language = Bundle.preferredLocalizations(from: allServerLanguages).first Numbers in a string example 1
String(localized: "\(amountOfRain) in last \(numberOfHours) hour",
comment: "Label showing how much rain has fallen in the last number of hours")
String(localized: "\(amountOfRain) in last ^[\(numberOfHours) hour](inflect: true)",
comment: "Label showing how much rain has fallen in the last number of hours") Numbers in a string example 2
if selectedCount == 1 {
return String(localized: "Remove this city
from your favorites")
} else {
return String(localized: "Remove these cities
from your favorites")
} Numbers in a string example 3
String(localized: "\(amountOfRain) in last ^[\(numberOfHours) hour](inflect: true).",
comment: "Label showing how much rain has fallen in the last number of hours") Formatter example
let humidity = 54
// In a SwiftUI view
Text(humidity, format: .percent)
// In Swift code
humidity.formatted(.percent) Formatter example 2
date.formatted(
.dateTime.year()
.month()
) // Jun 2022
whatToExpect.formatted()
// New features, exciting API, and advanced tips
amountOfRain.formatted(
.measurement(
width: .narrow,
usage: .rainfall)) // 12mm
(date...<later).formatted(
.components(
style: .wide
)
) // 24 minutes, 18 Seconds
date.formatted(
.relative(
presentation:
.numeric
)
) // 2 minutes ago
let components = PersonNameComponents()
…
nameComponentsFormatter
.string(from: components)
// Andreas Neusüß or 田中陽子
excitementLevel.formatted(
.number
.precision(
.fractionLength(2)
)
) // 1,001.42
price.formatted(
.currency(
code: "EUR"
)
) // $20.99
distance.formatted(
.measurement(
width: .wide,
usage: .road)
) // 500 feet
bytesCopied.formatted(
.byteCount(
style: .file
)) // 42.23 MB Combine a formatter with text
func expectedPrecipitationIn24Hours(for valueInMillimeters: Measurement<UnitLength>) -> String {
// Use user's preferred measures
let preferredUnit = UnitLength(forLocale: .current, usage: .rainfall)
let valueInPreferredSystem = valueInMillimeters.converted(to: preferredUnit)
// Format the amount of rainfall
let formattedValue = valueInPreferredSystem
.formatted(.measurement(width: .narrow, usage: .asProvided))
let integerValue = Int(valueInPreferredSystem.value.rounded())
// Load and use formatting string
return String(localized: "EXPECTED_RAINFALL",
defaultValue: "\(integerValue) \(formattedValue) expected in next \(24)h.",
comment: "Label - How much precipitation (2nd formatted value, in mm or Inches) is expected in the next 24 hours (3rd, always 24).")
} Stringsdict examples in English and Spanish
Localizable.stringsdict English:
<plist version="1.0">
<dict>
<key>EXPECTED_RAINFALL</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@next_expected_precipitation_amount_24h@</string>
<key>next_expected_precipitation_amount_24h</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>other</key>
<string>%2$@ expected in next %3$dh.</string>
</dict>
</dict>
</dict>
</plist>
Localizable.stringsdict Spanish:
<plist version="1.0">
<dict>
<key>EXPECTED_RAINFALL</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@next_expected_precipitation_amount_24h@</string>
<key>next_expected_precipitation_amount_24h</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>one</key>
<string>Se prevé %2$@ en las próximas %3$d h.</string>
<key>other</key>
<string>Se prevén %2$@ en las próximas %3$d h.</string>
</dict>
</dict>
</dict>
</plist> Swift Package localization example
let package = Package(
name: "FoodTruckKit",
defaultLocalization: "en",
products: [
.library(
name: "FoodTruckKit",
targets: ["FoodTruckKit"]),
],
…
) Loading a string in a Swift Package
let title = String(localized: "Wind",
bundle: .module,
comment: "Title for section that
shows data about wind.") Grid example
// Requires data types "Row" and "row" to be defined
struct WeatherTestView: View {
var rows: [Row]
var body: some View {
Grid(alignment: .leading) {
ForEach(rows) { row in
GridRow {
Text(row.dayOfWeek)
Image(systemName: row.weatherCondition)
.symbolRenderingMode(.multicolor)
Text(row.minimumTemperature)
.gridColumnAlignment(.trailing)
Capsule().fill(Color.orange).frame(height: 4)
Text(row.maximumTemperature)
}
.foregroundColor(.white)
}
}
}
} Resources
Related sessions
-
18 min -
20 min -
27 min -
34 min -
21 min -
27 min -
15 min -
16 min