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
84 changes: 82 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ look similar in usage:
val poet = ResourcesPoet.create()
.addBool("is_cool", true)
.addColor("color_primary", "#FF0000")
.addColor("color_accent", 0xFF0000FF) // Int overload
.addComment("This is a comment")
.addDimension("margin", "2dp")
.addDrawable("logo", "@drawable/logo")
Expand All @@ -81,9 +82,16 @@ val poet = ResourcesPoet.create()
.addIntegerArray("numbers", numbers)
.addPlurals("songs", plurals)
.addString("app_name", "Test")
.addStringArray("stuff", strings)
.addStringArray("stuff", strings, translatable = false)
.addStyle("AppTheme.Dark", "Base.AppTheme.Dark")
.addTypedArray("some_typed_array", typedArray)
.addTypedArray("some_typed_array", typedArray, translatable = false)
.addFraction("width", "50%p")
.addIntQuantity("count", quantities)
.addIntQuantityArray("counts", quantities)
.addColorArray("colors", listOf("#FF0000", "#00FF00"))
.addBoolArray("flags", listOf(true, false))
.addReference("my_ref", Reference("drawable", "logo"))
.addFontFamilyRes(FontFamilyRes("normal", "400", R.font.roboto))
```

We do not allow configuration of more complicated resources like `drawable` and `anim` in the creation sense.
Expand Down Expand Up @@ -124,6 +132,78 @@ 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.

## Additional Features

### Color with Int

Add colors using an `Int` value (e.g., `R.color.primary`) instead of a hex string:

```kotlin
val poet = ResourcesPoet.create()
.addColor("color_primary", 0xFF0000) // outputs #000000
.addColor("color_accent", 0xFF0000FF) // outputs #0000FF (alpha stripped)
```

### Translatable Attribute on Arrays

Mark string-arrays and typed-arrays as non-translatable:

```kotlin
val poet = ResourcesPoet.create()
.addStringArray("countries", listOf("US", "UK"), translatable = false)
.addTypedArray("items", listOf("@string/a", "@string/b"), translatable = false)
```

### Style Format Attribute

Add a `format` attribute to styles:

```kotlin
val poet = ResourcesPoet.create()
.addStyle("MyStyle", parentRef = "Base.Style",
styleItems = listOf(StyleItem("android:background", "@color/white")),
format = "string|reference")
```

### Font Family with Resource ID

Add font-family entries using an integer resource reference:

```kotlin
val poet = ResourcesPoet.create()
.addFontFamilyRes(FontFamilyRes("normal", "400", R.font.roboto_regular))
```

### Build to ByteArray

Get the XML as a `ByteArray` for in-memory processing:

```kotlin
val bytes: ByteArray = poet.buildBytes()
```

### Load Font-Family Files

Use `create(file, indent, elementType)` to load existing font-family XML files:

```kotlin
val file = File("res/font/my_fonts.xml")
val poet = ResourcesPoet.create(file, indent = true, elementType = ResourcesPoet.ELEMENT.FONT_FAMILIES)
```

## 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
14 changes: 14 additions & 0 deletions src/main/kotlin/com/commit451/resourcespoet/FontFamilyRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.commit451.resourcespoet

/**
* Represents an Android font-family resource using an integer resource reference
* @param fontStyle the font style (e.g. "normal", "italic")
* @param fontWeight the font weight (e.g. "400", "700")
* @param fontRes the integer resource ID of the font
* See [the Android docs](https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml)
*/
data class FontFamilyRes(
val fontStyle: String,
val fontWeight: String,
val fontRes: Int
)
12 changes: 12 additions & 0 deletions src/main/kotlin/com/commit451/resourcespoet/Quantity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.commit451.resourcespoet

/**
* Represents an Android int-quantity resource
* @param quantity the quantity (zero, one, two, few, many, other)
* @param value the integer value
* See [the Android docs](https://developer.android.com/guide/topics/resources/string-resource.html#Plurals)
*/
data class Quantity(
val quantity: Plural.Quantity,
val value: Int
)
17 changes: 17 additions & 0 deletions src/main/kotlin/com/commit451/resourcespoet/Reference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.commit451.resourcespoet

/**
* Represents an Android @reference resource
* @param type the reference type (e.g. "drawable", "color"), or null for a generic reference
* @param name the resource name
*/
data class Reference(
val type: String? = null,
val name: String
) {
/**
* Format the reference as an Android resource reference string
* e.g. "@drawable/logo" or "@color/primary" or "@string/app_name"
*/
fun toValue(): String = "@${if (type != null) "$type/" else ""}$name"
}
Loading
Loading