Skip to content

Instantly share code, notes, and snippets.

@Baztoune
Last active December 20, 2015 06:49
Show Gist options
  • Save Baztoune/6088571 to your computer and use it in GitHub Desktop.
Save Baztoune/6088571 to your computer and use it in GitHub Desktop.
How to use resource bundle with a locale variant in JSF 1.2
my.var=BlahdeBlih
other.var=ROFLOL
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j" template="/layout/template.xhtml">
<ui:param name="locale_variant" value="#{myBean.parameter.name()}" />
<ui:define name="body">
#{appMsg['my.var']}
</ui:define>
</ui:composition>
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<message-bundle>messages</message-bundle>
<locale-config>
<default-locale>fr</default-locale>
<supported-locale>fr</supported-locale>
<supported-locale>fr_FR_MYVARIANT</supported-locale>
<supported-locale>fr_FR_MYSECONDVARIANT</supported-locale>
</locale-config>
<resource-bundle>
<base-name>application</base-name>
<var>appMsg</var>
</resource-bundle>
</application>
</faces-config>

Depending on the page, my client needed to customize the labels depending on some variable. I wanted to use the i18n mechanism provided by JSF, so it can fallback to a default label and use a custom one when available.

The difficult part was actually to set the locale, depending on a parameter. A solution would have been to extend the ViewHandler and override the calculateLocale() method, but it wasn't a good fit (I didn't manage to access the conversation context, also the locale depends on various values depending on the page you're on). Defining a var in child pages and setting the locale in the root f:view worked like a charm (even though it's ugly).

<f:view locale="#{locale_variant eq null?'fr':'fr_FR_'}#{locale_variant}" />

<ui:param name="locale_variant"  value="#{myBean.parameter.name()}" />

<supported-locale>fr_FR_MYVARIANT</supported-locale>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="http://richfaces.org/a4j"
xmlns:notify="http://richfaces.org/sandbox/notify"
xmlns:s="http://jboss.com/products/seam/taglib" contentType="text/html"
locale="#{locale_variant eq null?'fr':'fr_FR_'}#{locale_variant}">
<f:loadBundle basename="application" var="msg" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Servidom</title>
<link rel="shortcut icon" href="#{request.contextPath}/favicon.ico" />
<ui:insert name="head" />
<script type="text/javascript">window.RICH_FACES_EXTENDED_SKINNING_ON=false;</script>
<a:loadStyle src="/stylesheet/normalize.css" />
<a:loadStyle src="/stylesheet/main.css" />
<ui:insert name="moreStyles" />
<ui:insert name="moreScripts" />
</head>
<body>
<ui:insert name="body" />
</body>
</html>
</f:view>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment