2012-12-01 14 views
11

में संसाधन बंडल गुण पढ़ें <resource-bundle> फ़ाइलों का उपयोग करके मैं अपने जेएसएफ पृष्ठों में i18n टेक्स्ट प्राप्त करने में सक्षम हूं।एक प्रबंधित बीन

लेकिन क्या मेरे प्रबंधित बीन में इन गुणों को एक्सेस करना संभव है ताकि मैं i18n मानों के साथ चेहरे संदेश सेट कर सकूं?

उत्तर

39

यह मानते हुए कि आप इसे कॉन्फ़िगर कर दिया है इस प्रकार है: अपने सेम अनुरोध scoped है

<resource-bundle> 
    <base-name>com.example.i18n.text</base-name> 
    <var>text</var> 
</resource-bundle> 

हैं, तो आप सिर्फ अपनी <var> द्वारा @ManagedProperty रूप <resource-bundle> इंजेक्षन कर सकते हैं:

@ManagedProperty("#{text}") 
private ResourceBundle text; 

public void someAction() { 
    String someKey = text.getString("some.key"); 
    // ... 
} 

या यदि आपको बस कुछ विशिष्ट कुंजी की आवश्यकता है:

@ManagedProperty("#{text['some.key']}") 
private String someKey; 

public void someAction() { 
    // ... 
} 

अपने सेम एक व्यापक दायरे में तथापि है, तो #{text} प्रोग्राम के रूप में विधि स्थानीय दायरे में मूल्यांकन:

public void someAction() { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    ResourceBundle text = context.getApplication().evaluateExpressionGet(context, "#{text}", ResourceBundle.class); 
    String someKey = text.getString("some.key"); 
    // ... 
} 

या यदि आप केवल कुछ विशिष्ट कुंजी की आवश्यकता:

public void someAction() { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    String someKey = context.getApplication().evaluateExpressionGet(context, "#{text['some.key']}", String.class); 
    // ... 
} 

आप इसे मानक ResourceBundle एपीआई द्वारा भी प्राप्त कर सकते हैं वैसे ही जेएसएफ स्वयं ही सह के तहत कर रहा है vers, आप केवल कोड में आधार नाम दोहराने की आवश्यकता होगी:

public void someAction() { 
    FacesContext context = FacesContext.getCurrentInstance(); 
    ResourceBundle text = ResourceBundle.getBundle("com.example.i18n.text", context.getViewRoot().getLocale()); 
    String someKey = text.getString("some.key"); 
    // ... 
} 

या यदि आप JSF के बजाय CDI द्वारा सेम प्रबंधन कर रहे हैं, तो आप उस के लिए एक @Producer बना सकते हैं:

public class BundleProducer { 

    @Produces 
    public PropertyResourceBundle getBundle() { 
     FacesContext context = FacesContext.getCurrentInstance(); 
     return context.getApplication().evaluateExpressionGet(context, "#{text}", PropertyResourceBundle.class); 
    } 

} 

और यह नीचे के रूप में इंजेक्षन:

@Inject 
private PropertyResourceBundle text; 

वैकल्पिक रूप से, यदि आप जेएसएफ उपयोगिता लाइब्रेरी OmniFaces के Messages वर्ग का उपयोग कर रहे हैं, तो आप बंडल का उपयोग करने के लिए सभी Message विधियों को एक बार देने के लिए केवल एक बार अपने रिज़ॉल्यूशन सेट कर सकते हैं।

Messages.setResolver(new Messages.Resolver() { 
    public String getMessage(String message, Object... params) { 
     ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", Faces.getLocale()); 
     if (bundle.containsKey(message)) { 
      message = bundle.getString(message); 
     } 
     return MessageFormat.format(message, params); 
    } 
}); 

भी देखें javadoc और showcase page में उदाहरण।

+0

'अपने सेम अनुरोध scoped है' यह है कि केवल गुंजाइश है कि propperties फ़ाइल तक पहुंच सकते हैं? –

+0

@ कुरियल: जेएसएफ '@ प्रबंधित पोपर्टी 'व्यापक दायरे में एक संक्षिप्त दायरे को इंजेक्शन देने में सक्षम नहीं है। केवल सीडीआई 'इंजेक्ट' है। यदि आप सीडीआई का उपयोग करते हैं, तो http://stackoverflow.com/q/28045667 – BalusC

+0

@ बालससी पर जाएं, जो संसाधन बंडल फ़ाइल में पैरामीटर के साथ पाठ लिखते समय वाक्यविन्यास फ़ॉर्म का उपयोग किया जाना चाहिए? –

1

यहाँ एक समाधान मैं उपयोग कर रहा हूँ, सरल रूप में नहीं लेकिन कम से कम काम कर रहे है:

प्रथम श्रेणी:

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(); 
    } 
} 

द्वितीय श्रेणी:

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()); 
    } 
} 

आप ओवरराइड करने के लिए की जरूरत है के बाद अपनी गुण फ़ाइलों के साथ BUNDLE_LIST। एक बार ऐसा करने के बाद, इसका उपयोग करें:

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

यदि आपके कोई प्रश्न हैं, तो संकोच न करें!

संपादित करें:

तुम भी MessageInterpolator

META-INF/सत्यापन की घोषणा करने की जरूरत है।एक्सएमएल

<validation-config 
    xmlns="http://jboss.org/xml/ns/javax/validation/configuration" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"> 
    <message-interpolator>com.spectotechnologies.website.util.MessageInterpolator</message-interpolator> 
</validation-config> 
2

एक और संभावना:

चेहरे-config.xml

<?xml version='1.0' encoding='UTF-8'?> 
<faces-config ...> 
    <application> 
    <locale-config> 
     <default-locale>de</default-locale> 
    </locale-config> 
    <resource-bundle> 
     <base-name>de.fhb.resources.text.backend</base-name> 
     <var>backendText</var> 
    </resource-bundle> 
    </application> 
</faces-config> 

YourBean.java

FacesContext context = FacesContext.getCurrentInstance(); 
Application app = context.getApplication(); 
ResourceBundle backendText = app.getResourceBundle(context, "backendText"); 
backendText.getString("your.property.key"); 
संबंधित मुद्दे