Skip to content

Commit 9ee3c0e

Browse files
authored
Merge pull request #25503 from stevebrun/stevebrun/NSLocalizedString-docs
2 parents c5c6f3b + d0db904 commit 9ee3c0e

File tree

1 file changed

+148
-1
lines changed

1 file changed

+148
-1
lines changed

stdlib/public/Darwin/Foundation/Foundation.swift

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,154 @@ public let NSNotFound: Int = .max
2929
// NSLocalizedString
3030
//===----------------------------------------------------------------------===//
3131

32-
/// Returns a localized string, using the main bundle if one is not specified.
32+
/// Returns the localized version of a string.
33+
///
34+
/// - parameter key: An identifying value used to reference a localized string.
35+
/// Don't use the empty string as a key. Values keyed by the empty string will
36+
/// not be localized.
37+
/// - parameter tableName: The name of the table containing the localized string
38+
/// identified by `key`. This is the prefix of the strings file—a file with
39+
/// the `.strings` extension—containing the localized values. If `tableName`
40+
/// is `nil` or the empty string, the `Localizable` table is used.
41+
/// - parameter bundle: The bundle containing the table's strings file. The main
42+
/// bundle is used by default.
43+
/// - parameter value: A user-visible string to return when the localized string
44+
/// for `key` cannot be found in the table. If `value` is the empty string,
45+
/// `key` would be returned instead.
46+
/// - parameter comment: A note to the translator describing the context where
47+
/// the localized string is presented to the user.
48+
///
49+
/// - returns: A localized version of the string designated by `key` in the
50+
/// table identified by `tableName`. If the localized string for `key` cannot
51+
/// be found within the table, `value` is returned. However, `key` is returned
52+
/// instead when `value` is the empty string.
53+
///
54+
/// Export Localizations with Xcode
55+
/// -------------------------------
56+
///
57+
/// Xcode can read through a project's code to find invocations of
58+
/// `NSLocalizedString(_:tableName:bundle:value:comment:)` and automatically
59+
/// generate the appropriate strings files for the project's base localization.
60+
///
61+
/// In Xcode, open the project file and, in the `Edit` menu, select
62+
/// `Export for Localization`. This will generate an XLIFF bundle containing
63+
/// strings files derived from your code along with other localizable assets.
64+
/// `xcodebuild` can also be used to generate the localization bundle from the
65+
/// command line with the `exportLocalizations` option.
66+
///
67+
/// xcodebuild -exportLocalizations -project <projectname>.xcodeproj \
68+
/// -localizationPath <path>
69+
///
70+
/// These bundles can be sent to translators for localization, and then
71+
/// reimported into your Xcode project. In Xcode, open the project file. In the
72+
/// `Edit` menu, select `Import Localizations...`, and select the XLIFF
73+
/// folder to import. You can also use `xcodebuild` to import localizations with
74+
/// the `importLocalizations` option.
75+
///
76+
/// xcodebuild -importLocalizations -project <projectname>.xcodeproj \
77+
/// -localizationPath <path>
78+
///
79+
/// Choose Meaningful Keys
80+
/// ----------------------
81+
///
82+
/// Words can often have multiple different meanings depending on the context
83+
/// in which they're used. For example, the word "Book" can be used as a noun—a
84+
/// printed literary work—and it can be used as a verb—the action of making a
85+
/// reservation. Words with different meanings which share the same spelling are
86+
/// heteronyms.
87+
///
88+
/// Different languages often have different heteronyms. "Book" in English is
89+
/// one such heteronym, but that's not so in French, where the noun translates
90+
/// to "Livre", and the verb translates to "Réserver". For this reason, it's
91+
/// important make sure that each use of the same phrase is translated
92+
/// appropriately for its context by assigning unique keys to each phrase and
93+
/// adding a description comment describing how that phrase is used.
94+
///
95+
/// NSLocalizedString("book-tag-title", value: "Book", comment: """
96+
/// noun: A label attached to literary items in the library.
97+
/// """)
98+
///
99+
/// NSLocalizedString("book-button-title", value: "Book", comment: """
100+
/// verb: Title of the button that makes a reservation.
101+
/// """)
102+
///
103+
/// Use Only String Literals
104+
/// ------------------------
105+
///
106+
/// String literal values must be used with `key`, `tableName`, `value`, and
107+
/// `comment`.
108+
///
109+
/// Xcode does not evaluate interpolated strings and string variables when
110+
/// generating strings files from code. Attempting to localize a string using
111+
/// those language features will cause Xcode to export something that resembles
112+
/// the original code expression instead of its expected value at runtime.
113+
/// Translators would then translate that exported value—leaving
114+
/// international users with a localized string containing code.
115+
///
116+
/// // Translators will see "1 + 1 = (1 + 1)".
117+
/// // International users will see a localization "1 + 1 = (1 + 1)".
118+
/// let localizedString = NSLocalizedString("string-interpolation",
119+
/// value: "1 + 1 = \(1 + 1)"
120+
/// comment: "A math equation.")
121+
///
122+
/// To dynamically insert values within localized strings, set `value` to a
123+
/// format string, and use `String.localizedStringWithFormat(_:_:)` to insert
124+
/// those values.
125+
///
126+
/// // Translators will see "1 + 1 = %d" (they know what "%d" means).
127+
/// // International users will see a localization of "1 + 1 = 2".
128+
/// let format = NSLocalizedString("string-literal",
129+
/// value: "1 + 1 = %d",
130+
/// comment: "A math equation.")
131+
/// let localizedString = String.localizedStringWithFormat(format, (1 + 1))
132+
///
133+
/// Multiline string literals are technically supported, but will result in
134+
/// unexpected behavior during internationalization. A newline will be inserted
135+
/// before and after the body of text within the string, and translators will
136+
/// likely preserve those in their internationalizations.
137+
///
138+
/// To preserve some of the aesthetics of having newlines in the string mirrored
139+
/// in their code representation, string literal concatenation with the `+`
140+
/// operator can be used.
141+
///
142+
/// NSLocalizedString("multiline-string-literal",
143+
/// value: """
144+
/// This multiline string literal won't work as expected.
145+
/// An extra newline is added to the beginning and end of the string.
146+
/// """,
147+
/// comment: "The description of a sample of code.")
148+
///
149+
/// NSLocalizedString("string-literal-contatenation",
150+
/// value: "This string literal concatenated with"
151+
/// + "this other string literal works just fine.",
152+
/// comment: "The description of a sample of code.")
153+
///
154+
/// Since comments aren't localized, multiline string literals can be safely
155+
/// used with `comment`.
156+
///
157+
/// Work with Manually Managed Strings
158+
/// ----------------------------------
159+
///
160+
/// If having Xcode generate strings files from code isn't desired behavior,
161+
/// call `Bundle.localizedString(forKey:value:table:)` instead.
162+
///
163+
/// let greeting = Bundle.localizedString(forKey: "program-greeting",
164+
/// value: "Hello, World!",
165+
/// table: "Localization")
166+
///
167+
/// However, this requires the manual creation and management of that table's
168+
/// strings file.
169+
///
170+
/// /* Localization.strings */
171+
///
172+
/// /* A friendly greeting to the user when the program starts. */
173+
/// "program-greeting" = "Hello, World!";
174+
///
175+
/// - note: Although `NSLocalizedString(_:tableName:bundle:value:comment:)`
176+
/// and `Bundle.localizedString(forKey:value:table:)` can be used in a project
177+
/// at the same time, data from manually managed strings files will be
178+
/// overwritten by Xcode when their table is also used to look up localized
179+
/// strings with `NSLocalizedString(_:tableName:bundle:value:comment:)`.
33180
public
34181
func NSLocalizedString(_ key: String,
35182
tableName: String? = nil,

0 commit comments

Comments
 (0)