When addressing displaying content across multiple languages .We should take care about content being displayed and even the authoring process should also be adjusted to the locale. WCM has two ways in it.
- TextProvider
- MLS(Multi-Lingual-Solution)
TextProvider :- Using text-provider you can localize authoring template labels to your localized values. For example you can change Display title and description of Authoring template.
Now think you want to change display title based on locale. We need to create a custom text provider and have localized values in property files.
Step1 :- Create a dynamic web application and place below plugin.xml in it.
Step 4 :- Now make a war file and deploy it in server.
Step 5 :- Now create a new AuthoringTemplate and click on localizations link on top and select your Text Provider and corresponding property.
Step 6 :- Now click ok and then view authoring template in english and spanish
Note :- Please observe the name difference krshna and krishna is changing based on loaded language.
Now think you want to change display title based on locale. We need to create a custom text provider and have localized values in property files.
Step1 :- Create a dynamic web application and place below plugin.xml in it.
<?xml version="1.0" encoding="UTF-8"?>
<plugin id="com.poc"
name="Sample Text Provider"
version="1.0.0"
provider-name="IBM">
<extension
point="com.ibm.workplace.wcm.api.TextProvider"
id="SampleTextProvider">
<provider class="com.poc.textProvider.SimpleTextProvider"/>
</extension>
</plugin>
Step 2 :- Create your com.poc.textProvider.SimpleTextProvider by extending com.ibm.workplace.wcm.api.TextProvider
package com.poc.textProvider;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.Set;
import com.ibm.portal.ListModel;
import com.ibm.portal.ModelException;
import com.ibm.workplace.wcm.api.plugin.textprovider.TextProvider;
public class SimpleTextProvider implements TextProvider{
/** The text provider title */
public static final String PROVIDER_TITLE = "SampleTextProvider";
/** The unique text provider name */
public static final String PROVIDER_NAME = "SampleTextProvider";
/** A brief description of this text provider */
private static final String PROVIDER_DESCRIPTION = "Simple Text provider";
/** Resource bundle name */
private static final String RESOURCE_BUNDLE_NAME = "SimpleBundle";
/** Resource bundle name */
private static final String RESOURCE_BUNDLE_PROPERTIES_NAME = "SimpleBundle_en.properties";
/** Is shown in authiring UI*/
@Override
public boolean isShownInAuthoringUI() {
return true;
}
@Override
public String getDescription(Locale arg0) {
return PROVIDER_DESCRIPTION;
}
/**
* A simple list model holding locales.
*/
protected static class SimpleLocaleListModel<K> implements ListModel<Locale>
{
/** the list of locales of this list model */
final List<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>.
*/
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
public Iterator<Locale> iterator() throws ModelException
{
return m_localeList.iterator();
}
}
/** a list model that only contains the English and spanish language locale */
private static final ListModel<Locale> ENGLISH_ONLY = new SimpleLocaleListModel<Locale>(new Locale[]{Locale.ENGLISH,Locale.FRENCH});
@Override
public ListModel<Locale> getLocales() {
return ENGLISH_ONLY;
}
@Override
public String getTitle(Locale arg0) {
return PROVIDER_TITLE;
}
/*This will return the list of keys which we can select later */
@Override
public Collection<String> getProviderKeys() {
Collection<String> keys = null;
try
{
LinkedProperties props = new LinkedProperties();
props.load(getClass().getClassLoader().getResourceAsStream(RESOURCE_BUNDLE_PROPERTIES_NAME));
keys = props.getKeySet();
}
catch (IOException e)
{
// The bundle was not found. Return null.
e.printStackTrace();
}
return keys;
}
@Override
public String getProviderName() {
return PROVIDER_TITLE;
}
@Override
public String getString(String p_key, Locale p_displayLocale) {
String value;
try
{
ResourceBundle bundle = ResourceBundle.getBundle(RESOURCE_BUNDLE_NAME, p_displayLocale);
value = bundle.getString(p_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 LinkedProperties extends Properties {
/** Keys */
private final LinkedHashSet<String> keys = new LinkedHashSet<String>();
/**
* @return An ordered set of keys
*/
public Set<String> getKeySet()
{
return keys;
}
@Override
public Object put(Object key, Object value) {
keys.add((String) key);
return super.put(key, value);
}
}
}
Step3 :- Now you need to create property files and place your localized files in it.
Step 5 :- Now create a new AuthoringTemplate and click on localizations link on top and select your Text Provider and corresponding property.
Step 6 :- Now click ok and then view authoring template in english and spanish
Note :- Please observe the name difference krshna and krishna is changing based on loaded language.
No comments:
Post a Comment