メインコンテンツまでスキップ

SwiftUIで直接記述しているTextの文字からLocalized.stringsを生成する

· 約3分
arasan01

macOSにはソースコードのNSLocalizedStringで定義されている文字列に対して抽出してLocalized.stringsを生成するコマンドがあります。これはAppleのドキュメントのManagin Strings Files yourselfのドキュメントで解説されています。

このコマンドはgenstringsというコマンドで定義されており使い方としてはファイル名をgenstringsにpipeから渡すことで使うようです。

$ find . -name \*.m | xargs genstrings -o .

ドキュメントではObjective-Cのコード(.m)を例にしていますが、manコマンドでgenstringsを見るとSwiftUIにも対応しているようです。SwiftUIのTextに直接文字列を記述して利用しているコードがある場合には以下のコードスニペットで同様にLocalized.stringsを作ることができました。

$ find . -name \*.swift | xargs genstrings -SwiftUI -o .

生成されるコードは以下のようなものでした。

  ❯ bat gen/Localizable.strings
───────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: gen/Localizable.strings <UTF-16LE>
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ /* No comment provided by engineer. */
2"%@" = "%@";
3
4 │ /* No comment provided by engineer. */
5"gat" = "gat";
6
7 │ /* No comment provided by engineer. */
8"No Output" = "No Output";
9
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

man genstrings
genstrings(1) General Commands Manual genstrings(1)

NAME
genstrings – generate string tables from source code

SYNOPSIS
genstrings [-a] [-SwiftUI] [-s routine [-s routine ...]] [-skipTable Table [-skipTable Table ...]] [-noPositionalParameters]
[-u] [-encoding charset-name] [-macRoman] [-d] [-q] [-bigEndian | -littleEndian] [-o outputDir] file ...

DESCRIPTION
The genstrings utility generates one or more .strings files from the C, Objective-C, C++, Objective-C++, or Swift source code
files provided as arguments. A .strings file is used for localizing an application for different languages, as described
under Internationalization in the Developer Documentation.

Source Code
genstrings scans the provided source files for calls to the following functions, to extract their string contents and produce
string tables for localization. The NSLocalizedString() macro is used as an example below; by default genstrings recognizes it
and the CFCopyLocalizedString() macro.

To enable support for the SwiftUI Text() initializer, use the -SwiftUI flag.

See the documentation for the -s routine option below for information on how to recognize other patterns.

The "key", "Table", and "value" arguments must be literal strings in order for genstrings to be able to extract them from
source code. The comment argument may be either a literal string or nil, but is strongly recommended to provide context to
localizers.

NSLocalizedString("key", comment)

Source lines containing this form will generate an appropriate string table entry to a file named Localizable.strings.

NSLocalizedStringFromTable("key", "Table", comment)

NSLocalizedStringFromTableInBundle("key", "Table", bundle, comment)

Source lines containing either of these forms will generate an appropriate string table entry in a file named Table.strings.

NSLocalizedStringWithDefaultValue("key", "Table", bundle, "value", comment)

Source lines with will generate an appropriate string table entry in a file named Table.strings with a distinct key and value;
all other forms reuse the key as the value.
...
OPTIONS
...
-SwiftUI Enables support for recognizing the SwiftUI Text() initializer, including its single-argument variant.