You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Greg Huber (Jira)" <ji...@apache.org> on 2022/01/21 10:11:00 UTC

[jira] [Commented] (WW-5112) Add ability (control flag) for TextProviders to prioritize reads from the default resource bundlest.

    [ https://issues.apache.org/jira/browse/WW-5112?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17479970#comment-17479970 ] 

Greg Huber commented on WW-5112:
--------------------------------

Thanks for this, now we can avoid the class searching without local mods :)

Testing on 6.0.0-RC1, it works great, although when in default mode (false) there is now probably too much logging.

AbstractLocalizedTextProvider
{code:java}
protected String getMessage(String bundleName, Locale locale, String key, ValueStack valueStack, Object[] args) {
        ResourceBundle bundle = findResourceBundle(bundleName, locale);
        if (bundle == null) {
            return null;
        }
        if (valueStack != null) {
            reloadBundles(valueStack.getContext());
        }
        try {
            String message = bundle.getString(key);
            if (valueStack != null) {
                message = TextParseUtil.translateVariables(bundle.getString(key), valueStack);
            }
            MessageFormat mf = buildMessageFormat(message, locale);
            return formatWithNullDetection(mf, args);
        } catch (MissingResourceException e) {
            if (devMode) {
                LOG.warn("#2Missing key [{}] in bundle [{}]!", key, bundleName);
            } else {
                LOG.debug("Missing key [{}] in bundle [{}]!", key, bundleName);
            }
            return null;
        }
    }

{code}
eg: with parameter set to false (default)


If it matches a bundle for the class (MyAdd.properties) and does not find the property it logs,

2022-01-02 09:55:32,786 WARN com.opensymphony.xwork2.util.AbstractLocalizedTextProvider AbstractLocalizedTextProvider:getMessage - #2Missing key [myadd.tip] in bundle [my.MyAdd]!

For an invalid property:

2022-01-02 09:55:32,786 WARN com.opensymphony.xwork2.util.AbstractLocalizedTextProvider AbstractLocalizedTextProvider:getMessage - #2Missing key [myadd.tipz] in bundle [my.MyAdd]!

and then logs it again upstream as not found:
2022-01-02 09:55:32,796 WARN com.opensymphony.xwork2.util.AbstractLocalizedTextProvider AbstractLocalizedTextProvider:findDefaultText - Missing key [myadd.tipz] in bundles [[ApplicationResources, org/apache/struts2/struts-messages, com/opensymphony/xwork2/xwork-messages]]!
2022-01-02 09:55:32,797 WARN org.apache.struts2.util.TextProviderHelper TextProviderHelper:getText - The first TextProvider in the ValueStack (my.MyAdd) could not locate the message resource with key 'myadd.tipz'
2022-01-02 09:55:32,797 WARN org.apache.struts2.util.TextProviderHelper TextProviderHelper:getText - The default value expression 'myadd.tipz' was evaluated and did not match a property. The literal value 'myadd.tipz' will be used.

We could comment out the MissingResourceException catch logging, reducing the unnecessary entries, and if it really does not exist we get the full log.

Originally posted this query on the dev list.

> Add ability (control flag) for TextProviders to prioritize reads from the default resource bundlest.
> ----------------------------------------------------------------------------------------------------
>
>                 Key: WW-5112
>                 URL: https://issues.apache.org/jira/browse/WW-5112
>             Project: Struts 2
>          Issue Type: Improvement
>          Components: Core
>    Affects Versions: 2.6
>            Reporter: James Chaplin
>            Priority: Minor
>              Labels: pull-request-available
>             Fix For: 2.6
>
>
> This proposed improvement derives from a discussion on the Struts Developer List, with the general idea introduced by Greg Huber.
> Improvement: Introduce a new STRUTS_I18N_SEARCH_DEFAULTBUNDLES_FIRST control flag constant, and accompanying logic, with the default setting being _false_.
> The new control flag will be used by AbstractLocalizedTextProvider descendants to identify alternative I18N resource bundle lookup logic that prioritizes key lookup in the default resource bundles (in cases where such a distinction makes sense).
> The StrutsLocalizedTextProvider will use the flag to determine whether searches for resources will check the default bundles first (if the flag is _true_) or last (if the flag is _false_) for a match to a lookup key.  The GlobalLocalizedTextProvider only searches default bundles, so the flag will have no effect on it.
> The _false_ setting corresponds to the standard lookup logic for the TextProviders in Struts 2, and will be the default, so that there will be no impact to existing applications.  Applications that wish to enable the alternate lookup will do so by setting the flag to _true_ in their Struts configuration.
> With a _true_ setting, if a key exists in a default resource bundle (such as the application's default resource bundle), it will supersede any such key in other bundles (such as ones at the package or class level).  This will allow for a different resource bundle strategy when using the StrutsLocalizedTextProvider, and may be more efficient for applications that either only use a single global application resource bundle. or that use such a bundle for the vast majority of their I18N resource keys.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)