A reference to a localizable string, accessible from another process.
Use ``LocalizedStringResource`` to provide localizable strings with lookups you defer to a later time.
When you create a localized string or a localized attributed string with an initializer that takes `String.LocalizationValue`, those initializers lookup the localized string immediately. If you want to perform the lookup at a later time, use this ``LocalizedStringResource`` type to refer to the localizable strings. Then, when you need to perform localization, create a `String` or ``AttributedString`` from an initializer that takes a ``LocalizedStringResource`` parameter, such as:
- `String`: `String.init(localized:)` or `String.init(localized:options:)`.
- ``AttributedString``: ``AttributedString/init(localized:)``, ``AttributedString/init(localized:including:)-2xebo``, or ``AttributedString/init(localized:including:)-15xc5``.
This approach allows you to provide localizable strings to an entirely separate process, which may use a different locale. For example, consider an app with a data model type called `UserAction` that uses ``LocalizedStringResource`` rather than strings for its `title` and `description` properties.
```swift
public protocol UserAction {
static var title: LocalizedStringResource { get }
static var description: LocalizedStringResource { get }
}
```
This app (or one of its embedded frameworks) then uses these ``LocalizedStringResource`` members to defer looking up localized strings. Typically, this happens when calling out to another process over XPC.
```swift
public func perform(action: UserAction) {
...
// Send text to another process via XPC or similar.
performActionOutOfProcess(title: action.title, description: action.description)
}
```
Then, when the other process receives the call, it can alter the ``locale`` in the ``LocalizedStringResource``, prior to resolving the localized strings.
```swift
func performActionOutOfProcess(title: LocalizedStringResource,
description: LocalizedStringResource) {
// Set resource locales to match the current locale
// of the separate process.
var fixedTitle = title
fixedTitle.locale = .current
var fixedDescription = description
fixedDescription.locale = .current
// Look up localized strings.
let titleString = String(localized: fixedTitle)
let descriptionString = String(localized: fixedDescription)
// Use a correctly localized title/description.
}
```
The AppIntents framework uses ``LocalizedStringResource`` to perform a late resolution of localized strings. This allows the Siri UI to potentially use different localization preferences than the app providing the intent.
Members (13)
get · iOS 16.0+
localizedStringResource
TypeScript
get localizedStringResource(): LocalizedStringResource
Swift declaration
var localizedStringResource: LocalizedStringResource { get }
A resource that helps provide a description of the instance.
This property implements the ``CustomLocalizedStringResourceConvertible`` protocol, which allows conforming types to provide a ``LocalizedStringResource`` that describes the instance.
Creates a localized string resource from the specified string literal.
- Parameters:
- value: The key to use when looking up a localized value.
This initializer uses the default values from ``LocalizedStringResource/init(_:table:locale:bundle:comment:)`` for the `table`, `locale`, `bundle`, and `comment`.
Creates a localized string resource from the given string interpolation.
- Parameters:
- stringInterpolation: The key to use when looking up a localized value, created from a string interpolation.
To create a localized string key from a string interpolation, use the `\()` string interpolation syntax. Swift matches the parameter types in the expression to one of the `appendInterpolation` methods in ``LocalizedStringResource/StringInterpolation``.
This initializer uses the default values from ``LocalizedStringResource/init(_:table:locale:bundle:comment:)`` for the `table`, `locale`, `bundle`, and `comment`.
get · iOS 16.0+
key
TypeScript
get key(): string
Swift declaration
let key: String
The key to use to look up a localized string.
get · iOS 16.0+
table
TypeScript
get table(): (string) | null
Swift declaration
let table: String?
The name of the table containing the key-value pairs.
get · iOS 16.0+
bundle
TypeScript
get bundle(): LocalizedStringResource_BundleDescription
Swift declaration
var bundle: LocalizedStringResource.BundleDescription { get }
The bundle containing the table's strings file.
get · iOS 16.0+
locale
TypeScript
get locale(): Locale
Swift declaration
var locale: Locale
The locale to use to look up the localized string from the string resource.
To perform localization in a different locale, change this value before passing it to a `String` or ``AttributedString`` initializer that takes a ``LocalizedStringResource``.
set · iOS 16.0+
locale
TypeScript
set locale(value: Locale)
Swift declaration
var locale: Locale
The locale to use to look up the localized string from the string resource.
To perform localization in a different locale, change this value before passing it to a `String` or ``AttributedString`` initializer that takes a ``LocalizedStringResource``.
Creates a localized string resource from a static string and its bundle properties.
- Parameters:
- key: The key for an entry in the specified table.
- defaultValue: A localization value to use if `key` doesn't exist in `table`. Xcode's Product > Export Localizations feature also extracts this value as the default translation in the project's development locale.
- table: The name of the table containing the key-value pairs. If not provided, `nil`, or an empty string, this value defaults to `Localizable.strings.`
- locale: The locale for the resource to use. By default, the resource uses ``Locale/current``.
- bundle: A ``LocalizedStringResource/BundleDescription`` that indicates where to locate the table's strings file. By default, the resource uses the main bundle.
- 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.
Creates a localized string resource from a localization key and its bundle properties.
- Parameters:
- keyAndValue: The key for an entry in the specified table.
- table: The name of the table containing the key-value pairs. If not provided, `nil`, or an empty string, this value defaults to `Localizable.strings.`
- locale: The locale for the resource to use. By default, the resource uses ``Locale/current``.
- bundle: A ``LocalizedStringResource/BundleDescription`` that indicates where to locate the table's strings file. By default, the resource uses the main bundle.
- 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.