https://github.com/aecsocket/glossa
Localization library for Adventure components
https://github.com/aecsocket/glossa
Last synced: about 1 year ago
JSON representation
Localization library for Adventure components
- Host: GitHub
- URL: https://github.com/aecsocket/glossa
- Owner: aecsocket
- License: mit
- Created: 2023-02-13T22:34:52.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-14T20:53:16.000Z (over 2 years ago)
- Last Synced: 2025-01-05T11:42:56.071Z (about 1 year ago)
- Language: Kotlin
- Size: 229 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Glossa
[](https://github.com/aecsocket/glossa/actions/workflows/build.yml)
[](https://central.sonatype.com/artifact/io.github.aecsocket/glossa)
[](https://central.sonatype.com/artifact/io.github.aecsocket/glossa)
Localization library for Adventure components
### [GitHub](https://github.com/aecsocket/glossa) ยท [Docs](https://aecsocket.github.io/glossa) ยท [Dokka](https://aecsocket.github.io/glossa/dokka)
Glossa provides a simple and opinionated API for developers to create localizable versions of their
software, and provides server admins and translators with tools to create translations based on
powerful and useful features like the MiniMessage format and Unicode ICU templates. It is designed
for Kotlin, with minimal support for Java.
## Usage
See the version badges for the latest release and snapshot builds.
Modules:
- `glossa` - core API
- `glossa-configurate` - tools for the [Configurate](https://github.com/spongepowered/configurate)
library
```kotlin
repositories {
mavenCentral()
// for snapshot builds
// maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
dependencies {
implementation("io.github.aecsocket", "glossa", "VERSION")
}
```
### Setting up Glossa
#### Using the Kotlin DSL
```kotlin
val english = Locale.forLanguageTag("en-US")
val glossa = glossaStandard(
defaultLocale = english,
invalidMessageProvider = InvalidMessageProvider.Default,
) {
substitutions {
substitution("icon_item", Component.text("๐ช"))
}
styles {
style("color_variable", Style.style(NamedTextColor.YELLOW))
}
translation(english) {
message("hello_world", "Hello world!")
messageList("found_item",
" You found !",
" You picked up !",
" You obtained !",
)
section("command") {
message("state", """
System state:
- Foo:
- Bar:
""".trimIndent())
message("players_online", """
There {players, plural,
=0 {are no players}
one {is # player}
other {are # players}
} online.
""".trimIndent())
}
}
}
```
#### Using Configurate
```yaml
substitutions:
icon_item: "๐ช"
styles:
color_variable:
color: yellow
translations:
en-US:
hello_world: "Hello world!"
found_item:
- " You found !"
- " You picked up !"
- " You obtained !"
command:
# `|-`: preserve newlines, remove last newline
state: |-
System state:
- Foo:
- Bar:
# `>-`: remove newlines, remove last newline
players_online: >-
There {players, plural,
=0 {are no players}
one {is # player}
other {are # players}
} online.
```
```kotlin
val glossa = glossaStandard(
defaultLocale = english,
invalidMessageProvider = InvalidMessageProvider.Default,
) {
try {
fromConfigLoader(YamlConfigurationLoader.builder()
.file(myLangFile)
.build()
)
} catch (ex: Exception) {
ex.printStackTrace()
}
}
```
### Generating messages
#### Using method generation calls
```kotlin
// typealias Message = List
val message: Message = glossa.message("hello_world")
// [ "Hello world!" ]
val foundItem: List = glossa.messageList("found_item") {
replace("item_name", Component.text("Epic Sword", NamedTextColor.BLUE))
}
// [
// "๐ช You found Epic Sword!",
// "๐ช You picked up Epic Sword!",
// "๐ช You obtained Epic Sword!"
// ]
```
#### Using a message proxy
```kotlin
interface MyMessages {
// @MessageKey("hello_world") // optional, explicitly specify message key
fun helloWorld(): Message
fun foundItem(
// @Placeholder("item_name") // optional, explicitly specify placeholder key
itemName: Component,
): List
// @SectionKey("command") // optional, explicitly specify subsection key
val command: Command
interface Command {
fun state(
foo: Component,
bar: Component,
): Message
fun playersOnline(
players: Int
): Message
}
}
val messages: MessageProxy = glossa.messageProxy()
val commandOutput: Message = messages.forLocale(english).command.state(
foo = Component.text("OK"),
bar = Component.text("ERROR"),
)
// [
// "System state:",
// "- Foo: OK",
// "- Bar: ERROR"
// ]
```