AbstractJacksonLocalizationMapReader

abstract class AbstractJacksonLocalizationMapReader(objectMapper: ObjectMapper, templateFunction: LocalizationTemplateFunction) : LocalizationMapReader(source)

Helps to create a LocalizationMapReader based on an ObjectMapper.

You may supply a LocalizationTemplateFunction to customize the templates being filled, if the default provider is not enough.

Note: For most cases, using JacksonLocalizationMapReader is more than enough.

Example - YAML reader

You must add the jackson-dataformat-yaml dependency, make sure to match the version that JDA uses to avoid incompatibilities.

Kotlin

class YamlLocalizationMapReader(
localizationTemplateFunction: LocalizationTemplateFunction,
private val folderName: String,
) : AbstractJacksonLocalizationMapReader(
ObjectMapper(YAMLFactory()), // Give it a YAMLFactory instead of the default JSONFactory
localizationTemplateFunction,
) {

override fun getInputStream(request: LocalizationMapRequest): InputStream? {
return this.javaClass.getResourceAsStream("/$folderName/${request.bundleName}.yml")
?: this.javaClass.getResourceAsStream("/$folderName/${request.bundleName}.yaml")
}
}

Java

public class YamlLocalizationMapReader extends AbstractJacksonLocalizationMapReader {

private final String folderName;

public YamlLocalizationMapReader(@NotNull LocalizationTemplateFunction templateFunction, @NotNull String folderName) {
super(new ObjectMapper(new YAMLFactory()), templateFunction);
this.folderName = folderName;
}

@Nullable
@Override
public InputStream getInputStream(@NotNull LocalizationMapRequest request) {
final InputStream stream = getClass().getResourceAsStream("/%s/%s.yml".formatted(folderName, request.bundleName()));
if (stream != null) return stream;

return getClass().getResourceAsStream("/%s/%s.yaml".formatted(folderName, request.bundleName()));
}
}

Parameters

objectMapper

Object mapper with support for any data format (such as JSON, YAML and TOML)

templateFunction

Function returning a LocalizationTemplate from the template string and locale

See also

Inheritors

Constructors

Link copied to clipboard
constructor(context: BContext, objectMapper: ObjectMapper)
constructor(objectMapper: ObjectMapper, templateFunction: LocalizationTemplateFunction)

Functions

Link copied to clipboard
@Nonnull
open fun appendPath(@Nonnull path: String, @Nonnull other: String): String

Utility method to append a path component to an existing path, this is simply path + '.' + other.

Link copied to clipboard

Retrieves the InputStream from the requested localization map, may return null if none can be found.

Link copied to clipboard

Reads a LocalizationMap from the requested bundle, returns null if no localization map exists.