Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.io.FilenameFilter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
Expand All @@ -68,6 +69,7 @@ public class PluginAwareResourceBundleMessageSource extends ReloadableResourceBu
private long pluginCacheMillis = Long.MIN_VALUE;
private boolean searchClasspath = false;
private String messageBundleLocationPattern = "classpath*:*.properties";
String[] basenamesDefinition = {};

public PluginAwareResourceBundleMessageSource() {
}
Expand All @@ -89,6 +91,12 @@ public void setResourceResolver(PathMatchingResourcePatternResolver resourceReso
this.resourceResolver = resourceResolver;
}

@Override
public void setBasenames(String... basenames) {
basenamesDefinition = basenames;
super.setBasenames(basenames);
}

public void afterPropertiesSet() throws Exception {
if (pluginCacheMillis == Long.MIN_VALUE) {
pluginCacheMillis = cacheMillis;
Expand Down Expand Up @@ -151,8 +159,10 @@ public boolean accept(File dir, String name) {
basenames.add(baseName);
}

setBasenames(basenames.toArray( new String[basenames.size()]));

List<String> mergedBasenames = new ArrayList<>(Arrays.asList(basenamesDefinition));
basenames.removeAll(mergedBasenames);
mergedBasenames.addAll(basenames);
super.setBasenames(mergedBasenames.toArray(new String[0]));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ class ResourceBundleMessageSourceSpec extends Specification {
void setup(){
messages = new TestResource('messages.properties','''\
foo=bar
shared.message=Messages Message
'''.stripIndent().getBytes('UTF-8'))

other = new TestResource('other.properties','''\
bar=foo
shared.message=Other Message
'''.stripIndent().getBytes('UTF-8'))
}

Expand All @@ -49,11 +51,27 @@ class ResourceBundleMessageSourceSpec extends Specification {
messageSource.setBasenames('messages','other')
def locale = Locale.default
expect:
messageSource.getBundleCodes(locale,'messages') == (['foo'] as Set)
messageSource.getBundleCodes(locale,'other') == (['bar'] as Set)
messageSource.getBundleCodes(locale,'messages','other') == (['foo','bar'] as Set)
messageSource.getBundleCodes(locale,'messages') == (['foo', 'shared.message'] as Set)
messageSource.getBundleCodes(locale,'other') == (['bar', 'shared.message'] as Set)
messageSource.getBundleCodes(locale,'messages','other') == (['foo','bar', 'shared.message'] as Set)
}

void 'Check method to verify ResourceBundle ordering prioritizes application over plugin messages'(){
given:
def messageSource = new ReloadableResourceBundleMessageSource(
resourceLoader: new DefaultResourceLoader(){
Resource getResourceByPath(String path){
path.startsWith('messages') ? messages:other
}
}
)
messageSource.setBasenames('other', 'messages')
def locale = Locale.default
expect: "other messages override plugin messages"
messageSource.getMessage('shared.message', null, locale) == 'Other Message'
messageSource.getMessage('foo', null, locale) == 'bar'
}

class TestResource extends ByteArrayResource{
String filename

Expand Down
Loading