Get all Available Translations of a Folder or Document

The org.hippoecm.hst.content.beans.standard.HippoBean interface (implemented by e.g.  HippoDocument or HippoFolder, so typically, all your document beans will benefit from this) has the following method:

<T extends HippoBean> HippoAvailableTranslationsBean<T> getAvailableTranslations();

The  HippoAvailableTranslationsBean contains all the translations for your document or folder bean. HippoAvailableTranslationsBean provides the following methods:

public interface HippoAvailableTranslationsBean<K extends HippoBean> {
    /**
     * It returns the available translations.
     * @return the {@link List} of available locale's and an empty
     *         {@link List} is no locale's are present
     */
    List<String> getAvailableLocales();

    /**
     * @param locale
     * @return <code>true</code> when the translation for <code>locale</code>
     *         is present
     */
    boolean hasTranslation(String locale);

    /**
     * @param <K> the type of elements in the {@link List} to be returned
     * @return the List of all {@link K}'s and an empty {@link List} if no
     *         translations found
     * @throws ClassCastException when the bean for <code>locale</code>
     *         cannot be cast to <K>
     */
    List<K> getTranslations() throws ClassCastException;

    /**
     * @param <K> the return type
     * @param locale the locale for the translation
     * @return returns the {@link K} for <code>locale</code> and
     *         <code>null</code> if not present
     * @throws ClassCastException when the bean for <code>locale</code>
     *         cannot be cast to <K>
     */
     K getTranslation(String locale) throws ClassCastException;
}

In your template, you can directly get all available translations from a bean without writing any code yourself. The example snippet below loops through all available translations. bean.localeString returns the name of the bean's language.

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>

Because crBean.availableTranslations.translations returns the list of translations including the current one, the snippet below is an example that treats the current bean differently (no link). We use the equalComparator for this (see org.hippoecm.hst.content.beans.standard.HippoBean javadocs)

JSP

<c:forEach var="bean" items="${requestScope.crBean.availableTranslations.translations}">
  <hst:link var="link" hippobean="${bean}"/>
  <!-- the equal comparator can be used to check whether the
       translation is the current bean  -->
  <c:choose>
    <c:when test="${bean.equalComparator[crBean]}">
      <!-- nothing: don't display 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>
    <!-- nothing: don't display current bean -->
  <#else>
    <div>
      <a href="${link}">
        ${bean.localeString}
      </a>
    </div>
  </#if>
</#list>

 

Did you find this page helpful?
How could this documentation serve you better?
On this page
    Did you find this page helpful?
    How could this documentation serve you better?