Skip to content

Commit dd2f451

Browse files
authored
fix: clearer explanation of string templates (JetBrains#301)
Android string resources are a bit more limited, with this change we clarify that.
1 parent 3add6d8 commit dd2f451

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

topics/compose/compose-multiplatform-resources-usage.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[//]: # (title: Using multiplatform resources in your app)
22

3-
<toc-settings structure-elements="chapter" depth="3"/>
3+
<show-structure depth="2"/>
44

55
When you've [set up the resources for your project](compose-multiplatform-resources-setup.md),
66
build the project to generate the special `Res` class which provides access to resources.
@@ -154,21 +154,34 @@ You can use special symbols in string resources:
154154
* `\t` — for a tab symbol
155155
* `\uXXXX` — for a specific Unicode character
156156

157+
You don't need to escape special XML characters like "@" or "?"
158+
as you do [for Android strings](https://developer.android.com/guide/topics/resources/string-resource#escaping_quotes).
159+
157160
#### String templates
158161

159-
Currently, arguments have basic support in string resources:
162+
Currently, arguments have basic support for string resources.
163+
When creating a template, use the `%<number>` format to place arguments within the string and include a `$d` or `$s` suffix
164+
to indicate that it is a variable placeholder and not simple text.
165+
For example:
160166

161167
```XML
162168
<resources>
163-
<string name="str_template">Hello, %1$s! You have %2$d new messages.</string>
169+
<string name="str_template">Hello, %2$s! You have %1$d new messages.</string>
164170
</resources>
165171
```
166172

167-
There is no difference between `%...s` and `%...d ` when using string templates with arguments from composable code,
168-
for example:
173+
After creating and importing the string template resource, you can refer to it while
174+
passing the arguments for placeholders in the correct order:
175+
176+
```kotlin
177+
Text(stringResource(Res.string.str_template, 100, "User_name"))
178+
```
179+
180+
There is no difference between the `$s` and `$d` suffixes, and no others are supported.
181+
You can put the `%1$s` placeholder in the resource string and use it to display a fractional number, for example:
169182

170183
```kotlin
171-
Text(stringResource(Res.string.str_template, "User_name", 100))
184+
Text(stringResource(Res.string.str_template, "User_name", 100.1f))
172185
```
173186

174187
#### String arrays

0 commit comments

Comments
 (0)