Localization

Localize Strings

Add Localizable.strings per language and use NSLocalizedString or SwiftUI’s Text("key").

Syntax: "key" = "value"; in each Localizable.strings, read with NSLocalizedString("key", comment: "") or Text("key").

Example

Localizable.strings (en)

Localizable.strings (es)

SwiftUI

"hello" = "Hello";
"welcome_name" = "Welcome, %@";

Pluralization (.stringsdict)

Use .stringsdict to support plural rules that vary by language.

Example

Localizable.stringsdict (en)

SwiftUI usage

{
  "apples_count" : {
"NSStringLocalizedFormatKey" : "%#@apples@",
"apples" : {
  "NSStringFormatSpecTypeKey" : "NSStringPluralRuleType",
  "NSStringFormatValueTypeKey" : "d",
  "one" : "%d apple",
  "other" : "%d apples"
}
} }

SwiftUI Localization Tips

  • Prefer Text("key") for static keys present in Localizable.strings.
  • For values, compose with String.localizedStringWithFormat and a localized key (supports .stringsdict).
  • Use system formatters for locale-aware output: numbers, dates, currency.

Example

Formatters.swift

import SwiftUI

struct PricesView: View {
  let price: Double = 12.5
  let date = Date()
  var body: some View {
VStack(alignment: .leading) {
  Text(price, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))
  Text(date, style: .date)
}
} }

Testing Localization

  • In Xcode scheme, set Application Language to a target language or a Pseudolanguage to catch truncation.
  • Test Right-to-Left using the Right-to-Left Pseudolanguage and verify UI flips correctly.
  • Avoid hard-coded strings; every user-facing string should be a key in Localizable.strings.
  • Use String Catalogs in modern Xcode to manage keys and translations at scale.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *