-
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
Showing
39 changed files
with
536 additions
and
132 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
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
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
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
30 changes: 30 additions & 0 deletions
30
...eso-api/src/main/kotlin/org/progreso/api/command/argument/arguments/LocaleArgumentType.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,30 @@ | ||
package org.progreso.api.command.argument.arguments | ||
|
||
import org.progreso.api.Api | ||
import org.progreso.api.command.argument.ArgumentType | ||
import org.progreso.api.command.reader.StringReader | ||
import org.progreso.api.i18n.I18n | ||
|
||
class LocaleArgumentType : ArgumentType<I18n.Locale?> { | ||
companion object { | ||
fun create() = LocaleArgumentType() | ||
} | ||
|
||
override val name = "locale" | ||
|
||
override fun parse(reader: StringReader): I18n.Locale? { | ||
val localeName = reader.readString() | ||
val locale = I18n.getLocaleByNameOrNull(localeName) | ||
if (locale == null) { | ||
Api.CHAT.errorLocalized( | ||
"argument.locale.error", | ||
"locale" to localeName | ||
) | ||
} | ||
return locale | ||
} | ||
|
||
override fun check(reader: StringReader): Boolean { | ||
return true | ||
} | ||
} |
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
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
progreso-api/src/main/kotlin/org/progreso/api/i18n/I18n.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 org.progreso.api.i18n | ||
|
||
import org.progreso.api.Api.GSON | ||
import org.progreso.api.i18n.exceptions.I18nException | ||
import java.io.InputStream | ||
|
||
object I18n { | ||
lateinit var locales: List<Locale> | ||
lateinit var locale: Locale | ||
|
||
fun initialize(locales: List<Locale>, locale: String) { | ||
this.locales = locales | ||
this.locale = locales.first { it.name == locale } | ||
} | ||
|
||
fun getLocaleByNameOrNull(name: String): Locale? { | ||
return locales.firstOrNull { it.name == name } | ||
} | ||
|
||
@JvmStatic | ||
fun i18n(key: String, vararg replacements: Pair<String, Any>): String { | ||
val value = locale[key] | ||
?: throw I18nException("Internationalization of $key not found in ${locale.name} dictionary") | ||
|
||
return replace(value, *replacements) | ||
} | ||
|
||
private fun replace(value: String, vararg replacements: Pair<String, Any>): String { | ||
var result = value | ||
replacements.forEach { result = result.replace("{{${it.first}}}", it.second.toString()) } | ||
return result | ||
} | ||
|
||
data class Locale( | ||
val name: String, | ||
private val dictionary: Map<String, String> | ||
) { | ||
companion object { | ||
fun fromStream(name: String, stream: InputStream): Locale { | ||
return Locale(name, GSON.fromJson<Map<String, String>>(stream.reader(), Map::class.java)) | ||
} | ||
} | ||
|
||
operator fun get(key: String): String? { | ||
return dictionary[key] | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
progreso-api/src/main/kotlin/org/progreso/api/i18n/exceptions/I18nException.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,3 @@ | ||
package org.progreso.api.i18n.exceptions | ||
|
||
class I18nException(message: String) : Exception(message) |
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
Oops, something went wrong.