forked from MinecraftFreecam/Freecam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support translating name & description
Move name & description definition from `gradle.properties` to a lang file in `:data`. Introduce `ModNameProcessor` & `ModDescriptionProcessor`, along with a new method `LangTask.getTranslation()` which can be used to get a specific translation after the task has finished running. Make use of this in `:data` to get the name & description from the translations into the mod metadata file. Fixes MinecraftFreecam#172
- Loading branch information
1 parent
9a3786b
commit 2eab327
Showing
9 changed files
with
214 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
buildSrc/src/main/kotlin/net/xolt/freecam/gradle/ModDescriptionProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.xolt.freecam.gradle | ||
|
||
internal class ModDescriptionProcessor(private val modID: String, private val variant: String) : LangProcessor { | ||
override fun process( | ||
translations: Map<String, String>, | ||
fallback: Map<String, String>? | ||
): Map<String, String> { | ||
val firstID = "${modID}.description" | ||
val secondID = "${modID}.description.${variant}" | ||
val ids = listOf(firstID, secondID) | ||
|
||
// Nothing to do if this language has no "description" translations | ||
if (ids.none(translations.keys::contains)) { | ||
return translations | ||
} | ||
|
||
val map = translations.toMutableMap() | ||
|
||
// Remove any description.variant keys | ||
map.keys | ||
.filter { it.startsWith("${firstID}.") } | ||
.forEach(map::remove) | ||
|
||
// Set modmenu summary if this language has a translation for firstID | ||
translations[firstID]?.let { map["modmenu.summaryTranslation.${modID}"] = it } | ||
|
||
// Set "full" description | ||
// Use fallback if either part is missing from this language | ||
ids.mapNotNull { translations[it] ?: fallback?.get(it) } | ||
.joinToString(" ") | ||
.let { description -> | ||
map[firstID] = description | ||
map["modmenu.descriptionTranslation.${modID}"] = description | ||
} | ||
|
||
return map | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
buildSrc/src/main/kotlin/net/xolt/freecam/gradle/ModNameProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package net.xolt.freecam.gradle | ||
|
||
internal class ModNameProcessor(private val modID: String, private val variant: String) : LangProcessor { | ||
override fun process( | ||
translations: Map<String, String>, | ||
fallback: Map<String, String>? | ||
): Map<String, String> { | ||
val firstID = "${modID}.name" | ||
val secondID = "${modID}.name.${variant}" | ||
val ids = listOf(firstID, secondID) | ||
|
||
// Nothing to do if this language has no "name" translations | ||
if (ids.none(translations.keys::contains)) { | ||
return translations | ||
} | ||
|
||
val map = translations.toMutableMap() | ||
|
||
// Remove any name.variant keys | ||
map.keys | ||
.filter { it.startsWith("${firstID}.") } | ||
.forEach(map::remove) | ||
|
||
// Set "full" name | ||
// Use fallback if either part is missing from this language | ||
ids.mapNotNull { translations[it] ?: fallback?.get(it) } | ||
.joinToString(" ") | ||
.let { name -> | ||
map[firstID] = name | ||
map["modmenu.nameTranslation.${modID}"] = name | ||
} | ||
|
||
return map | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
buildSrc/src/test/kotlin/net/xolt/freecam/gradle/ModDescriptionProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package net.xolt.freecam.gradle | ||
|
||
import org.junit.jupiter.api.DynamicTest | ||
import org.junit.jupiter.api.TestFactory | ||
|
||
class ModDescriptionProcessorTest { | ||
|
||
@TestFactory | ||
fun `Basic tests`(): List<DynamicTest> { | ||
val sample1 = mapOf( | ||
"modid.description" to "Default description", | ||
"modid.description.special" to "(extra special)" | ||
) | ||
|
||
return listOf( | ||
processorTest( | ||
name = "Discard variant descriptions", | ||
processor = ModDescriptionProcessor("modid", "normal"), | ||
translations = sample1, | ||
result = mapOf( | ||
"modid.description" to "Default description", | ||
"modmenu.descriptionTranslation.modid" to "Default description", | ||
"modmenu.summaryTranslation.modid" to "Default description" | ||
) | ||
), | ||
processorTest( | ||
name = "Append \"extra special\" to description", | ||
processor = ModDescriptionProcessor("modid", "special"), | ||
translations = sample1, | ||
result = mapOf( | ||
"modid.description" to "Default description (extra special)", | ||
"modmenu.descriptionTranslation.modid" to "Default description (extra special)", | ||
"modmenu.summaryTranslation.modid" to "Default description" | ||
) | ||
) | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
buildSrc/src/test/kotlin/net/xolt/freecam/gradle/ModNameProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package net.xolt.freecam.gradle | ||
|
||
import org.junit.jupiter.api.DynamicTest | ||
import org.junit.jupiter.api.TestFactory | ||
|
||
class ModNameProcessorTest { | ||
|
||
@TestFactory | ||
fun `Basic tests`(): List<DynamicTest> { | ||
val sample1 = mapOf( | ||
"modid.name" to "ModName", | ||
"modid.name.special" to "(extra special)" | ||
) | ||
|
||
return listOf( | ||
processorTest( | ||
name = "Discard variant names", | ||
processor = ModNameProcessor("modid", "normal"), | ||
translations = sample1, | ||
result = mapOf( | ||
"modid.name" to "ModName", | ||
"modmenu.nameTranslation.modid" to "ModName" | ||
) | ||
), | ||
processorTest( | ||
name = "Append \"extra special\" to name", | ||
processor = ModNameProcessor("modid", "special"), | ||
translations = sample1, | ||
result = mapOf( | ||
"modid.name" to "ModName (extra special)", | ||
"modmenu.nameTranslation.modid" to "ModName (extra special)" | ||
) | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"freecam.name": "Freecam", | ||
"freecam.name.modrinth": "(Modrinth Edition)", | ||
"freecam.description": "A highly customizable freecam mod.", | ||
"freecam.description.modrinth": "Some features have been restricted to comply with Modrinth's Content Rules." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters