Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ val poet = ResourcesPoet.create()

Top-level and per-element ignores can be combined — the top-level applies to all children, and per-element ignores override or add to it.

## Adding Resource Comments

You can add a `comment` attribute to `<string>` elements. This comment is displayed in Android Studio's resource inspector, making it easier for translators and developers to understand the purpose of each string:

```kotlin
val poet = ResourcesPoet.create()
.addString("dialog_close_button", "Close", comment = "Button to dismiss the dialog")
```

```xml
<string comment="Button to dismiss the dialog" name="dialog_close_button">Close</string>
```

License
--------

Expand Down
8 changes: 6 additions & 2 deletions src/main/kotlin/com/commit451/resourcespoet/ResourcesPoet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,20 @@ class ResourcesPoet private constructor(
* @param name the name
* @param value the value
* @param translatable whether this string should be translated
* @param comment comment for the string (shown in Android Studio resource inspector)
* @param toolsIgnore lint rule names to suppress (e.g., "UnusedResource")
* @return poet
*/
fun addString(name: String, value: String, translatable: Boolean = true, toolsIgnore: String? = null): ResourcesPoet {
//<string name="app_name" translatable="false">Cool</string>
fun addString(name: String, value: String, translatable: Boolean = true, comment: String? = null, toolsIgnore: String? = null): ResourcesPoet {
//<string name="app_name" translatable="false" comment="...">Cool</string>
val element = document.createElement(Type.STRING.toString())
element.setAttribute("name", name)
if (!translatable) {
element.setAttribute("translatable", "false")
}
if (comment != null) {
element.setAttribute("comment", comment)
}
setToolsIgnore(element, toolsIgnore)
element.appendChild(document.createTextNode(value))
resourceElement.appendChild(element)
Expand Down
8 changes: 8 additions & 0 deletions src/test/kotlin/com/commit451/resourcespoet/StringTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ class StringTest {

TestUtil.assertEquals("string_translatable_false.xml", poet)
}

@Test
fun stringCommentTest() {
val poet = ResourcesPoet.create()
.addString("dialog_close_button", "Close", comment = "Button to dismiss the dialog")

TestUtil.assertEquals("string_comment.xml", poet)
}
}
4 changes: 4 additions & 0 deletions src/test/resources/string_comment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string comment="Button to dismiss the dialog" name="dialog_close_button">Close</string>
</resources>
Loading