Usage in an HST Site
To use a Resource Bundle Document in a JSP page, you must first load the bundle. The standard approach is to use the <fmt:setBundle/> tag from the Java Standard Tag Library (JSTL). However, <fmt:setBundle/> only loads resource bundles from the classpath.
To support resource bundles stored in the repository, the HST tag library provides <hst:setBundle/>. This tag extends the behavior of <fmt:setBundle/> and allows you to load resource bundles from the repository using their unique ID. This enables you to use document-based resource bundles managed in the CMS.
After loading a resource bundle, reference its values in your JSP page using the <fmt:message/> tag. Set the key attribute to the resource key you want to retrieve (for example, <fmt:message key="search.results"/>).
The following JSP example demonstrates how to load a resource bundle and use its values:
<fmt:setLocale value="en_US"/> <hst:setBundle basename="com.myhippoproject.example"/> <form action="<hst:link siteMapItemRefId='search'/>" method="get"> <div class="input-group"> <fmt:message key='search.box.placeholder' var="placeholder"/> <input type="text" placeholder="${fn:escapeXml(placeholder)}" name="query"> <div class="input-group-btn"> <fmt:message key='search.box.button_label' var="label"/> <button class="btn btn-default" type="submit">${fn:escapeXml(label)}</button> </div> </div> </form>
- The first line sets the locale to English using the
<fmt:setLocale/>tag. - The second line loads the resource bundle by specifying its unique ID.
- The following lines generate an HTML fragment that uses values from the resource bundle.
This example renders resources for the American English locale. To display resources for a different locale, update the locale value in the first line (for example, <fmt:setLocale value="de"/> for German).