Skip to content

Commit

Permalink
Merge pull request #14 from TheProgramSrc/feat/translation-colors
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Solis authored Mar 28, 2022
2 parents a268845 + 1348afb commit 9c684a7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# v0.1.5 - Snapshot
* Now translations are automatically colorized (unless you set `colorize` to `false`)
* Updated tests

# v0.1.4 - Snapshot
* Updated SimpleCoreAPI to v0.3.0-SNAPSHOT
* Now the TranslationManager is initialized at the load stage of the
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id 'org.jetbrains.dokka' version '1.6.10'
}

def projectVersion = (System.getenv("VERSION") ?: '0.1.4-SNAPSHOT').replaceFirst("v", "").replace('/', '')
def projectVersion = (System.getenv("VERSION") ?: '0.1.5-SNAPSHOT').replaceFirst("v", "").replace('/', '')

group 'xyz.theprogramsrc'
version projectVersion
Expand All @@ -31,7 +31,7 @@ dependencies {
compileOnly 'org.jetbrains.kotlin:kotlin-stdlib:1.6.10'
compileOnly 'org.spigotmc:spigot-api:1.18.1-R0.1-SNAPSHOT'
compileOnly 'net.md-5:bungeecord-api:1.18-R0.1-SNAPSHOT'
compileOnly 'xyz.theprogramsrc:simplecoreapi:0.3.0-SNAPSHOT'
compileOnly 'xyz.theprogramsrc:simplecoreapi:0.3.1-SNAPSHOT'
compileOnly 'xyz.theprogramsrc:filesmodule:0.1.1-SNAPSHOT'

testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ data class Translation(
* You can use '{}' or '%%' as placeholder identifiers like '{test}' or '%test%'. Defaults to empty map.
* @return The translated string.
*/
fun translate(language: String? = null, placeholders: Map<String, String> = emptyMap()): String {
fun translate(language: String? = null, placeholders: Map<String, String> = emptyMap(), colorize: Boolean = true): String {
val file = YmlConfig(File(File("translations/${if(group.endsWith("/")) group else "$group/"}").folder(), (language ?: TranslationManager.getCurrentLanguage()) + ".lang")) // Get the file of the translation
val mainColor = this.mainColor ?: "" // Get the main color of the translation
var translation = mainColor.plus(
Expand All @@ -59,7 +59,11 @@ data class Translation(
translation = translation.replace("{$key}", value).replace("%$key%", value) // Replace the placeholder using %% and {}
}

return translation // Return the translated string
return if(colorize) { // Return the translated string
translation.replace("&", "§")
} else {
translation
}
}

override fun equals(other: Any?): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class TranslationColorTest {
defaultValue = "defaultValue",
mainColor = "&7"
)
assertEquals("&7defaultValue", translation.translate())
assertEquals("&7defaultValue", translation.translate(colorize=false))
}

@Test
Expand All @@ -33,7 +33,7 @@ internal class TranslationColorTest {
colors = arrayOf("&c"),
mainColor = "&7"
)
assertEquals("&7defaultValue is &ccolorized&7.", translation.translate())
assertEquals("&7defaultValue is &ccolorized&7.", translation.translate(colorize=false))
}

@Test
Expand All @@ -44,7 +44,7 @@ internal class TranslationColorTest {
colors = arrayOf("&c", "&a"),
mainColor = "&7"
)
assertEquals("&7defaultValue is &ccolorized&7 and &acolorized&7.", translation.translate())
assertEquals("&7defaultValue is &ccolorized&7 and &acolorized&7.", translation.translate(colorize=false))

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.io.File

internal class TranslationTest {
internal class TranslationManagerTest {

companion object {
@BeforeAll
Expand Down

0 comments on commit 9c684a7

Please sign in to comment.