Refactor Localizer

This commit is contained in:
CapacitorSet 2019-07-08 19:04:20 +02:00
parent 13325e37cd
commit 7dde252c50

View file

@ -17,14 +17,17 @@
package org.congressodeiradicali.karlmarx
import org.congressodeiradicali.karlmarx.Locales.Locale
object LocalizableString extends Enumeration {
type LocalizableString = Value
val BAN_UNAUTHORIZED, BAN_SUCCESSFUL, BAN_FAILED, BAN_FAILED_INVALID_USER, BAN_FAILED_BAN_ADMIN, BAN_FAILED_REPLY, KICK_UNAUTHORIZED = Value
val NO_SUCH_PLUGIN, PLUGIN_ENABLED, PLUGIN_DISABLED = Value
}
object Locale {
val ENGLISH_LOCALE: Map[LocalizableString.Value, String] = Map(
object Locales {
type Locale = Map[LocalizableString.Value, String]
val ENGLISH_LOCALE: Locale = Map(
(LocalizableString.BAN_UNAUTHORIZED, "Only an administrator can ban users"),
(LocalizableString.BAN_SUCCESSFUL, "User was banned successfully"),
(LocalizableString.BAN_FAILED, "Cannot ban, something went wrong"),
@ -38,7 +41,7 @@ object Locale {
(LocalizableString.PLUGIN_DISABLED, "Plugin disabled")
)
val ITALIAN_LOCALE: Map[LocalizableString.Value, String] = Map(
val ITALIAN_LOCALE: Locale = Map(
(LocalizableString.BAN_UNAUTHORIZED, "Solo un amministratore può bannare gli utenti"),
(LocalizableString.BAN_SUCCESSFUL, "Utente bannato con successo"),
(LocalizableString.BAN_FAILED, "Non sono riuscito a bannare, qualcosa è andato storto"),
@ -53,12 +56,16 @@ object Locale {
)
}
class Localizer(val locale: String) {
def getString(name: LocalizableString.Value): String = locale match {
case "en_EN" => Locale.ENGLISH_LOCALE(name)
case "it_IT" => Locale.ITALIAN_LOCALE(name)
case _ => Locale.ENGLISH_LOCALE(name)
class Localizer(val localeName: String) {
val currentLocale: Locale = localeName match {
case "en_EN" => Locales.ENGLISH_LOCALE
case "it_IT" => Locales.ITALIAN_LOCALE
case _ => throw new RuntimeException(s"No such locale: $localeName")
}
def getString(name: LocalizableString.Value): String = {
if (currentLocale.contains(name))
currentLocale(name)
else
throw new RuntimeException(s"Locale does not implement LocalizableString $name")
}
}