Read resource bundle properties in a managed bean

后端 未结 3 405
时光取名叫无心
时光取名叫无心 2020-11-28 09:57

Using files I\'m able to have i18n text in my JSF pages.

But is it possible to access these same properties in my managed bean

3条回答
  •  隐瞒了意图╮
    2020-11-28 10:39

    Here is a solution I'm using, not as simple but at least working :

    First class :

    package com.spectotechnologies.website.util;
    
    import java.util.Locale;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
    
    /**
     *
     * @author Alexandre Lavoie
     */
    public class MessageInterpolator extends ResourceBundleMessageInterpolator
    {
        public static final String LANGUAGE_TAG_PATTERN = "\\{[^}]*\\}";
    
        @Override
        public String interpolate(String p_sMessage, Context p_oContext)
        {
            return super.interpolate(replaceMessages(p_sMessage),p_oContext);
        }
    
        @Override
        public String interpolate(String p_sMessage, Context p_oContext, Locale p_oLocale)
        {
            StringManager.setLocale(p_oLocale);
    
            return super.interpolate(replaceMessages(p_sMessage),p_oContext,p_oLocale);
        }
    
        private String replaceMessages(String p_sMessage)
        {
            Matcher oMatcher;
            String sKey;
            String sReplacement;
            StringBuffer sbTemp = new StringBuffer();
    
            oMatcher = Pattern.compile(LANGUAGE_TAG_PATTERN).matcher(p_sMessage);
            while(oMatcher.find())
            {
                sKey = oMatcher.group().substring(1,oMatcher.group().length() - 1);
    
                sReplacement = StringManager.getString(sKey);
    
                if(!sReplacement.startsWith("???"))
                {
                    oMatcher.appendReplacement(sbTemp,sReplacement);
                }
            }
    
            oMatcher.appendTail(sbTemp);
    
            return sbTemp.toString();
        }
    }
    

    Second class :

    package com.spectotechnologies.website.util;
    
    import com.spectotechnologies.util.BundleManager;
    import com.spectotechnologies.util.FacesSessionManager;
    import java.util.Locale;
    
    /**
     * set-up and interface a BundleManager
     * save the bundleManager into the session
     * @author Charles Montigny
     */
    public final class StringManager
    {
        /** the session name of this class bundle manager */
        public static final String BUNDLE_MANAGER_SESSION_NAME = "StringManager_BundleManager";
    
        /**  List of ResourceBundle names to load.
         * Will be load in order.
         * The last ones values may overrite the first ones values. */
        private final static String[] BUNDLE_LIST = {
            "com.spectotechnologies.hibernate.validation.resources.ValidationMessages",
            "com.spectotechnologies.website.general.resources.ValidationMessages",
            "com.spectotechnologies.website.general.resources.General"};
    
        /** bundle manager */
        private static BundleManager m_oBundleManager = null;
    
        private static BundleManager getBundleManager()
        {
            if(m_oBundleManager == null)
            {
                // get the bundle into the session
                m_oBundleManager = (BundleManager)FacesSessionManager.getObject(BUNDLE_MANAGER_SESSION_NAME);
                if(m_oBundleManager == null)
                {
                    // session was empty, load a new one
                    m_oBundleManager = new BundleManager();
                    for(int iIndex = 0; iIndex < BUNDLE_LIST.length; iIndex++)
                    {
                        m_oBundleManager.addBundle(BUNDLE_LIST[iIndex]);
                    }
                    // add the bundle to the session
                    FacesSessionManager.setObject(BUNDLE_MANAGER_SESSION_NAME, m_oBundleManager);
                }
            }
            return m_oBundleManager;
        }
    
        /**
         * get a string value in the bundle manager by a string key
         * @param p_sKey the string key
         * @return the value of the string key
         */
        public static String getString(String p_sKey)
        {
            return getBundleManager().getStringValue(p_sKey);
        }
    
        /**
         * set the locale
         * @param p_oLocale the locale to set
         */
        public static void setLocale(Locale p_oLocale)
        {
            getBundleManager().setLocale(p_oLocale);
    
            // update the session
            FacesSessionManager.setObject(BUNDLE_MANAGER_SESSION_NAME,
                    getBundleManager());
        }
    }
    

    After you need to override BUNDLE_LIST with your properties files. Once done, use it like that :

    sMessage = StringManager.getString("website.validation.modifySystem.modified");
    

    If you have questions, do not hesitate!

    EDIT :

    You also need to declare the MessageInterpolator

    META-INF/validation.xml

    
        com.spectotechnologies.website.util.MessageInterpolator
    
    

提交回复
热议问题