[
https://issues.apache.org/jira/browse/WW-3452?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18125826#comment-18125826
]
Lukasz Lenart edited comment on WW-3452 at 10/10/26 12:38 PM:
--------------------------------------------------------------
Closing this as Won't Fix. Wildcard bundle names won't be added to core:
classpath-scan order varies between containers and between jar orders in
{{WEB-INF/lib}}, so when two matching bundles share a key, the message shown
would change between deployments. A pattern such as {{com.acme.messages.*}}
also leaves open whether sub-packages are included and how locale variants fold
back into a base name. Struts would have to fix and support answers to all of
these.
An application can already do this without any framework change.
{{LocalizedTextProvider}} is a pluggable bean, and {{setCustomI18NResources}}
can be overridden to expand the patterns before passing them on:
{code:java}public class WildcardLocalizedTextProvider extends
StrutsLocalizedTextProvider {
@Override
public void setCustomI18NResources(String bundles) { // no @Inject on the
override
super.setCustomI18NResources(expandWildcards(bundles));
}
}
{code}
{code:xml}<bean type="org.apache.struts2.text.LocalizedTextProvider"
name="wildcard"
class="com.acme.WildcardLocalizedTextProvider" scope="singleton"/>
<constant name="struts.localizedTextProvider" value="wildcard"/>
{code}
Leave {{@Inject}} off the override. The parent's annotation already makes the
container call the method, and annotating the override as well makes it run
twice. Sort the expanded names so lookup order stays the same across
deployments: bundles registered later are searched first.
A complete working example, with tests, is in the {{text-provider}} app of
struts-examples:
[WildcardLocalizedTextProvider.java|https://github.com/apache/struts-examples/blob/main/text-provider/src/main/java/org/apache/struts_example/WildcardLocalizedTextProvider.java]
was (Author: lukaszlenart):
Closing this as Won't Fix. Wildcard bundle names won't be added to core:
classpath-scan order varies between containers and between jar orders in
{{WEB-INF/lib}}, so when two matching bundles share a key, the message shown
would change between deployments. A pattern such as {{com.acme.messages.*}}
also leaves open whether sub-packages are included and how locale variants fold
back into a base name. Struts would have to fix and support answers to all of
these.
An application can already do this without any framework change.
{{LocalizedTextProvider}} is a pluggable bean, and {{setCustomI18NResources}}
can be overridden to expand the patterns before passing them on:
{code:java}public class WildcardLocalizedTextProvider extends
StrutsLocalizedTextProvider {
@Override
public void setCustomI18NResources(String bundles) { // no @Inject on the
override
super.setCustomI18NResources(expandWildcards(bundles));
}
}
{code}
{code:xml}<bean type="org.apache.struts2.text.LocalizedTextProvider"
name="wildcard"
class="com.acme.WildcardLocalizedTextProvider" scope="singleton"/>
<constant name="struts.localizedTextProvider" value="wildcard"/>
{code}
Leave {{@Inject}} off the override. The parent's annotation already makes the
container call the method, and annotating the override as well makes it run
twice. Sort the expanded names so lookup order stays the same across
deployments: bundles registered later are searched first.
A complete working example, with tests, is in the {{text-provider}} app of
struts-examples:
https://github.com/apache/struts-examples/blob/main/text-provider/src/main/java/org/apache/struts\_example/WildcardLocalizedTextProvider.java
> let struts.custom.i18n.resources supports wildcard
> --------------------------------------------------
>
> Key: WW-3452
> URL: https://issues.apache.org/jira/browse/WW-3452
> Project: Struts 2
> Issue Type: Improvement
> Reporter: Yanming Zhou
> Priority: Major
> Labels: message, resource
> Fix For: 7.5.0
>
> Original Estimate: 4h
> Time Spent: 20m
> Remaining Estimate: 3h 40m
>
> {code:xml}
> <constant name="struts.custom.i18n.resources"
> value="resources.i18n.messages,com.acme.messages.*" />
> {code}
> following code is my uncompleted patch,I want to know how to scan wildcard
> resources using xwork/struts2 util class.
> org.apache.struts2.config.BeanSelectionProvider line:245
> {code:java}
>
> LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
> String bundles =
> props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES);
> if (bundles != null && bundles.length() > 0) {
> StringTokenizer customBundles = new
> StringTokenizer(props.getProperty(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES),
> ", ");
> while (customBundles.hasMoreTokens()) {
> String name = customBundles.nextToken();
> // add start
> if(name.contains("*")){
> Collection<String> resources = new HashSet<String>();
> //TODO scan resources from classpath
> for(String res : resources)
> try {
> LOG.info("Loading global messages from " + res);
> LocalizedTextUtil.addDefaultResourceBundle(res);
> } catch (Exception e) {
> LOG.error("Could not find messages file " + res +
> ".properties. Skipping");
> }
> continue;
> }
> // add end
> try {
> LOG.info("Loading global messages from " + name);
> LocalizedTextUtil.addDefaultResourceBundle(name);
> } catch (Exception e) {
> LOG.error("Could not find messages file " + name +
> ".properties. Skipping");
> }
> }
> }
> {code}
--
This message was sent by Atlassian Jira
(v8.20.10#820010)