Am 26.06.2017 um 08:57 schrieb Lukasz Lenart:
> 2017-06-25 8:07 GMT+02:00 i...@flyingfischer.ch <i...@flyingfischer.ch>:
>> I get stuck with the refactored LocalizedTextUtil:
>>
>> We had a change in 2.5.2 which disallowed the use of getText(String key)
>> in classes that cannot extend ActionSupport.
>>
>> I did work around with a custom utility class that did the job, built
>> mainly around:
>>
>> final String value = LocalizedTextUtil.findDefaultText(key, getLocale());
>>
>> This does not work anymore. What is the present correct way to get this
>> working? Is there any documentation for this?
> LocalizedTextUtil was converted into a bean, the LocalizedTextProvider
> - an interface with two implementations. You can use them by defining
> a setter and annotating it with @Inject (like for other Struts beans)
>
> @Inject
> public void setLocalizedTextProvider(LocalizedTextProvider
> localizedTextProvider) {
>     this.localizedTextProvider = localizedTextProvider;
> }
>
> This will inject an implementation which behaves the same as
> LocalizedTextUtil, it will scan hierarchy (but just once) to localise
> proper resource bundles.
>
> The second implementation based just on the global properties defined
> with "struts.custom.i18n.resources" and you can use it by adding a
> name to the @Inject
>
> @Inject("global-only")
> public void setLocalizedTextProvider(LocalizedTextProvider
> localizedTextProvider) {
>     this.localizedTextProvider = localizedTextProvider;
> }
>
>
> Regards

Thanks for the inputs. I cant't get this to work. Sorry, this is rather
a topic for user list. I'll wait to see some examples coming around. For
the moment being I'll use, not very sophisticated my utility class
below. This works fine. Though I think Struts should have somthing like
this integrated. Before 2.5 this was as easy as "getText(key)".

Markus


package util;

import java.util.Locale;
import java.util.ResourceBundle;

import org.apache.commons.text.StringEscapeUtils;

import com.opensymphony.xwork2.ActionContext;

public class Texti18n {

    /**
     * Returns i18n text messages from the default bundles for utility
classes which cannot
     * extend ActionSupport: Utility myUtility = new Utility() will not
work with struts default
     * getText() i18n mechanism, due to a change in behavior with struts
> 2.5.2.
     * Returns the value for a key depending on the locale present. For
missing i18n keys in a given
     * language bundle, the default bundle (English) will be used. If a
key is not present at all, the key will
     * be returned instead of a value.
     */
    public String getText(final String key) {

        final ResourceBundle bundle =
ResourceBundle.getBundle("global-messages", getLocale());
        final String value = bundle.getString(key);

        if (value != null) {
            return
StringEscapeUtils.unescapeHtml4(value.replaceAll("''", "'"));
        }

        return key;
    }

    /**
     * Returns the Locale present in the ActionContext. The Locale.US
will be returned as default,
     * if no Locale is present.
     */
    public Locale getLocale() {

        final ActionContext context = ActionContext.getContext();

        if (context != null && context.getLocale() != null) {
            return context.getLocale();
        } else {
            // default language
            return Locale.US;
        }

    }

}





---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@struts.apache.org
For additional commands, e-mail: dev-h...@struts.apache.org

Reply via email to