|
1 | 1 | [//]: # (title: Using multiplatform resources in your app)
|
2 | 2 |
|
3 |
| -<toc-settings structure-elements="chapter" depth="3"/> |
| 3 | +<show-structure depth="2"/> |
4 | 4 |
|
5 | 5 | When you've [set up the resources for your project](compose-multiplatform-resources-setup.md),
|
6 | 6 | 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:
|
154 | 154 | * `\t` — for a tab symbol
|
155 | 155 | * `\uXXXX` — for a specific Unicode character
|
156 | 156 |
|
| 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 | + |
157 | 160 | #### String templates
|
158 | 161 |
|
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: |
160 | 166 |
|
161 | 167 | ```XML
|
162 | 168 | <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> |
164 | 170 | </resources>
|
165 | 171 | ```
|
166 | 172 |
|
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: |
169 | 182 |
|
170 | 183 | ```kotlin
|
171 |
| -Text(stringResource(Res.string.str_template, "User_name", 100)) |
| 184 | +Text(stringResource(Res.string.str_template, "User_name", 100.1f)) |
172 | 185 | ```
|
173 | 186 |
|
174 | 187 | #### String arrays
|
|
0 commit comments