-
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.
- Loading branch information
1 parent
74cfadf
commit a3cc6bf
Showing
4 changed files
with
102 additions
and
2 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
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/cc/modlabs/kpaper/extensions/TranslationExtension.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,48 @@ | ||
package cc.modlabs.kpaper.extensions | ||
|
||
import cc.modlabs.kpaper.translation.Translations | ||
import org.bukkit.command.CommandSender | ||
import org.bukkit.entity.Player | ||
import java.util.* | ||
|
||
fun getUnknownTranslation(key: String): String { | ||
return "<hover:show_text:'Click to copy'><click:suggest_command:'$key'>$key</click></hover>" | ||
} | ||
|
||
val String.minecraftTranslated | ||
get() = "<lang:$this>" | ||
|
||
|
||
fun String.toTranslated(player: Player, placeholders: Map<String, Any?> = mapOf(), overwriteLang: String? = null): String { | ||
return Translations.getTranslation( | ||
overwriteLang ?: player.locale().code, this, placeholders) ?: getUnknownTranslation(this) | ||
} | ||
|
||
fun String.toTranslated(commandSender: CommandSender, placeholders: Map<String, Any?> = mapOf()): String { | ||
return if (commandSender is Player) { | ||
toTranslated(commandSender, placeholders) | ||
} else { | ||
Translations.getTranslation("en_US", this, placeholders) ?: getUnknownTranslation(this) | ||
} | ||
} | ||
|
||
fun Player.translate(messageKey: String, placeholders: Map<String, Any?> = mapOf()): String { | ||
return messageKey.toTranslated(this, placeholders) | ||
} | ||
|
||
fun CommandSender.translate(messageKey: String, placeholders: Map<String, Any?> = mapOf()): String { | ||
return messageKey.toTranslated(this, placeholders) | ||
} | ||
|
||
val Locale.code: String | ||
get() = this.language + (if (this.country.isNotEmpty()) "_$country" else "") | ||
|
||
fun String.toMinecraftTranslated(vararg args: Any): String { | ||
return "<lang:$this${ | ||
if (args.isNotEmpty()) { | ||
args.joinToString(separator = ":", prefix = ":") | ||
} else { | ||
"" | ||
} | ||
}>" | ||
} |
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
42 changes: 42 additions & 0 deletions
42
src/main/kotlin/cc/modlabs/kpaper/translation/Translations.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,42 @@ | ||
package cc.modlabs.kpaper.translation | ||
|
||
import cc.modlabs.kpaper.translation.interfaces.TranslationHook | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
|
||
object Translations { | ||
|
||
lateinit var manager: TranslationManager | ||
private val translationHooks = arrayListOf<TranslationHook>() | ||
|
||
private fun loadTranslations(callback: ((Map<String, Int>) -> Unit)? = null) { | ||
CoroutineScope(Dispatchers.Default).launch { | ||
manager.loadTranslations(callback) | ||
} | ||
} | ||
|
||
fun registerTranslationHook(hook: TranslationHook) { | ||
translationHooks.add(hook) | ||
} | ||
|
||
fun getTranslation(language: String, key: String, placeholders: Map<String, Any?> = mapOf()): String? { | ||
if (!::manager.isInitialized) return null | ||
|
||
var translation = manager.get(language, key, placeholders)?.message ?: return null | ||
|
||
for (hook in translationHooks) { | ||
translation = hook.onHandleTranslation(language, key, placeholders, translation) | ||
} | ||
|
||
return translation | ||
} | ||
|
||
val translations = manager | ||
|
||
fun load(translationManager: TranslationManager) { | ||
manager = translationManager | ||
|
||
loadTranslations() | ||
} | ||
} |