diff --git a/README.md b/README.md index bd4c6ca..362f9c2 100644 --- a/README.md +++ b/README.md @@ -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 `` 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 +Close +``` + License -------- diff --git a/src/main/kotlin/com/commit451/resourcespoet/ResourcesPoet.kt b/src/main/kotlin/com/commit451/resourcespoet/ResourcesPoet.kt index 9370361..b1a6b1a 100644 --- a/src/main/kotlin/com/commit451/resourcespoet/ResourcesPoet.kt +++ b/src/main/kotlin/com/commit451/resourcespoet/ResourcesPoet.kt @@ -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 { - //Cool + fun addString(name: String, value: String, translatable: Boolean = true, comment: String? = null, toolsIgnore: String? = null): ResourcesPoet { + //Cool 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) diff --git a/src/test/kotlin/com/commit451/resourcespoet/StringTest.kt b/src/test/kotlin/com/commit451/resourcespoet/StringTest.kt index e5d5328..d0044a8 100644 --- a/src/test/kotlin/com/commit451/resourcespoet/StringTest.kt +++ b/src/test/kotlin/com/commit451/resourcespoet/StringTest.kt @@ -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) + } } diff --git a/src/test/resources/string_comment.xml b/src/test/resources/string_comment.xml new file mode 100644 index 0000000..81bdf05 --- /dev/null +++ b/src/test/resources/string_comment.xml @@ -0,0 +1,4 @@ + + + Close +