Get All Available Translations of a Folder or Document
The org.hippoecm.hst.content.beans.standard.HippoBean interface, which is implemented by classes such as HippoDocument and HippoFolder, provides a method to retrieve all available translations for a bean. All document and folder beans that extend HippoBean support this functionality.
<T extends HippoBean> HippoAvailableTranslationsBean<T> getAvailableTranslations();
The HippoAvailableTranslationsBean interface exposes all translations for a given document or folder bean. It provides the following methods:
public interface HippoAvailableTranslationsBean<K extends HippoBean> { /** * Returns the available locales. * @return a {@link List} of available locale strings, or an empty list if no locales are present */ List<String> getAvailableLocales(); /** * Checks if a translation exists for the specified locale. * @param locale the locale string * @return true if a translation for the locale exists */ boolean hasTranslation(String locale); /** * Returns all translation beans. * @param <K> the type of elements in the returned list * @return a list of all translation beans, or an empty list if none are found * @throws ClassCastException if a bean cannot be cast to <K> */ List<K> getTranslations() throws ClassCastException; /** * Returns the translation bean for a specific locale. * @param <K> the return type * @param locale the locale string * @return the translation bean for the locale, or null if not present * @throws ClassCastException if the bean cannot be cast to <K> */ K getTranslation(String locale) throws ClassCastException; }
You can access all available translations directly in your templates. The following examples show how to iterate through all translations of a bean. The property bean.localeString returns the language of each bean.
Example: List All Translations
JSP
<c:forEach var="bean" items="${requestScope.crBean.availableTranslations.translations}"> <hst:link var="link" hippobean="${bean}"/> <div> <a href="${link}"> ${bean.localeString} </a>HippoDocument </div> </c:forEach>
Freemarker
<#list crBean.availableTranslations.translations as bean> <@hst.link var="link" hippobean=bean/> <div> <a href="${link}"> ${bean.localeString} </a> </div> </#list>
Example: Exclude the Current Bean from the List
The crBean.availableTranslations.translations property includes the current bean in the list of translations. To exclude the current bean (for example, to avoid linking to the current language), compare each translation bean to the current bean. In JSP, use the equalComparator property. In Freemarker, use the equality operator.
JSP
<c:forEach var="bean" items="${requestScope.crBean.availableTranslations.translations}"> <hst:link var="link" hippobean="${bean}"/> <!-- Use the equal comparator to check if the translation is the current bean --> <c:choose> <c:when test="${bean.equalComparator[crBean]}"> <!-- Do not display a link for the current bean --> </c:when> <c:otherwise> <div> <a href="${link}"> ${bean.localeString} </a> </div> </c:otherwise> </c:choose> </c:forEach>
Freemarker
<#list crBean.availableTranslations.translations as bean> <@hst.link var="link" hippobean=bean /> <#if bean == crBean> <!-- Do not display a link for the current bean --> <#else> <div> <a href="${link}"> ${bean.localeString} </a> </div> </#if> </#list>
For more information about the equalComparator property, refer to the HippoBean class in the Site Toolkit API.