xcstringstoolsとは何か?Xcode 15に導入されたString Catalogsのための新ツール
謝辞
2023年8月24日に開催された勉強会「公開 SwiftWednesday【iOSDC Japan 2023 直前】」での発表内容を基にした記事です。
はじめに
WWDC23においてXcode 15の新機能として「String Catalogs」が発表されました。
これまでの多言語対応は.stringsファイルが主に利用されてきました。しかし、Xcode 15からは.xcstringsファイルを活用できるようになりました。具体的な使い方については、WWDCのセッション動画を参照ください。
String Catalogs実装時の.ipaファイルの内部変化
以前は.stringsファイルで多言語対応を行うと、その結果として.ipaファイル内にも.stringsファイルが生成されていました。.ipaファイルに生成される.stringsファイルの中身はバイナリ形式になっています。

Xcode 15から利用できる.xcstringsファイルを使用しても、.ipaファイル内にはバイナリ形式の.xcstringsファイルは生成されません。バイナリ形式の.stringsファイルが生成されます。
下図が示すように、.xcstringsファイルをコンパイルすると、Xcodeがそのファイルを.stringsファイルに自動的に変換します。

この変換プロセスにおいて、Xcode 15で新しく追加されたxcstringstoolが利用されていると考えられます。
xcstringstoolについて
xcstringstoolはXcode 15から追加された.xcstringsファイルを操作するためのツールです。このツールはXcode.app/Contents/Developer/usr/bin/ディレクトリに格納されています。
xcstringstoolは以下の3つのサブコマンドを通じて操作することができます。
- compile
- sync
サブコマンドprintの概要は以下のとおりです。このサブコマンドを利用することで.xcstringsファイルに存在しているkeyを取り出すことができます。
OVERVIEW: Prints all string keys represented in an xcstrings file.
USAGE: xcstringstool print <input-file>
ARGUMENTS:
<input-file> The path to the .xcstrings file to print.
OPTIONS:
-h, --help Show help information.
翻訳内容は以下の通りです
概要: xcstringsファイルに表現されているすべての文字列キーを出力します。
使用法: xcstringstool print <input-file>
引数:
<入力ファイル> 表示する.xcstringsファイルへのパス。
オプション:
-h, --help ヘルプ情報を表示します。
compile
サブコマンドcompileの概要は以下のとおりです。このサブコマンドを利用することで.xcstringsファイルをコンパイルし、.stringsファイル、もしくは、.stringsdictファイルを生成することができます
OVERVIEW: Produces build products for an .xcstrings file.
USAGE: xcstringstool compile <input-file> --output-directory <output-directory> [--format <format>] [--language <language> ...] [--serialization-format <serialization-format>] [--dry-run]
ARGUMENTS:
<input-file> The path to the .xcstrings file to compile.
OPTIONS:
-o, --output-directory <output-directory>
The directory to place output files.
-f, --format <format> The output format for the overall compilation (default: stringsAndStringsdict)
-l, --language <language>
Optionally specify particular languages to compile.
You can pass as many of these options as you want.
--serialization-format <serialization-format>
The output format for individual files that can have different representations (default: text)
--dry-run If specified, outputs a newline-separated list of output paths that would be produced by a
compilation, but does not actually compile anything.
-h, --help Show help information.
翻訳内容は以下の通りです
概要: .xcstringsファイルのビルド成果物を生成します。
使用法: xcstringstool compile <input-file> --output-directory <output-directory> [--format <format>] [--language <language> ...] [--serialization-format <serialization-format>] [--dry-run]
引数:
<入力ファイル> コンパイルする.xcstringsファイルへのパス。
オプション:
-o, --output-directory <output-directory>
出力ファイルを配置するディレクトリ。
-f, --format <形式> 全体のコンパイルに対する出力形式(デフォルト: stringsAndStringsdict)
-l, --language <言語>
必要に応じて特定の言語を指定してコンパイルします。
このオプションは何度でも指定することができます。
--serialization-format <serialization-format>
異なる表現を持つことができる個々のファイルの出力形式(デフォルト: text)
--dry-run 指定されている場合、コンパイルによって生成される出力パスの改行
区切りのリストを出力しますが、実際には何もコンパイルしません。
-h, --help ヘルプ情報を表示します。
オプションの--serialization-formatにはtextかbinaryを指定することが可能です。
また、オプションのformatにはstringsAndStringsdictかstringsdictOnlyのいずれかを指定することができます。
以下のコマンドを実行することにより、.ipaファイル内に生成された.stringsファイルと同様のファイルを出力することができます。ポイントは--serialization-formatに対してbinaryを指定することです。
$ xcstringstool compile <input-file> --output-directory <output-directory> --serialization-format binary
sync
サブコマンドsyncの概要は以下のとおりです。
このサブコマンドを利用することで.stringsdataファイルに存在する文字列に基づいて、.xcstringsファイルを更新することができます。
このサブコマンドは.xcstringsへのマイグレーションで利用されているのではないかと推測しています。
OVERVIEW: Updates an .xcstrings file based on strings present in .stringsdata files.
USAGE: xcstringstool sync [<xcstrings> ...] [--stringsdata <stringsdata> ...] [--skip-marking-strings-stale]
ARGUMENTS:
<xcstrings> The .xcstrings files to merge into.
OPTIONS:
--stringsdata <stringsdata>
.stringsdata files specifying the strings used from source.
--skip-marking-strings-stale
Prevents marking strings stale and/or removing them from the .xcstrings when not found in the
.stringsdata.
-h, --help Show help information.
翻訳内容は以下の通りです
$ ./xcstringstool help sync
概要: .stringsdataファイルに存在する文字列に基づいて、.xcstringsファイルを更新します。
使い方: xcstringstool sync [<xcstrings> ...] [--stringsdata <stringsdata> ...] [--skip-marking-strings-stale]
引数:
<xcstrings> マージ先の.xcstringsファイル。
オプション:
--stringsdata <stringsdata>
ソースから使用される文字列を指定する.stringsdataファイル。
--skip-marking-strings-stale
.stringsdataに存在しない場合に、文字列を古いものとしてマークしたり、
.xcstringsからそれらを削除したりするのを防ぎます。
-h, --help ヘルプ情報を表示します。
まとめ
- Xcode 15からは、
.xcstringsファイルを用いて多言語対応が可能です。 -
.xcstringsを使用しても、最終的に.ipaファイル内には.stringsファイルが生成されます。 -
.xcstringsから.stringsへの変換には、新たに追加されたxcstringstoolが利用されていると考えられます。-
xcstringstoolのcompileコマンドを使用すると、バイナリ形式の.stringsファイルを生成できます。 - その際に
--serialization-format binaryというオプションが利用されています。
-
開発環境
- Swift compiler version info:
Apple Swift version 5.9 (swiftlang-5.9.0.128.106 clang-1500.0.40.1)
- Xcode version info:
Xcode 15.0 Build version 15A5229m (beta 8)
参考資料
Discussion