From 7dde252c50e8e71d487e06b127db2381dbb5f8ef Mon Sep 17 00:00:00 2001 From: CapacitorSet <9286933+CapacitorSet@users.noreply.github.com> Date: Mon, 8 Jul 2019 19:04:20 +0200 Subject: [PATCH] Refactor Localizer --- .../karlmarx/Localizer.scala | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/main/scala/org/congressodeiradicali/karlmarx/Localizer.scala b/src/main/scala/org/congressodeiradicali/karlmarx/Localizer.scala index 54e7d2c..4926996 100644 --- a/src/main/scala/org/congressodeiradicali/karlmarx/Localizer.scala +++ b/src/main/scala/org/congressodeiradicali/karlmarx/Localizer.scala @@ -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") } - }