|
1 | 1 | # Localization |
2 | 2 |
|
3 | | -## Technical considerations |
| 3 | +Use `NSLocalizedString()` for all user-facing text. During release, strings are automatically extracted and uploaded to GlotPress for translation. The developes do not need to edit `Localizable.strings`. |
4 | 4 |
|
5 | | -During development, using [`NSLocalizedString()`](https://developer.apple.com/documentation/foundation/nslocalizedstring) in the code should be enough. You shouldn't need to touch the `Localizable.strings` files manually. |
| 5 | +## Key Rules |
6 | 6 |
|
7 | | -During the release process, `NSLocalizedString` statements are scanned and stored in the `Localizable.strings` file. The file is then uploaded to [GlotPress](https://translate.wordpress.org/projects/apps/ios/) for translation. Before the release build is finalized, all the translations are grabbed from GlotPress and saved back to the `Localizable.strings` files. |
| 7 | +1. **Use reverse-DNS keys**: `"editor.post.buttonTitle"` not `"Post"` |
| 8 | +2. **Always add meaningful comments**: Describe context and placeholders |
| 9 | +3. **Use positional placeholders**: `%1$@`, `%2$d` not just `%@`, `%d` |
| 10 | +4. **No variables in NSLocalizedString**: All parameters must be string literals |
| 11 | +5. **Use String.localizedStringWithFormat**: Don't use string interpolation |
8 | 12 |
|
9 | | -### Use unique reverse-DNS naming style Keys |
10 | | - |
11 | | -Use unique reverse-DNS naming style for keys of localized strings (instead of using the English copy as key). This allows to avoid issues where the same word in English could need different translations based on context, or very long keys causing issues with some translation services. |
12 | | - |
13 | | -```swift |
14 | | -// Do |
15 | | -let postBtnTitle = NSLocalizedString("editor.post.buttonTitle", value: "Post", comment: "Verb. Action to publish a post") |
16 | | -let postType = NSLocalizedString("reader.post.title", value: "Post", comment: "Noun. Describes when an entry is a blog post (and not story or page)" |
17 | | -``` |
18 | | - |
19 | | -```swift |
20 | | -// Don't |
21 | | -let postBtnTitle = NSLocalizedString("Post", comment: "Verb. Action to publish a post") |
22 | | -let postType = NSLocalizedString("Post", comment: "Noun. Describes when an entry is a blog post (and not story or page)" |
23 | | -``` |
24 | | - |
25 | | -### Always add Comments |
26 | | - |
27 | | -Always add a meaningful comment. If possible, describe where and how the string will be used. If there are placeholders, describe what each placeholder is. |
| 13 | +## Examples |
28 | 14 |
|
| 15 | +### Basic Usage |
29 | 16 | ```swift |
30 | | -// Do |
31 | | -let succeededMessage = String(format: NSLocalizedString( |
32 | | - "reader.post.follow.successTitle", |
33 | | - value: "Following %1$@", |
34 | | - comment: "Notice title when following a site succeeds. %1$@ is a placeholder for the site name." |
35 | | -), siteName) |
36 | | -``` |
37 | | - |
38 | | -```swift |
39 | | -// Don't |
40 | | -let succeededMessage = String(format: NSLocalizedString( |
41 | | - "reader.post.follow.successTitle", |
42 | | - value: "Following %1$@", |
43 | | - comment: "" |
44 | | -), siteName) |
45 | | -``` |
46 | | - |
47 | | -Comments help give more context to translators. |
48 | | - |
49 | | -### Use positional placeholders |
50 | | - |
51 | | -Use the `%n$x` format (with `n` being an integer for the parameter position/index in the arguments to `String(format:)`, and `x` being one of the type specifiers like `@` or `d`); in particular, don't use just `%x` (the one without explicit positional index) for positional placeholders. This way, translators will not risk of messing up the parameter resolution order when translating the copy in locales where the order of the words in the sentence might be different than the one in English. |
52 | | - |
53 | | -```swift |
54 | | -// Do |
55 | | -let alertWarning = String(format: NSLocalizedString( |
56 | | - "login.email.locationWarning", |
57 | | - value: "Are you trying to log in to %1$@ near %2$@?", |
58 | | - comment: "Login location warning alert. %1$@ is an account name and %2$@ is a location name." |
59 | | -), accountName, locationName) |
60 | | -``` |
61 | | - |
62 | | -```swift |
63 | | -// Don't |
64 | | -let alertWarning = String(format: NSLocalizedString( |
65 | | - "login.email.locationWarning", |
66 | | - value: "Are you trying to log in to %@ near %@?", |
67 | | - comment: "Login location warning alert." |
68 | | -), accountName, locationName) |
69 | | -``` |
70 | | - |
71 | | -### Do not use Variables |
72 | | - |
73 | | -Do not use variables as the argument of `NSLocalizedString()` (neither for the key, the value or the comment). The string key, value and comment will not be automatically picked up by the `genstrings` tool which expects string literals. |
| 17 | +private enum Strings { |
| 18 | + static let title = NSLocalizedString( |
| 19 | + "settings.screen.title", |
| 20 | + value: "Settings", |
| 21 | + comment: "Title for the settings screen" |
| 22 | + ) |
| 23 | + |
| 24 | + static let saveButton = NSLocalizedString( |
| 25 | + "settings.save.button", |
| 26 | + value: "Save Changes", |
| 27 | + comment: "Button to save settings changes" |
| 28 | + ) |
| 29 | +} |
74 | 30 |
|
75 | | -```swift |
76 | | -// Do |
77 | | -let myText = NSLocalizedString("someScreen.title", value: "This is the text I want to translate.", comment: "Put a meaningful comment here.") |
78 | | -myTextLabel?.text = myText |
| 31 | +// Usage |
| 32 | +Text(Strings.title) |
| 33 | +Button(Strings.saveButton) { /* action */ } |
79 | 34 | ``` |
80 | 35 |
|
| 36 | +### With Placeholders |
81 | 37 | ```swift |
82 | | -// Don't |
83 | | -let myText = "This is the text I want to translate." |
84 | | -myTextLabel?.text = NSLocalizedString("someScreen.title", value: myText, comment: "Put a meaningful comment here.") |
85 | | -let myKey = "some.place.title" |
86 | | -myTextLabel?.text = NSLocalizedString(myKey, value: "This is the text I want to translate.", comment: "Put a meaningful comment here.") |
87 | | -let comment = "Put a meaningful comment here." |
88 | | -myTextLabel?.text = NSLocalizedString("someScreen.title", value: "This is the text I want to translate.", comment: comment) |
89 | | -``` |
90 | | - |
91 | | -### Do not use Interpolated Strings |
92 | | - |
93 | | -Interpolated strings are harder to understand by translators and they may end up translating/changing the variable name, causing a crash. |
94 | | - |
95 | | -Use [`String.localizedStringWithFormat`](https://developer.apple.com/documentation/swift/string/1414192-localizedstringwithformat) instead. |
| 38 | +private enum Strings { |
| 39 | + static let welcomeMessage = NSLocalizedString( |
| 40 | + "dashboard.welcome.message", |
| 41 | + value: "Welcome back, %1$@!", |
| 42 | + comment: "Welcome message on dashboard. %1$@ is the user's name." |
| 43 | + ) |
| 44 | + |
| 45 | + static let postCount = NSLocalizedString( |
| 46 | + "dashboard.post.count", |
| 47 | + value: "You have %1$d posts in %2$@", |
| 48 | + comment: "Post count message. %1$d is number of posts, %2$@ is site name." |
| 49 | + ) |
| 50 | +} |
96 | 51 |
|
97 | | -```swift |
98 | | -// Do |
99 | | -let year = 2019 |
100 | | -let template = NSLocalizedString("mySite.copyrightNotice.title", value: "© %d Acme, Inc.", comment: "Copyright Notice") |
101 | | -let str = String.localizeStringWithFormat(template, year) |
| 52 | +// Usage |
| 53 | +let welcome = String.localizedStringWithFormat(Strings.welcomeMessage, userName) |
| 54 | +let count = String.localizedStringWithFormat(Strings.postCount, postCount, siteName) |
102 | 55 | ``` |
103 | 56 |
|
| 57 | +### Pluralization |
104 | 58 | ```swift |
105 | | -// Don't |
106 | | -let year = 2019 |
107 | | -let str = NSLocalizedString("mySite.copyrightNotice.title", value: "© \(year) Acme, Inc.", comment: "Copyright Notice") |
108 | | -``` |
109 | | - |
110 | | -### Multiline Strings |
111 | | - |
112 | | -For readability, you can split the string and concatenate the parts using the plus (`+`) symbol. |
| 59 | +private enum Strings { |
| 60 | + static let postCountSingular = NSLocalizedString( |
| 61 | + "posts.count.singular", |
| 62 | + value: "%1$d post", |
| 63 | + comment: "Number of posts (singular). %1$d is the count." |
| 64 | + ) |
| 65 | + |
| 66 | + static let postCountPlural = NSLocalizedString( |
| 67 | + "posts.count.plural", |
| 68 | + value: "%1$d posts", |
| 69 | + comment: "Number of posts (plural). %1$d is the count." |
| 70 | + ) |
| 71 | +} |
113 | 72 |
|
114 | | -```swift |
115 | | -// Okay |
116 | | -NSLocalizedString( |
117 | | - "someScreen.concatenatedDescription", |
118 | | - value: "Take some long text here " + |
119 | | - "and then concatenate it using the '+' symbol." |
120 | | - comment: "You can even use this form of concatenation " + |
121 | | - "for extra-long comments that take the time to explain " + |
122 | | - "lots of details to help our translators make accurate translations." |
123 | | -) |
| 73 | +// Usage |
| 74 | +let template = count == 1 ? Strings.postCountSingular : Strings.postCountPlural |
| 75 | +let text = String.localizedStringWithFormat(template, count) |
124 | 76 | ``` |
125 | 77 |
|
126 | | -Do not use extended delimiters (e.g. triple quotes). They are not automatically picked up. |
| 78 | +### Shared Strings |
| 79 | +Use `SharedStrings` (@WordPress/Classes/Utility/SharedStrings.swift) for common UI elements like "Cancel", "Done", "Save". |
127 | 80 |
|
| 81 | +### Numbers |
128 | 82 | ```swift |
129 | | -// Don't |
130 | | -NSLocalizedString( |
131 | | - "someScreen.tripleQuotedDescription", |
132 | | - """Triple-quoted text, when used in NSLocalizedString, is Not OK. Our scripts break when you use this.""" |
133 | | - comment: """Triple-quoted text, when used in NSLocalizedString, is Not OK.""" |
134 | | -) |
| 83 | +let localizedCount = NumberFormatter.localizedString(from: NSNumber(value: count), number: .none) |
135 | 84 | ``` |
136 | 85 |
|
137 | | -### Shared Strings |
138 | | - |
139 | | -Use `SharedStrings` for localizable strings used across many screens like button title "Cancel". |
140 | | - |
141 | | -### Pluralization |
| 86 | +## Organization Pattern |
142 | 87 |
|
143 | | -GlotPress currently does not support pluralization using the [`.stringsdict` file](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPInternational/LocalizingYourApp/LocalizingYourApp.html#//apple_ref/doc/uid/10000171i-CH5-SW10). So, right now, you have to support plurals manually by having separate localized strings. |
| 88 | +Organize strings using private enums within each view or view model: |
144 | 89 |
|
145 | | -This is not an ideal situation, and in the future we're hoping to properly support real pluralization with `.stringdict` files, which takes into account the complexity of different locales having different pluralization rules (sometimes way more complex than the simple singular/plural rule that English has, e.g. like [in Irish](https://unicode-org.github.io/cldr-staging/charts/latest/supplemental/language_plural_rules.html#ga)). |
146 | | - |
147 | | -In the meantime, we sadly have to make-do with the simplistic solution of providing two different localized strings and doing the pluralization decision by code, even if that will lead to some inexact pluralization in some locales. |
148 | 90 | ```swift |
149 | | -struct PostCountLabels { |
150 | | - static let singular = NSLocalizedString("reader.post.title" ,value: "%d Post", comment: "Number of posts displayed in Posting Activity when a day is selected. %d will contain the actual number (singular).") |
151 | | - static let plural = NSLocalizedString("reader.postList.title", value: "%d Posts", comment: "Number of posts displayed in Posting Activity when a day is selected. %d will contain the actual number (plural).") |
| 91 | +struct PostEditorView: View { |
| 92 | + var body: some View { |
| 93 | + NavigationView { |
| 94 | + TextEditor(text: $postContent) |
| 95 | + .placeholder(Strings.placeholder) |
| 96 | + .navigationTitle(Strings.title) |
| 97 | + } |
| 98 | + } |
152 | 99 | } |
153 | 100 |
|
154 | | -let postCountText = (count == 1 ? PostCountLabels.singular : PostCountLabels.plural) |
155 | | -``` |
156 | | - |
157 | | -### Numbers |
158 | | - |
159 | | -Localize numbers whenever possible. |
| 101 | +private enum Strings { |
| 102 | + static let title = NSLocalizedString("editor.title", value: "New Post", comment: "Editor screen title") |
| 103 | + static let placeholder = NSLocalizedString("editor.placeholder", value: "Start writing...", comment: "Editor text placeholder") |
| 104 | +} |
160 | 105 |
|
161 | | -```swift |
162 | | -let localizedCount = NumberFormatter.localizedString(from: NSNumber(value: count), number: .none) |
| 106 | +#Preview { |
| 107 | + PostEditorView() |
| 108 | +} |
163 | 109 | ``` |
164 | 110 |
|
165 | | -## Testing considerations |
| 111 | +## Testing |
166 | 112 |
|
167 | | -Test changes that include localized content by using large words or with letters/accents not frequently used in English. |
| 113 | +Test with long words and special characters to ensure UI layouts work across languages. |
0 commit comments