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.
Declaration
init<S>(localized key: String.LocalizationValue, options: AttributedString.FormattingOptions = [], table: String? = nil, bundle: Bundle? = nil, locale: Locale? = nil, comment: StaticString? = nil, including scope: S.Type) where S : AttributeScopeParameters
- key:
The key for a string in the table that
tableidentifies. - options:
Options that affect the handling of attributes.
- table:
The bundle’s string table to search. If
tableisnilor is an empty string, the method attempts to use the table inLocalizable.strings. The default isnil. - bundle:
The bundle to use for looking up strings. If
nil, an app searches its main bundle. The default isnil. - locale:
The locale of the localized string to retrieve. If
nil, this initializer uses the current locale. The default isnil. - 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.
- scope:
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.
"Add ^[%lld %@ %@](inflect: true) to your order" = "Añadir ^[%1$lld %3$@ %2$@](inflect: true) a tu pedido"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.