Web Content Manager(WCM)-Text Provider Plugin

Text Provider Plugin:
A text provider is used to provide localized text that can be used within web content item forms. For example, a text provider can be used to localize the field labels or help text within an authoring template so that each user sees the labels or help text in their own language.
Create Web based Project -> create class that needs to implement TextProvider or TextProviderAdapter
packagecom.ibm.wcmproject;
importjava.io.IOException;
importjava.util.ArrayList;
import java.util.Collection;
importjava.util.Iterator;
importjava.util.LinkedHashSet;
import java.util.List;
importjava.util.Locale;
importjava.util.MissingResourceException;
importjava.util.Properties;
importjava.util.ResourceBundle;
import java.util.Set;
import com.ibm.portal.ListModel;
importcom.ibm.portal.ModelException;
importcom.ibm.workplace.wcm.api.plugin.textprovider.TextProvider;
public classTextProviderPlugin implements TextProvider{
      private static final String BUNDLE_PROPERTIES = “/com/ibm/wcmproject/nl/SimpleBundle_en.properties”;
     
      public static final String PROVIDER_TITLE = “Simple Text Provider”;
     
      public static final String PROVIDER_DESCRIPTION = “test.SimpleTextProvider”;
     
      public static final String RESOURCE_BUNDLE_NAME = “com.ibm.wcmproject.nl.SimpleBundle”;
     
      public static final String PROVIDER_NAME = “test.SimpleTextProvider”;
     
      protected static classSimpleLocaleListModel<K> implementsListModel<Locale>{
            /** the list of locales of this list model */
            finalList<Locale> m_localeList = new ArrayList<Locale>();
            /**
             * Constructs this simple list model holding the given locales.
             *
             * @param p_locales
             *           the locales of this list model. May be <code>null</code>.
           * @return
             */
            public  SimpleLocaleListModel(final Locale[] p_locales)
            {
               if (p_locales != null)
               {
                  for (int i = 0; i < p_locales.length; ++i)
                  {
                     m_localeList.add(p_locales[i]);
                  }
               }
            }           @Override
            publicIterator<Locale> iterator() throws ModelException {
                  // TODOAuto-generated method stub
                  return  m_localeList.iterator();
            }
           
      }
     
      private static finalListModel<Locale> ENGLISH_ONLY = newSimpleLocaleListModel<Locale>(new Locale[]{Locale.ENGLISH,Locale.FRENCH});
     
      //This method is used to prevent your text provider from appearing in the authoring UI
      @Override
      public booleanisShownInAuthoringUI() {
            return true;
      }
      //This method returns a description of the text provider.
      @Override
      public String getDescription(Locale arg0) {
            return PROVIDER_DESCRIPTION;
      }
      //This method returns a list of locales that are supported by this text provider.
      @Override
      public ListModel<Locale> getLocales() {
            return ENGLISH_ONLY;
      }
      //This method returns the title for the text provider that is used to allow selection of the text provider.
      @Override
      public String getTitle(Locale arg0) {
            // TODOAuto-generated method stub
            return PROVIDER_TITLE;
      }
      //This method returns a list of keys that are used when accessing the text provider.
      //These keys are displayed in the authoring UI when a user is configuring the text provider
      @Override
      publicCollection<String> getProviderKeys() {
            // TODOAuto-generated method stub
            Collection<String> keys = null;
            try
            {
               LinkedProperties props = newLinkedProperties();
               props.load(getClass().getClassLoader().getResourceAsStream(BUNDLE_PROPERTIES));
              
               keys = props.getKeySet();
            }
            catch (IOException e)
            {
               // The bundle was not found. Return null.
               e.printStackTrace();
            }
           
            return keys;      }
      //This method returns the unique name of the text provider
      @Override
      public String getProviderName() {
            // TODOAuto-generated method stub
            return PROVIDER_NAME;
      }
      //This method returns some translated text,given a key identifying the message and a locale
      @Override
      public String getString(String key, Locale displayLocale) {
            String value;
              System.out.println(displayLocale);
            try
            {
               ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME, displayLocale);
               value = bundle.getString(key);
            }
            catch(MissingResourceException e)
            {
               // The bundle or key was not found. Return null.
               value = null;
            }
            return value;     }
         /** Used to provide the properties in order */
         private class LinkedPropertiesextends Properties {
            /** Keys */
            private finalLinkedHashSet<String> keys = newLinkedHashSet<String>();
         
            /**
             * @return An ordered set of keys
             */
            publicSet<String> getKeySet()
            {
               return keys;
            }
            @Override
            public Object put(Object key, Object value) {
                keys.add((String) key);
                return super.put(key, value);
            }
        }
}
Create properties file under com.ibm.wcmproject.nl
1.       English properties file
2.       Other language properties file
Install this plugin in using plugin.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<plugin id=“custom.plugins” name=“Custom Plugin” version=“1.0.0”
      provider-name=“Example”>
      <extension point=“com.ibm.workplace.wcm.api.TextProvider” id=“WCMTextProviderPlugin”>
            <provider class=“com.ibm.wcmproject.TextProviderPlugin” />
      </extension>

</plugin>

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks