JBoss.orgCommunity Documentation
In the previous versions, Social had hard-coded messages for activities, such as creating spaces, granting the "manager" role to a member, sending connection request to another user, updating a user's profile/avatar, and more. And now, to internationalize these types of messages, you can use resource bundles and I18NActivityProcessor.
For example, to internationalize an activity of the exosocial:spaces type that is for space creation message, do as follows:
1. Set titleId for the ExoSocialActivity model.
ActivityType = exosocial:spaces, titleId = space_created, resource bundle key file: locale.social.Core, and associated message bundle key:SpaceActivityPublisher.space_created
The titleId is used to map with a corresponding message bundle key via the configuration.
Sample code for saving internationalized activities
public void saveActivity() {
ActivityManager activityManager = (ActivityManager) PortalContainer.getInstance().getComponentInstanceOfType(ActivityManager.class);
ExoSocialActivity activity = new ExoSocialActivityImpl();
activity.setType("exosocial:spaces"); // the associated activity type
activity.setTitleId("space_created"); // to indicate this is i18n activity type
// this is the fallback activity title when it's not i18n-ized.
// This must be required.
activity.setTitle("Test was created by @john");
//template params are used to for compound messages
// with message bundle key:
// SpaceActivityPublisher.space_created={0} was created by {1}.
Map<String, String> templateParams = new LinkedHashMap<String, String>();
templateParams.put("space_name", "Test");
templateParams.put("user", "@john");
//must indicate this param if you want a template value is processed by activity processors
templateParams.put(BaseActivityProcessorPlugin.TEMPLATE_PARAM_TO_PROCESS, "user");
activity.setTemplateParams(templateParams);
//gets the target stream to post
IdentityManager identityManager = (IdentityManager) PortalContainer.getInstance().getComponentInstanceOfType(IdentityManager.class);
Identity spaceIdentity = identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, "test", false);
activity.setUserId(spaceIdentity.getId()); // the actor is the space identity
//posts this activity to space's activity stream
activityManager.saveActivityNoReturn(spaceIdentity, activity);
}
The sample code above is enough for creating an internationalized activity that will be displayed on the space activity stream portlet after all the configurations below are done. The returned result will be displayed in English like this: "Test was created by <a href="link-to-john-profile">John</a>".
2. Register the ActivityResourceBundlePlugin plugin to the I18NActivityProcessor component in the configuration file as the example below:
<external-component-plugins> <target-component>org.exoplatform.social.core.processor.I18NActivityProcessor</target-component> <component-plugin> <name>exosocial:spaces</name> <!-- activity type --> <set-method>addActivityResourceBundlePlugin</set-method> <type>org.exoplatform.social.core.processor.ActivityResourceBundlePlugin</type> <init-params> <object-param> <name>locale.social.Core</name> <!-- resource bundle key file --> <description>activity key type resource bundle mapping for exosocial:spaces</description> <object type="org.exoplatform.social.core.processor.ActivityResourceBundlePlugin"> <field name="activityKeyTypeMapping"> <map type="java.util.HashMap"> <entry> <key><string>space_created</string></key> <value><string>SpaceActivityPublisher.space_created</string></value> </entry> </map> </field> </object> </object-param> </init-params> </component-plugin> </external-component-plugins>
If the resource bundle message is compound, you must provide templateParams. The argument number will be counted as it appears on the map. For example:
templateParams = {"key1": "value1", "key2": "value2"} => arguments = ["value1", "value2"]
To reserve this order, LinkedHashMap must be used to create templateParams instead of HashMap.
3. Register an external resource bundle for that activity type to get an associated resource bundle as follow:
<external-component-plugins>
<target-component>org.exoplatform.services.resources.ResourceBundleService</target-component>
<component-plugin>
<name>Social Core Component Resource Bundle</name>
<set-method>addResourceBundle</set-method>
<type>org.exoplatform.services.resources.impl.BaseResourceBundlePlugin</type>
<init-params>
<values-param>
<name>classpath.resources</name>
<description>The resources that start with the following package name should be loaded from file system</description>
<value>locale.social.Core</value>
</values-param>
<values-param>
<name>portal.resource.names</name>
<description>The resources that start with the following package name should be loaded from file system</description>
<value>locale.social.Core</value>
</values-param>
</init-params>
</component-plugin>
</external-component-plugins>