blob: b321c63ccbb381ac88a6519e244e51606c62ca17 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Localization</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/site.css" />
<link rel="stylesheet" type="text/css" href="css/prettify.css" />
</head>
<body onload="prettyPrint()">
<script type="text/javascript">prefix='';</script>
<script type="text/javascript" src="js/prettify.js"></script>
<script src="templates/header.js" type="text/javascript"></script>
<div id="left">
<noscript>Please turn on Javascript to view this menu</noscript>
<script src="templates/left.js" type="text/javascript"></script>
</div>
<div id="content">
<h1>Localization support</h1>
<p>A <a
href="http://markmail.org/thread/drcabfc6z42sijdo">discussion</a>
on the slf4j-dev mailing list spawned an open-source project
called <a href="http://cal10n.qos.ch"><b>CAL10N or Compiler
Assisted Localization</b></a>. As its name indicates, CAL10N
focuses on the issue of localization/internationalization in Java
applications.
</p>
<p>The <code>org.slf4j.cal10n</code> package which ships with
<em>slf4j-ext-${project.version}.jar</em> adds an extremely thin
layer on top of CALI0N to offer localized logging.</p>
<p>Once you have a handle on an <a
href="http://cal10n.qos.ch/apidocs/ch/qos/cal10n/IMessageConveyor.html">IMessageConveyor</a>
instance, you can create a <a
href="xref/org/slf4j/cal10n/LocLoggerFactory.html">LocLoggerFactory</a>,
which in turn can create <a
href="xref/org/slf4j/cal10n/LocLogger.html">LocLogger</a>
instances capable of doing localized logging.
</p>
<p>Let us assume that you have defined localized message in your
application. In accordance with the CAL10N's philosophy, you have
the declared the keys for those messages in the enum type
<code>Production</code>.</p>
<pre class="prettyprint source">import ch.qos.cal10n.LocaleData;
import ch.qos.cal10n.Locale;
import ch.qos.cal10n.BaseName;
@BaseName("production")
@LocaleData( { @Locale("en_UK"), @Locale("ja_JP") })
public enum Production {
APPLICATION_STARTED,
APPLICATION_STOPPED,
...
DB_CONNECTION,
DB_CONNECTION_FAILURE;
}</pre>
<p>It is assumed that you have created appropriate bundle file for
the various locales "en_UK" and "ja_JP" as appropriate. Here is a
sample bundle for the "en_UK" locale.
</p>
<pre class="source">APPLICATION_STARTED=Application <b>{0}</b> has started.
APPLICATION_STOPPED=Application <b>{0}</b> has stopped.
... </pre>
<p>Then, you
can instantiate a <code>IMessageConveyor</code>, inject it into a
<code>LogLoggerFactory</code>, retrieve multiple
<code>LogLogger</code> instances by name and log away, as the next
sample code illustrates.
</p>
<pre class="prettyprint source">import java.util.Locale;
import org.slf4j.cal10n.LocLogger;
import org.slf4j.cal10n.LocLoggerFactory;
import ch.qos.cal10n.IMessageConveyor;
import ch.qos.cal10n.MessageConveyor;
public class MyApplication {
// create a message conveyor for a given locale
IMessageConveyor messageConveyor = new MessageConveyor(Locale.UK);
// create the LogLoggerFactory
LocLoggerFactory llFactory_uk = new LocLoggerFactory(messageConveyor);
// create a locLogger
LocLogger locLogger = llFactory_uk.getLocLogger(this.getClass());
public void applicationStart() {
locLogger.info(Production.APPLICATION_STARTED, "fooApp");
// ..
}
public void applicationStop() {
locLogger.info(Production.APPLICATION_STOPPED, "fooApp");
// ...
}
}</pre>
<p>Assuming the resource bundle
<em>production_en_UK.properties</em> exists, and the underlying
logging framework is enabled for the info level, log messages will
be output in UK English.
</p>
<p>Note that a <code>LogLogger</code> is a regular SLF4J logger
with additional methods supporting localization. For those
additional methods which take an enum as first parameter,
<code>LogLogger</code> follows the standard Java convention for
parameter substitution as defined by the <a
href="http://java.sun.com/j2se/1.5.0/docs/api/java/text/MessageFormat.html">java.text.MessageFormat</a>
class. For non-localized logs, which take a String as first
parameter, <code>LogLogger</code> follows the {} convention, as
customary for all <code>org.slf4j.Logger</code> implementations.
</p>
<p>Here is an example illustrating the difference.</p>
<pre class="prettyprint source">import ...;
public class MyApplication {
IMessageConveyor messageConveyor = new MessageConveyor(Locale.UK);
LocLoggerFactory llFactory_uk = new LocLoggerFactory(messageConveyor);
LocLogger locLogger = llFactory_uk.getLocLogger(this.getClass());
public void someMethod() {
// follows the MessageFormat convention
locLogger.info(Production.APPLICATION_STARTED, "fooApp");
// follows the {} convention
logLogger.info("Hello {}", name);
...
}
}</pre>
<script src="templates/footer.js" type="text/javascript"></script>
</div>
</body>
</html>