Abstract Jackson Localization Map Reader
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
Object mapper with support for any data format (such as JSON, YAML and TOML)
Function returning a LocalizationTemplate from the template string and locale
See also
Inheritors
Constructors
Constructs a AbstractJacksonLocalizationMapReader with the default LocalizationTemplateFunction.
Functions
Utility method to append a path component to an existing path, this is simply path + '.' + other
.
Retrieves the InputStream from the requested localization map, may return null
if none can be found.
Reads a LocalizationMap from the requested bundle, returns null
if no localization map exists.