Skip to content

Commit 4c71b8f

Browse files
author
Francisco Solis
committed
Moved Missing Class
1 parent c8712ab commit 4c71b8f

2 files changed

Lines changed: 92 additions & 58 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package xyz.theprogramsrc.translationsmodule
2+
3+
import xyz.theprogramsrc.filesmodule.config.YmlConfig
4+
import xyz.theprogramsrc.filesmodule.utils.folder
5+
import java.io.File
6+
7+
/**
8+
* Representation of a translation.
9+
* @param id The id of the translation
10+
* @param defaultValue The default value of the translation.
11+
* @param group The group (folder) of the translation. Defaults to "common"
12+
* @param language The language of the translation. (Defaults to "en")
13+
* @param mainColor The main color of the translation. (Defaults to null)
14+
* @param colors The colors to use in the translation replacing strings. Example (using color '&c'): '**test**' should return '&ctest'. Defaults to empty array.
15+
* @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)
16+
*/
17+
data class Translation(
18+
val id: String,
19+
val defaultValue: String,
20+
val group: String = "common",
21+
val language: String = "en",
22+
val mainColor: String? = null,
23+
val colors: Array<String> = emptyArray(),
24+
val autoRegister: Boolean = true
25+
) {
26+
27+
init {
28+
if(autoRegister) {
29+
TranslationManager.instance.registerTranslations(group, this)
30+
}
31+
}
32+
33+
/**
34+
* Translates this [Translation] to the current language.
35+
* @param language The language of the translation. Set to null to use the default language. Defaults to null
36+
* @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'.
37+
* You can use '{}' or '%%' as placeholder identifiers like '{test}' or '%test%'. Defaults to empty map.
38+
* @return The translated string.
39+
*/
40+
fun translate(language: String? = null, placeholders: Map<String, String> = emptyMap()): String {
41+
val file = YmlConfig(File(File("translations/${if(group.endsWith("/")) group else "$group/"}").folder(), (language ?: TranslationManager.getCurrentLanguage()) + ".lang")) // Get the file of the translation
42+
val mainColor = this.mainColor ?: "" // Get the main color of the translation
43+
var translation = mainColor.plus(
44+
if(file.has(id)) { // If the translation exists
45+
file.getString(id) // Get the translation from the file
46+
} else { // If the translation doesn't exist
47+
defaultValue // Get the default value
48+
}
49+
)
50+
for(i in colors.indices) { // For each color
51+
try {
52+
val color = colors[i] // Get the color
53+
val string = Regex("\\*\\*(.+?)\\*\\*").findAll(translation).first().groupValues[1] // Get the string to replace
54+
translation = translation.replaceFirst("**$string**", "$color$string$mainColor") // Replace the first match with the colorized string
55+
}catch (_: Exception){} // Ignore errors
56+
}
57+
58+
placeholders.forEach { (key, value) -> // For each placeholder
59+
translation = translation.replace("{$key}", value).replace("%$key%", value) // Replace the placeholder using %% and {}
60+
}
61+
62+
return translation // Return the translated string
63+
}
64+
65+
override fun equals(other: Any?): Boolean {
66+
if (this === other) return true
67+
if (javaClass != other?.javaClass) return false
68+
69+
other as Translation
70+
71+
if (id != other.id) return false
72+
if (defaultValue != other.defaultValue) return false
73+
if (!colors.contentEquals(other.colors)) return false
74+
if (group != other.group) return false
75+
if (language != other.language) return false
76+
if (mainColor != other.mainColor) return false
77+
if (autoRegister != other.autoRegister) return false
78+
79+
return true
80+
}
81+
82+
override fun hashCode(): Int {
83+
var result = id.hashCode()
84+
result = 31 * result + defaultValue.hashCode()
85+
result = 31 * result + colors.contentHashCode()
86+
result = 31 * result + group.hashCode()
87+
result = 31 * result + language.hashCode()
88+
result = 31 * result + (mainColor?.hashCode() ?: 0)
89+
result = 31 * result + autoRegister.hashCode()
90+
return result
91+
}
92+
}

src/main/kotlin/xyz/theprogramsrc/translationsmodule/objects/Translation.kt

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)