Contents

init(_:tableName:bundle:comment:)

Creates a text view that displays localized content identified by a key.

Declaration

init(_ key: LocalizedStringKey, tableName: String? = nil, bundle: Bundle? = nil, comment: StaticString? = nil)

Parameters

  • key:

    The key for a string in the table identified by tableName.

  • tableName:

    The name of the string table to search. If nil, use the table in the Localizable.strings file.

  • bundle:

    The bundle containing the strings file. If nil, use the main bundle.

  • comment:

    Contextual information about this key-value pair.

Mentioned in

Discussion

Use this initializer to look for the key parameter in a localization table and display the associated string value in the initialized text view. If the initializer can’t find the key in the table, or if no table exists, the text view displays the string representation of the key instead.

Text("pencil") // Localizes the key if possible, or displays "pencil" if not.

When you initialize a text view with a string literal, the view triggers this initializer because it assumes you want the string localized, even when you don’t explicitly specify a table, as in the above example. If you haven’t provided localization for a particular string, you still get reasonable behavior, because the initializer displays the key, which typically contains the unlocalized string.

If you initialize a text view with a string variable rather than a string literal, the view triggers the init(_:) initializer instead, because it assumes that you don’t want localization in that case. If you do want to localize the value stored in a string variable, you can choose to call the init(_:tableName:bundle:comment:) initializer by first creating a LocalizedStringKey instance from the string variable:

Text(LocalizedStringKey(someString)) // Localizes the contents of `someString`.

If you have a string literal that you don’t want to localize, use the init(verbatim:) initializer instead.

Styling localized strings with markdown

If the localized string or the fallback key contains Markdown, the view displays the text with appropriate styling. For example, consider an app with the following entry in its Spanish localization file:

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

You can create a Text view with the Markdown-formatted base language version of the string as the localization key, like this:

Text("_Please visit our [website](https://www.example.com)._")

When viewed in a Spanish locale, the view uses the Spanish text from the strings file, applying the Markdown styling.

[Image]

See Also

Creating a text view