Generic Initializer

init(localized:options:table:bundle:locale:comment:including:)

Creates an attributed string by looking up a localized string from the app’s bundle, including an attribute scope that a key path identifies.

Declaration 宣言

init<S>(localized key: String.LocalizationValue, options: AttributedString.FormattingOptions = [], table: String? = nil, bundle: Bundle? = nil, locale: Locale? = nil, comment: StaticString? = nil, including scope: KeyPath<AttributeScopes, S.Type>) where S : AttributeScope

Parameters パラメータ

key

The key for a string in the table that table identifies.

options オプション

Options that affect the handling of attributes. 属性の取り扱いに影響を及ぼすオプション。

table

The bundle’s string table to search. If table is nil or is an empty string, the method attempts to use the table in Localizable.strings. The default is nil.

bundle

The bundle to use for looking up strings. If nil, an app searches its main bundle. The default is nil. 省略時にはnilです。

locale

The locale of the localized string to retrieve. If nil, this initializer uses the current locale. The default is nil. 省略時にはnilです。

comment

The comment to place above the key-value pair in the strings file. This parameter provides the translator with some context about the localized string’s presentation to the user. stringsファイルにおいてキー値ペアの上方に置くことになるコメント。このパラメータは、ローカライズされた文字列のユーザへの提示についての何らかの文脈をトランスレータに提供します。

scope

An AttributeScopes key path that identifies an attribute scope to associate with the attributed string.

Discussion 議論

To create localizable attributed strings, use Markdown syntax in your strings files. The following example shows a string from a Spanish localization:


"_Please visit our [website](https://www.example.com)._" = "_Visita nuestro [sitio web](https://www.example.com)._";

You load this string with one of the initializers that takes localized as its first parameter, like the following:


let visitString = AttributedString(localized: "_Please visit our [website](https://www.example.com)._")

The resulting attributed string contains an inlinePresentationIntent attribute to apply the emphasized presentation intent over the entire string. It also applies the link attribute to the text sitio web. User interface frameworks like SwiftUI and UIKit can then use these attributes when presenting the text to the user.

Applying Automatic Grammar Agreement

To apply the automatic grammar agreement feature when loading a localized string, use Apple’s Markdown extension syntax: ^[text to inflect](inflect: true). The following example shows a format string with a Spanish localization for a food-ordering app.


"_Please visit our [website](https://www.example.com)._" = "_Visita nuestro [sitio web](https://www.example.com)._";

You load and localize this string as follows:


let orderString = AttributedString(
     localized: "Add ^[\(quantity) \(foodSizeSelection.localizedName) \(food.localizedName)](inflect: true) to your order")
// "Añadir 2 ensaladas grandes a tu pedido."

When the string loads, the automatic grammar agreement feature adjusts the text for foodSizeSelection and food to match the number and gender of the sentence.

See Also 参照

Creating a Localized Attributed String ローカライズされた属性付き文字列を作成する