<i18n:localize> Tag

This tag allows you to define the language, country, variant, and base bundle name to be used throughout a page when accessing resource bundles via the <i18n:getmessage> tag. This tag also specifies a character encoding and content type to be specified for a JSP. Because of this, the tag should be used as early in the page as possible—before anything is written to the output stream—so that the bytes are properly encoded.

When an HTML page is included in a larger page, only the larger page can use the <i18n:localize> tag. This is because the <i18n:localize> tag sets the encoding for the page, and the encoding must be set in the parent (including) page before any bytes are written to the response’s output stream. The parent page must set an encoding that is sufficient for all the content on that page as well as any included pages.

The preferred approach is to retrieve all strings dynamically from the <i18n:getMessage> tag, and avoid embedding strings statically (that is, avoid hard-coding them) in your JSP.

If your page contains only dynamic strings (strings retrieved using the <i18n:getMessage tag>), then do not use the <i18n:localize> tag in conjunction with the <%@ page contentType="<something>" > page directive defined in the JSP specification. The directive is unnecessary if you are using the <i18n:localize> tag, and can result in inconsistent or wrong contentType declarations.

If you must mix static strings and dynamic strings on the same page, then you will need to use the page directive. Ensure that the character set specified by the <i18n:localize> tag is compatible with the character set specified in the page directive.

Setting the default locale: To avoid server errors, make sure that a default locale is set on each machine running WebLogic Server.

Syntax

<tagName attribute="value" />

Attributes


bundleName

Optional (String) - The base name of the MessageBundle is used to retrieve localized text for a JSP page.

The tag looks for a .properties file in the current directory (that the JSP is in) to retrieve localized content for the JSP. For example, you could store a file called Messages.properties in the current directory. For the bundleName, enter the filename without the .properties extension that you have put in the current directory. For example, bundleName="Messages".

Using a Global .properties File

If the .properties file is not found in the current directory, the tag will look upwards (towards the Web application root directory) in the directory structure to see if the file exists in any of the parent directories. In this case, a single .properties file could be placed in the Web application root directory and be shared as a properties file for all JSPs in the Web application when they reference this properties file with the bundleName attribute. The bundleName setting for all JSPs in the Web application would still be bundleName="Messages".

You could also store a .properties file in a Web application subdirectory. To use a global .properties file in this way (for example, one stored in WEB-INF/classes/test/Messages.properties), set the bundleName attribute in all JSPs as bundleName="/WEB-INF/classes/test/Messages".

Performance Tuning: You can determine how often WebLogic Portal checks for updated content in resource bundles. The default is for resource bundles to never expire. In the portal Web project's <project>/WEB-INF/web.xml file, add a <context-param> entry with i18n.bundle.reload.seconds, as shown in the following example:

    <context-param>
        <param-name>i18n.bundle.reload.seconds</param-name>
        <param-value>300</param-value>
    </context-param>

The <param-value> number is in seconds; 300 seconds is 5 minutes. Changing the value is a balance between flexibility and performance. If your resource bundle content changes frequently, lowering the value makes WebLogic Portal update content more frequently, though at a cost to performance. If your resource bundle content stays relatively static, increase performance by choosing a higher value, such as 86400 (24 hours).

The default is for resource bundles to never exprire, which can be explicitly set by a negative value such as -1. If you use the default, you must redeploy the Web application to update resource bundles.

language

Optional (String or String []) - A String—two character ISO Language Code—denoting the user's preferred language, or a String[] containing a list of preferred language codes for a user, with stronger preferences indexed lower (earlier) in the array.

country

Optional (String) - The two-character ISO Country Code for a country. For example, this code would be used to look for a MessageBundle containing text localized to English speaking users in the U.S. (en-us) as opposed to English speaking users in the U.K. (en-uk).

variant

Optional (String) - A String representing a locale's variant. The variant is used when localization demands a more specific locale than can be denoted by having just language and a country. For example, two variants for English are U.S. English and U.K. English.

locale

Optional (java.util.Locale) - Instead of specifying language, country, and variant as Strings, a java.util.Locale object can be provided. If provided, the values in the Locale's language, country, and variant fields will negate any of the other language, country, and variant values passed to the tag as Strings.

charset

Optional (String) - The name of the character encoding set to use for this page. Defaults to "UTF-8" if no encoding can be determined for the chosen language; otherwise an encoding appropriate for the chosen language is used.

contentType

Optional (String) - The type of content contained in the page, defaults to "text/html".

Example1

This example shows how to use <i18n:localize> to define a single language preference.

<% 
// Definition of a single language preference 
String language = "en"; 
%> 
<i18n:localize language="<%=language%>" bundleName="Messages"/>        
<html> 
<body> 
<i18n:getMessage messageName="greeting"/> 
</body> 
</html>

Example 2

This example shows how to use <i18n:localize> to define two language preferences, English and Spanish.

<% 
// Array that defines two languages preferences - English and
// Spanish in that order of preference. 
String[] languages = new String[] { "en", "es" }; 
%> 
<i18n:localize language="<%=languages%>" bundleName="Messages"/>        
<html> 
<body> 
<i18n:getMessage messageName="greeting"/> 
</body> 
</html> 

Related Topics

<i18n:getMessage> Tag

<l10n:forward> Tag

<l10n:include> Tag

<l10n:resolve> Tag