Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #112 from vt-middleware/resource-bundle-support
Add support for resource bundles.
- Loading branch information
Showing
6 changed files
with
69 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/main/java/org/passay/ResourceBundleMessageResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* See LICENSE for licensing and NOTICE for copyright. */ | ||
package org.passay; | ||
|
||
import java.util.MissingResourceException; | ||
import java.util.ResourceBundle; | ||
|
||
/** | ||
* Resolves messages from rule result details from a resource bundle. Uses {@link ResourceBundle#getBundle(String)} to | ||
* load the default bundle. | ||
* | ||
* @author Middleware Services | ||
*/ | ||
public class ResourceBundleMessageResolver extends AbstractMessageResolver | ||
{ | ||
|
||
/** Maps locale specific message keys to message strings. */ | ||
private final ResourceBundle resourceBundle; | ||
|
||
|
||
/** Creates a new message resolver with the default message map. */ | ||
public ResourceBundleMessageResolver() | ||
{ | ||
this(getDefaultBundle()); | ||
} | ||
|
||
|
||
/** | ||
* Creates a new message resolver with the supplied resource bundle. | ||
* | ||
* @param bundle locale specific map of keys to messages. | ||
*/ | ||
public ResourceBundleMessageResolver(final ResourceBundle bundle) | ||
{ | ||
if (bundle == null) { | ||
throw new IllegalArgumentException("Bundle cannot be null."); | ||
} | ||
resourceBundle = bundle; | ||
} | ||
|
||
|
||
@Override | ||
protected String getMessage(final String key) | ||
{ | ||
try { | ||
return resourceBundle.getString(key); | ||
} catch (MissingResourceException e) { | ||
return null; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Returns the default resource bundle which is found in passay.properties. | ||
* | ||
* @return default resource bundle. | ||
*/ | ||
public static ResourceBundle getDefaultBundle() | ||
{ | ||
return ResourceBundle.getBundle("passay"); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.