iOS i18n learning notes

12 阅读1分钟

Overview

This article serves as a collection of my learning notes on iOS internationalization. While much of the content overlaps with the offical documentation, ,I've compiled it here for easier personal reference and quick lookup.

Standard Text

Xcode 15 introduced String Catalogs, a streamlined replacement for .string and .stringsdict files. Since the significantly simplify the localization workflow(including pluralization),this post will focus solely on how to use String Catalogs.

image.png

When selecting a String Catalog in Xcode, you'll find it in incredibly user-friendly to add new languages and text entries.

image.png

String Catalog are human-readable. if you open them with a standard text editor,you'll see the following content: code.png

How to Reference Localized strings Now,let's look at how to use the entries define in the String Catalog whthin our code.Taking SayApple as an example:

//1.Using Type-safe Symbols
//Xcode automatically generates static members for our keys,
//allowing for type-safe referencing and autocomplete.
let msgxx = String(localized: .sayApple) 

//2. Using tanditaional String literals
//You can still reference keys using raw strings.
let msgxx2 = String(localized: "SayApple")

//3. In SwiftUI
//SwiftUI views like Text can take these symbols or strings directly.
Text(.sayApple)
Text("SayApple")

image.png

  1. messageTip %d
// Method A: Direct usage in SwiftUI (Most Idiomatic)
Text(.messageTip(10))
// Method B: Manually creating a string (e.g., for ViewModels or Alerts)
let alertString = String(localized: .messageTip(5))

  1. messageTipNew
// Method 1: SwiftUI - The most idiomatic way
// SwiftUI's Text view automatically handles the substitution 
// when using the generated type-safe symbols.
Text(.messageTipNew(2))

// Method 2: Traditional String Formatting
// This approach involves fetching the localized template first, 
// then manually injecting the arguments using String(format:...).
let template = String(localized: "messageTipNew")
let ret = String(format: template, arguments: [2])

// Method 3: Type-Safe Localization Resource
// Here, we use the generated symbol directly to create a localized string.
// This is ideal for logic outside of SwiftUI views (e.g., ViewModels).
let result = String(localized: .messageTipNew(2))

Localized Privacy Descriptions

When requesting sensitive permissions, you must provide a clear reason to the user.If your app supports only a single language,these descriptions can be defined directly in the Info.plist (or under Target->Build Setting in recent Xcode versions). However,for multi-language support, you should create an InfoPlist.xcstrings file and fill in the corresponding keys for standard permissions as shown below:

image.png

This file override any descriptions written in the Info.plist. Consequently, you can safely ignore the configuration in the original plist file.

Localizing Visual Assets

It is common to display different images based on the user's language or region. Xcode provides an incredibly streamliend way to manage this,as demonstrated below:

image.png

image.png

References

localizing-and-varying-text-with-a-string-catalog