ローカライゼーションとインターナショナリゼーション
String Catalogを使用したアプリのローカライゼーションに関する包括的なガイドです。Apple Design Awardのインクルーシビティ部門受賞アプリは、常に複数の言語をサポートし、優れたRTL(右から左)サポートを備えています。
概要
String Catalog(.xcstrings)は、Xcode 15で導入されたアプリのローカライゼーションを管理するための統合フォーマットです。従来の.stringsファイルや.stringsdictファイルを、管理、差分確認、翻訳ワークフローとの統合が容易な単一のJSONベースのフォーマットに置き換えます。
このスキルでは、String Catalog、SwiftUI/UIKitのローカライゼーションAPI、複数形処理、RTLサポート、ロケール対応の書式設定、およびレガシーフォーマットからの移行戦略について説明します。
このスキルを使用するタイミング
- Xcode 15+でString Catalogを設定するとき
- SwiftUIおよびUIKitアプリをローカライズするとき
- 複数形を正しく処理するとき(多くの言語で重要)
- RTL言語(アラビア語、ヘブライ語)をサポートするとき
- 日付、数値、通貨をロケールに応じてフォーマットするとき
- 従来の
.strings/.stringsdictファイルから移行するとき - App ShortcutsとApp Intentsのローカライゼーションを準備するとき
- 不足している翻訳や誤った複数形をデバッグするとき
システム要件
- Xcode 15+: String Catalog(
.xcstrings)のため - Xcode 26+: 自動シンボル生成、
#bundleマクロ、AIによるコメント生成のため - iOS 15+:
LocalizedStringResourceのため - iOS 16+: App Shortcutsのローカライゼーションのため
- それ以前のiOSバージョンでは従来の
.stringsファイルを使用します
パート1: String Catalog(WWDC 2023/10155)
String Catalogの作成
方法1: Xcodeナビゲーター
- ファイル → 新規 → ファイル
- 「String Catalog」を選択
- 名前を付ける(例:
Localizable.xcstrings) - ターゲットに追加
方法2: 自動抽出
Xcode 15は、以下から文字列を自動抽出できます。
- SwiftUIビュー(
Text、Label、Buttonの文字列リテラル) - Swiftコード(
String(localized:)) - Objective-C(
NSLocalizedString) - C(
CFCopyLocalizedString) - Interface Builderファイル(
.storyboard、.xib) - Info.plistの値
- App Shortcutsのフレーズ
必要なビルド設定:
- 「Use Compiler to Extract Swift Strings」 → Yes
- 「Localization Prefers String Catalogs」 → Yes
String Catalogの構造
各エントリには以下が含まれます。
- Key: 一意の識別子(デフォルトは英語の文字列)
- Default Value: 翻訳がない場合のフォールバック
- Comment: 翻訳者向けのコンテキスト
- String Table: 編成用コンテナ(デフォルトは「Localizable」)
.xcstrings JSONの例:
{
"sourceLanguage" : "en",
"strings" : {
"Thanks for shopping with us!" : {
"comment" : "Label above checkout button",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Thanks for shopping with us!"
}
},
"es" : {
"stringUnit" : {
"state" : "translated",
"value" : "¡Gracias por comprar con nosotros!"
}
}
}
}
},
"version" : "1.0"
}
翻訳の状態
Xcodeは各翻訳の状態を追跡します。
- New(⚪)- 文字列がまだ翻訳されていない
- Needs Review(🟡)- ソースが変更され、翻訳が古い可能性がある
- Reviewed(✅)- 翻訳が承認され、最新である
- Stale(🔴)- 文字列がソースコード内に見つからなくなった
ワークフロー:
- 開発者が文字列を追加 → New
- 翻訳者が翻訳を追加 → Reviewed
- 開発者がソースを変更 → Needs Review
- 翻訳者が更新 → Reviewed
- 開発者がコードを削除 → Stale
パート2: SwiftUIのローカライゼーション
LocalizedStringKey(自動)
Stringパラメータを持つSwiftUIビューは、自動的にローカライゼーションをサポートします。
// ✅ 自動的にローカライズ可能
Text("Welcome to WWDC!")
Label("Thanks for shopping with us!", systemImage: "bag")
Button("Checkout") { }
// Xcodeはこれらの文字列をString Catalogに抽出します
仕組み: SwiftUIは内部でLocalizedStringKeyを使用し、String Catalog内の文字列を検索します。
String(localized:)とコメント
Swiftコードでの明示的なローカライゼーションの場合:
// 基本
let title = String(localized: "Welcome to WWDC!")
// 翻訳者向けのコメント付き
let title = String(localized: "Welcome to WWDC!",
comment: "Notification banner title")
// カスタムテーブル付き
let title = String(localized: "Welcome to WWDC!",
table: "WWDCNotifications",
comment: "Notification banner title")
// デフォルト値付き(キー ≠ 英語テキスト)
let title = String(localized: "WWDC_NOTIFICATION_TITLE",
defaultValue: "Welcome to WWDC!",
comment: "Notification banner title")
ベストプラクティス: 翻訳者にコンテキストを与えるために、常にcommentを含めてください。
LocalizedStringResource(遅延ローカライゼーション)
ローカライズ可能な文字列を他の関数に渡す場合:
import Foundation
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."
)
重要な違い: LocalizedStringResourceは、使用されるまでルックアップを遅延させるため、カスタムビューを完全にローカライズ可能にします。
AttributedStringとMarkdown
// Markdownの書式はローカライゼーション全体で保持されます
let subtitle = AttributedString(localized: "**Bold** and _italic_ text")
パート3: UIKitとFoundation
NSLocalizedStringマクロ
// 基本
let title = NSLocalizedString("Recent Purchases", comment: "Button Title")
// テーブル付き
let title = NSLocalizedString("Recent Purchases",
tableName: "Shopping",
comment: "Button Title")
// バンドル付き
let title = NSLocalizedString("Recent Purchases",
tableName: nil,
bundle: .main,
value: "",
comment: "Button Title")
Bundle.localizedString
let customBundle = Bundle(for: MyFramework.self)
let text = customBundle.localizedString(forKey: "Welcome",
value: nil,
table: "MyFramework")
カスタムマクロ
// Objective-C
#define MyLocalizedString(key, comment) \
[myBundle localizedStringForKey:key value:nil table:nil]
Info.plistのローカライゼーション
アプリ名、権限などをローカライズする:
Info.plistを選択- エディタ → ローカライゼーションを追加
- 各言語の
InfoPlist.stringsを作成:
// InfoPlist.strings (Spanish)
"CFBundleName" = "Mi Aplicación";
"NSCameraUsageDescription" = "La app necesita acceso a la cámara para tomar fotos.";
パート4: 複数形
言語によって複数形のルールは異なります。
- 英語: 2つの形式(one, other)
- ロシア語: 3つの形式(one, few, many)
- ポーランド語: 3つの形式(one, few, other)
- アラビア語: 6つの形式(zero, one, two, few, many, other)
SwiftUIでの複数形処理
// Xcodeは自動的に複数形のバリエーションを作成します
Text("\(count) items")
// カスタム書式付き
Text("\(visitorCount) Recent Visitors")
String Catalog内:
{
"strings" : {
"%lld Recent Visitors" : {
"localizations" : {
"en" : {
"variations" : {
"plural" : {
"one" : {
"stringUnit" : {
"state" : "translated",
"value" : "%lld Recent Visitor"
}
},
"other" : {
"stringUnit" : {
"state" : "translated",
"value" : "%lld Recent Visitors"
}
}
}
}
}
}
}
}
}
XLIFFエクスポート形式
翻訳のためにエクスポートする場合(ファイル → ローカライゼーションを書き出す):
レガシー(stringsdict):
<trans-unit id="/%lld Recent Visitors:dict/NSStringLocalizedFormatKey:dict/:string">
<source>%#@recentVisitors@</source>
</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>
String Catalog(よりクリーン):
<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>
複数形変数を含む置換
// 異なる複数形を持つ複数の変数
let message = String(localized: "\(songCount) songs on \(albumCount) albums")
Xcodeは各変数の複数形に対してバリエーションを作成します。
songCount: one, otheralbumCount: one, other- 合計の組み合わせ: 2 × 2 = 4つの翻訳エントリ
パート5: デバイスと幅のバリエーション
デバイス固有の文字列
プラットフォームごとに異なるテキスト:
// 同じコード、デバイスごとに異なる文字列
Text("Bird Food Shop")
String Catalogのバリエーション:
{
"Bird Food Shop" : {
"localizations" : {
"en" : {
"variations" : {
"device" : {
"applewatch" : {
"stringUnit" : {
"value" : "Bird Food"
}
},
"other" : {
"stringUnit" : {
"value" : "Bird Food Shop"
}
}
}
}
}
}
}
}
結果:
- iPhone/iPad: "Bird Food Shop"
- Apple Watch: "Bird Food"(小さな画面用に短く)
幅のバリエーション
ダイナミックタイプとサイズクラスの場合:
Text("Application Settings")
String Catalogは、狭い幅に対して短いテキストを提供できます。
パート6: RTLサポート
レイアウトのミラーリング
SwiftUIは、RTL言語に対して自動的にレイアウトをミラーリングします。
// ✅ アラビア語/ヘブライ語で自動的にミラーリング
HStack {
Image(systemName: "chevron.right")
Text("Next")
}
// iPhone (英語): [>] Next
// iPhone (アラビア語): Next [<]
Leading/Trailing と Left/Right
常にセマンティックな方向を使用:
// ✅ 正しい - 自動的にミラーリング
.padding(.leading, 16)
.frame(maxWidth: .infinity, alignment: .leading)
// ❌ 間違い - ミラーリングされない
.padding(.left, 16)
.frame(maxWidth: .infinity, alignment: .left)
画像とアイコン
反転すべき/すべきでない画像をマークする:
// ✅ 方向性あり - RTLでミラーリング
Image(systemName: "chevron.forward")
// ✅ 方向性なし - 決してミラーリングしない
Image(systemName: "star.fill")
// カスタム画像
Image("backButton")
.flipsForRightToLeftLayoutDirection(true)
RTLモードでのテスト
Xcodeスキーム:
- スキームを編集 → 実行 → オプション
- アプリケーション言語: アラビア語 / ヘブライ語
- または: アプリ言語 → 右から左の疑似言語
シミュレーター: 設定 → 一般 → 言語と地域 → 優先言語の順序
SwiftUIプレビュー:
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environment(\.layoutDirection, .rightToLeft)
.environment(\.locale, Locale(identifier: "ar"))
}
}
パート7: ロケール対応の書式設定
DateFormatter
let formatter = DateFormatter()
formatter.locale = Locale.current // ✅ 現在のロケールを使用
formatter.dateStyle = .long
formatter.timeStyle = .short
let dateString = formatter.string(from: Date())
// 米国: "January 15, 2024 at 3:30 PM"
// フランス: "15 janvier 2024 à 15:30"
// 日本: "2024年1月15日 15:30"
日付フォーマット文字列をハードコードしない:
// ❌ 間違い - 他のロケールで壊れる
formatter.dateFormat = "MM/dd/yyyy"
// ✅ 正しい - ロケールに適応
formatter.dateStyle = .short
通貨用のNumberFormatter
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .currency
let priceString = formatter.string(from: 29.99)
// 米国: "$29.99"
// 英国: "£29.99"
// 日本: "¥30"(整数に丸める)
// フランス: "29,99 €"(カンマ小数点、記号の前にスペース)
MeasurementFormatter
let distance = Measurement(value: 100, unit: UnitLength.meters)
let formatter = MeasurementFormatter()
formatter.locale = Locale.current
let distanceString = formatter.string(from: distance)
// 米国: "328 ft"(ヤードポンド法に変換)
// メートル法の国: "100 m"
ロケール固有のソート
let names = ["Ångström", "Zebra", "Apple"]
// ✅ ロケール対応のソート
let sorted = names.sorted { (lhs, rhs) in
lhs.localizedStandardCompare(rhs) == .orderedAscending
}
// スウェーデン: ["Ångström", "Apple", "Zebra"] (Åはスウェーデン語で最初に来る)
// 米国: ["Ångström", "Apple", "Zebra"] (ÅはAとして扱われる)
パート8: App Shortcutsのローカライゼーション
パラメータ付きフレーズ
import AppIntents
struct ShowTopDonutsIntent: AppIntent {
static var title: LocalizedStringResource = "Show Top Donuts"
@Parameter(title: "Timeframe")
var timeframe: Timeframe
static var parameterSummary: some ParameterSummary {
Summary("\(.applicationName) Trends for \(\.$timeframe)") {
\.$timeframe
}
}
}
String Catalogが自動的に抽出:
- Intentのタイトル
- パラメータ名
- プレースホルダーを含むフレーズテンプレート
ローカライズされたフレーズ:
英語: "Food Truck Trends for this week"
スペイン語: "Tendencias de Food Truck para esta semana"
AppShortcutsProviderのローカライゼーション
struct FoodTruckShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: ShowTopDonutsIntent(),
phrases: [
"\(.applicationName) Trends for \(\.$timeframe)",
"Show trending donuts in \(\.$timeframe)"
],
shortTitle: "Donut Trends",
systemImageName: "chart.bar.fill"
)
}
}
ローカライズ対象:
shortTitle- すべてのフレーズバリエーション
- パラメータ名
パート9: Xcode 26+の新機能
自動シンボル生成
Xcode 26+は、String Catalogのキーから型安全なシンボルを自動生成します。
// Xcode 26+ 自動生成:
// enum LocalizedString {
// static let thanksForShopping = "Thanks for shopping with us!"
// static let welcome = "Welcome to WWDC!"
// }
// 使用例:
Text(LocalizedString.thanksForShopping)
let title = String(localized: LocalizedString.welcome)
#bundle マクロ
ローカライズされたリソースをバンドルする場合:
// #bundleはすべてのローカライゼーションインスタンスを1つの単位にバンドルします
let localizedStrings = #bundle("Localizable")
// 利用可能な場合は型安全なアクセスを提供
AIによるコメント生成
Xcode 26+は、コードコンテキストに基づいてローカライゼーションコメントを自動生成できます。
// AIはコード分析に基づいてコメントを生成します:
// Text("Proceed to Payment") → comment: "Button title on checkout screen"
パート10: 移行戦略
レガシー形式からの移行
.stringsファイルと.stringsdictファイルからの移行:
Xcode 15+の自動移行:
- プロジェクトナビゲーターで
.stringsファイルを選択 - エディタ → 最新のフォーマットに変換
- Xcodeが
.xcstringsファイルに変換 - 手動でレガシーファイルを削除
手動移行:
- キーと値を
Localizable.xcstringsにコピー stringsdictの複数形定義をString Catalogのvariationsディクショナリに変換- プロジェクトから古いファイルを削除
潜在的な問題
- 重複キー: ソースコードと生成されたカタログの両方に同じキーがある場合
- 解決策: レガシーファイルがプロジェクトから完全に削除されていることを確認
- 複数形の不一致:
stringsdictとString Catalogで異なる形式- 解決策: 各言語の複数形ルールを確認
- カスタム文字列テーブル: String Catalog名はファイル名と一致する必要があります
- 解決策:
Localizable.xcstrings(デフォルト)またはカスタムファイルを作成
- 解決策:
トラブルシューティング
翻訳が表示されない
# クリーンビルド
xcodebuild clean build
複数形が正しくない
- キーがソースコード内のフォーマット指定子と正確に一致することを確認
%dと%lld→IntとInt64を確認- String Catalogで「Device」バリエーションが正しく設定されていることを確認
RTLの問題
- すべてのレイアウト制約に左/右ではなく
.leading/.trailingを使用 .flipsForRightToLeftLayoutDirection(true/false)を使用して画像を確認- シミュレーターで疑似言語「右から左」を使用してテスト
ロケールのフォーマット
formatter.locale = Locale.currentを忘れない- ハードコードされた日付/数値フォーマット文字列を使用しない
- ユーザーが地域を変更した場合に、
Locale.currentが更新されることを確認
パフォーマンス
String(localized:)のコストが高い呼び出しをキャッシュ- ループ内での
DateFormatterの作成を避ける - 静的ローカライゼーションには
LocalizedStringResourceを使用
ベストプラクティスの概要
- 常にString Catalogを使用(
.strings/.stringsdictよりも) - 常にコメントを含める(各
String(localized:)call) - レイアウトには左/右ではなく先行/末尾を使用
- 日付/数値フォーマット文字列をハードコードしない
- 正しい複数形のために
LocalizedStringKeyを使用 - RTLとLTRの両方でテスト
- 各言語の翻訳状態を追跡(New/Needs Review/Reviewed/Stale)
.xcstringsファイルをバージョン管理にコミット- XLIFFを使用して翻訳者に書き出す
- ロケール対応の比較には
localizedStandardCompareを使用