net.sf.mmm.util.nls.base
Class NlsCachingLocalizer

java.lang.Object
  extended by net.sf.mmm.util.nls.base.NlsCachingLocalizer
All Implemented Interfaces:
NlsLocalizer

public class NlsCachingLocalizer
extends Object
implements NlsLocalizer

This is a class that stores messages and simplifies to resolve them.
ATTENTION:
This class collects instances NlsMessage and NlsTemplate to speed up repetitive translations. So only use this class for a dedicated set of constant internationalized messages to avoid memory leaks or supply an according MapFactory at construction.

Since:
2.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Field Summary
private  String bundleName
          The bundle-name.
private  NlsMessageFactory messageFactory
          The NlsMessageFactory instance.
private  Map<String,NlsMessage> messagesMap
          The Map used to cache messages.
private static Pattern TEMPLATE_PATTERN
          The pattern.
private  NlsTemplateResolver templateResolver
          The NlsTemplateResolver instance.
private  Map<String,NlsTemplate> templatesMap
          The Map used to cache templates.
 
Constructor Summary
NlsCachingLocalizer(String bundleName)
          The constructor.
NlsCachingLocalizer(String bundleName, NlsMessageFactory messageFactory, NlsTemplateResolver templateResolver)
          The constructor.
NlsCachingLocalizer(String bundleName, NlsMessageFactory messageFactory, NlsTemplateResolver templateResolver, MapFactory<? extends Map> mapFactory)
          The constructor.
 
Method Summary
 String localize(Locale locale, String internationalizedMessage)
          This method localizes the given internationalizedMessage to the given target locale.
 String localize(Locale locale, String internationalizedMessage, Map<String,Object> arguments)
          This method localizes the given internationalizedMessage with the given arguments filled in to the given target locale.
protected  NlsTemplate parseTemplate(String internationalizedMessage)
          This method parses the given internationalizedMessage as NlsTemplate in case it is given in the form {<BUNDLE>#<KEY>}.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TEMPLATE_PATTERN

private static final Pattern TEMPLATE_PATTERN
The pattern.


messageFactory

private final NlsMessageFactory messageFactory
The NlsMessageFactory instance.


templateResolver

private final NlsTemplateResolver templateResolver
The NlsTemplateResolver instance.


messagesMap

private final Map<String,NlsMessage> messagesMap
The Map used to cache messages.


templatesMap

private final Map<String,NlsTemplate> templatesMap
The Map used to cache templates.


bundleName

private final String bundleName
The bundle-name.

Constructor Detail

NlsCachingLocalizer

public NlsCachingLocalizer(String bundleName)
The constructor.

Parameters:
bundleName - is the default bundle-name.

NlsCachingLocalizer

public NlsCachingLocalizer(String bundleName,
                           NlsMessageFactory messageFactory,
                           NlsTemplateResolver templateResolver)
The constructor.

Parameters:
bundleName - is the default bundle-name.
messageFactory - is the NlsMessageFactory instance.
templateResolver - is the NlsTemplateResolver instance.

NlsCachingLocalizer

public NlsCachingLocalizer(String bundleName,
                           NlsMessageFactory messageFactory,
                           NlsTemplateResolver templateResolver,
                           MapFactory<? extends Map> mapFactory)
The constructor.

Parameters:
bundleName - is the default bundle-name.
messageFactory - is the NlsMessageFactory instance.
templateResolver - is the NlsTemplateResolver instance.
mapFactory - is the MapFactory used to create caches for the the messages and templates.
Method Detail

parseTemplate

protected NlsTemplate parseTemplate(String internationalizedMessage)
This method parses the given internationalizedMessage as NlsTemplate in case it is given in the form {<BUNDLE>#<KEY>}.

Parameters:
internationalizedMessage - is the template specified in the form described above or the internationalized message.
Returns:
the NlsTemplate if the internationalizedMessage has the form described above or null otherwise.

localize

public String localize(Locale locale,
                       String internationalizedMessage,
                       Map<String,Object> arguments)
This method localizes the given internationalizedMessage with the given arguments filled in to the given target locale.

Specified by:
localize in interface NlsLocalizer
Parameters:
locale - is the Locale to translate to.
internationalizedMessage - is the internationalized message to translate.
arguments - are the arguments to fill in.
Returns:
the localized message.

localize

public String localize(Locale locale,
                       String internationalizedMessage)
This method localizes the given internationalizedMessage to the given target locale.

Specified by:
localize in interface NlsLocalizer
Parameters:
locale - is the Locale to translate to.
internationalizedMessage - is the internationalized message to translate.
Returns:
the localized message.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.