yaml-resource-bundle
yaml-resource-bundle copied to clipboard
Java ResourceBundle for YAML format.
yaml-resource-bundle
Java ResourceBundle for YAML format.
Features
- Accesses YAML-formatted resources via ResourceBundle.
- Supports locale-specific resources according to the ResourceBundle specification.
- Supports YAML values nested in a map or list.
- Supports YAML anchors and aliases indicated by
&and*. - Supports multiple YAML documents separated by
---.
Dependencies
Depends on:
- Java 8, 11, 17 or 21
- Kotlin 1.9
- SnakeYAML 2.2
Usage
Adding the Dependency
The artifact is published on Maven Central Repository. If you are using Maven, add the following dependency.
<dependency>
<groupId>dev.akkinoc.util</groupId>
<artifactId>yaml-resource-bundle</artifactId>
<version>${yaml-resource-bundle.version}</version>
</dependency>
Creating Resource Files
Create a YAML-formatted resource file on the classpath. Also, create locale-specific resource files as needed.
For example (resource.yml, resource_en.yml, resource_fr.yml, etc):
fruits:
apple: Apple
orange: Orange
grape: Grape
colors:
- Red
- Orange
- Purple
Accessing the Resource
Access the resource via YamlResourceBundle.
For example in Java:
import dev.akkinoc.util.YamlResourceBundle;
// Gets the resource bundle
// YamlResourceBundle.Control is specified for ResourceBundle.Control
ResourceBundle bundle = ResourceBundle.getBundle(
"resource", YamlResourceBundle.Control.INSTANCE);
// Gets the map values
System.out.println(bundle.getString("fruits.apple")); // => "Apple" or localized value
System.out.println(bundle.getString("fruits.orange")); // => "Orange" or localized value
System.out.println(bundle.getString("fruits.grape")); // => "Grape" or localized value
// Gets the list values
System.out.println(bundle.getString("colors[0]")); // => "Red" or localized value
System.out.println(bundle.getString("colors[1]")); // => "Orange" or localized value
System.out.println(bundle.getString("colors[2]")); // => "Purple" or localized value
// Gets the list values as an array
System.out.println(Arrays.toString(bundle.getStringArray("colors")));
// => "[Red, Orange, Purple]" or localized values
// Gets the all keys
System.out.println(bundle.keySet());
// => "[fruits.apple, fruits.orange, fruits.grape,
// colors, colors[0], colors[1], colors[2]]" (not sorted)
API Reference
Please refer to the Javadoc.
Release Notes
Please refer to the Releases page.
License
Licensed under the Apache License, Version 2.0.
Support the Project
If this project is useful to you, I appreciate giving a ⭐ star to this repository. I would also appreciate if you would consider 💖 sponsoring as well. Your support is my biggest motive force. Thanks ✨