-
-
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.
Merge pull request #3 from TheProgramSrc/feat/main-color
- Loading branch information
Showing
9 changed files
with
222 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,11 @@ jobs: | |
with: | ||
name: TranslationsModule | ||
path: ./build/libs/TranslationsModule-${{ env.VERSION }}.jar | ||
# Here we upload the binary to the release | ||
- name: Upload to release | ||
uses: JasonEtco/upload-to-release@master | ||
with: | ||
args: ./build/libs/TranslationsModule-${{ env.VERSION }}.jar application/java-archive | ||
# Now we deploy the documents to GitHub pages | ||
- name: Deploy Dokka | ||
uses: JamesIves/[email protected] | ||
|
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
92 changes: 92 additions & 0 deletions
92
src/main/kotlin/xyz/theprogramsrc/translationsmodule/Translation.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,92 @@ | ||
package xyz.theprogramsrc.translationsmodule | ||
|
||
import xyz.theprogramsrc.filesmodule.config.YmlConfig | ||
import xyz.theprogramsrc.filesmodule.utils.folder | ||
import java.io.File | ||
|
||
/** | ||
* Representation of a translation. | ||
* @param id The id of the translation | ||
* @param defaultValue The default value of the translation. | ||
* @param group The group (folder) of the translation. Defaults to "common" | ||
* @param language The language of the translation. (Defaults to "en") | ||
* @param mainColor The main color of the translation. (Defaults to null) | ||
* @param colors The colors to use in the translation replacing strings. Example (using color '&c'): '**test**' should return '&ctest'. Defaults to empty array. | ||
* @param autoRegister If the translation should be automatically registered. (Defaults to true) It is recommended to disable if you're going to initialize the same translation multiple times (for example inside a loop) | ||
*/ | ||
data class Translation( | ||
val id: String, | ||
val defaultValue: String, | ||
val group: String = "common", | ||
val language: String = "en", | ||
val mainColor: String? = null, | ||
val colors: Array<String> = emptyArray(), | ||
val autoRegister: Boolean = true | ||
) { | ||
|
||
init { | ||
if(autoRegister) { | ||
TranslationManager.instance.registerTranslations(group, this) | ||
} | ||
} | ||
|
||
/** | ||
* Translates this [Translation] to the current language. | ||
* @param language The language of the translation. Set to null to use the default language. Defaults to null | ||
* @param placeholders The placeholders to use in the translation replacing strings. Example (using placeholder id 'test' and value 'test_value'): '{test}' should return 'test_value'. | ||
* 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 { | ||
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( | ||
if(file.has(id)) { // If the translation exists | ||
file.getString(id) // Get the translation from the file | ||
} else { // If the translation doesn't exist | ||
defaultValue // Get the default value | ||
} | ||
) | ||
for(i in colors.indices) { // For each color | ||
try { | ||
val color = colors[i] // Get the color | ||
val string = Regex("\\*\\*(.+?)\\*\\*").findAll(translation).first().groupValues[1] // Get the string to replace | ||
translation = translation.replaceFirst("**$string**", "$color$string$mainColor") // Replace the first match with the colorized string | ||
}catch (_: Exception){} // Ignore errors | ||
} | ||
|
||
placeholders.forEach { (key, value) -> // For each placeholder | ||
translation = translation.replace("{$key}", value).replace("%$key%", value) // Replace the placeholder using %% and {} | ||
} | ||
|
||
return translation // Return the translated string | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) return true | ||
if (javaClass != other?.javaClass) return false | ||
|
||
other as Translation | ||
|
||
if (id != other.id) return false | ||
if (defaultValue != other.defaultValue) return false | ||
if (!colors.contentEquals(other.colors)) return false | ||
if (group != other.group) return false | ||
if (language != other.language) return false | ||
if (mainColor != other.mainColor) return false | ||
if (autoRegister != other.autoRegister) return false | ||
|
||
return true | ||
} | ||
|
||
override fun hashCode(): Int { | ||
var result = id.hashCode() | ||
result = 31 * result + defaultValue.hashCode() | ||
result = 31 * result + colors.contentHashCode() | ||
result = 31 * result + group.hashCode() | ||
result = 31 * result + language.hashCode() | ||
result = 31 * result + (mainColor?.hashCode() ?: 0) | ||
result = 31 * result + autoRegister.hashCode() | ||
return result | ||
} | ||
} |
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
58 changes: 0 additions & 58 deletions
58
src/main/kotlin/xyz/theprogramsrc/translationsmodule/objects/Translation.kt
This file was deleted.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
src/test/kotlin/xyz/theprogramsrc/translationsmodule/TranslationColorTest.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,50 @@ | ||
package xyz.theprogramsrc.translationsmodule | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class TranslationColorTest { | ||
|
||
companion object { | ||
@BeforeAll | ||
@JvmStatic | ||
fun setUp() { | ||
TranslationManager() | ||
} | ||
|
||
} | ||
|
||
@Test | ||
fun testMainColorTranslation(){ | ||
val translation = Translation( | ||
id = "identifier", | ||
defaultValue = "defaultValue", | ||
mainColor = "&7" | ||
) | ||
assertEquals("&7defaultValue", translation.translate()) | ||
} | ||
|
||
@Test | ||
fun testMainColorWithColorizedText(){ | ||
val translation = Translation( | ||
id = "identifier2", | ||
defaultValue = "defaultValue is **colorized**.", | ||
colors = arrayOf("&c"), | ||
mainColor = "&7" | ||
) | ||
assertEquals("&7defaultValue is &ccolorized&7.", translation.translate()) | ||
} | ||
|
||
@Test | ||
fun testMainColorWithMultipleColorizedTexts(){ | ||
val translation = Translation( | ||
id = "identifier3", | ||
defaultValue = "defaultValue is **colorized** and **colorized**.", | ||
colors = arrayOf("&c", "&a"), | ||
mainColor = "&7" | ||
) | ||
assertEquals("&7defaultValue is &ccolorized&7 and &acolorized&7.", translation.translate()) | ||
|
||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
src/test/kotlin/xyz/theprogramsrc/translationsmodule/TranslationManagerTest.kt
This file was deleted.
Oops, something went wrong.
49 changes: 49 additions & 0 deletions
49
src/test/kotlin/xyz/theprogramsrc/translationsmodule/TranslationTest.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,49 @@ | ||
package xyz.theprogramsrc.translationsmodule | ||
|
||
import org.junit.jupiter.api.Assertions.assertFalse | ||
import org.junit.jupiter.api.Assertions.assertTrue | ||
import org.junit.jupiter.api.BeforeAll | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.io.File | ||
|
||
internal class TranslationTest { | ||
|
||
companion object { | ||
@BeforeAll | ||
@JvmStatic | ||
fun setUp() { | ||
TranslationManager() | ||
} | ||
} | ||
|
||
@BeforeEach | ||
fun beforeEach() { | ||
File("translations").deleteRecursively() | ||
} | ||
|
||
@Test | ||
fun testAutoRegister() { | ||
val translation = Translation( | ||
id = "identifier", | ||
defaultValue = "defaultValue", | ||
) | ||
val folder = File("translations/${translation.group}") | ||
val file = File(folder, "${translation.language}.lang") | ||
assertTrue(file.exists()) | ||
} | ||
|
||
@Test | ||
fun testManualRegistration() { | ||
val translation = Translation( | ||
id = "identifier2", | ||
defaultValue = "defaultValue", | ||
autoRegister = false, | ||
) | ||
val folder = File("translations/${translation.group}") | ||
val file = File(folder, "${translation.language}.lang") | ||
assertFalse(file.exists()) | ||
TranslationManager.instance.registerTranslation(translation = translation) | ||
assertTrue(file.exists()) | ||
} | ||
} |