An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# Glossa
[![CI](https://img.shields.io/github/actions/workflow/status/aecsocket/glossa/build.yml)](https://github.com/aecsocket/glossa/actions/workflows/build.yml)
[![Release](https://img.shields.io/maven-central/v/io.github.aecsocket/glossa?label=release)](https://central.sonatype.com/artifact/io.github.aecsocket/glossa)
[![Snapshot](https://img.shields.io/nexus/s/io.github.aecsocket/glossa?label=snapshot&server=https%3A%2F%2Fs01.oss.sonatype.org)](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"
// ]
```