Package org.ametys.core.resources
Class I18nTextResourceHandler
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.core.resources.AbstractResourceHandler
-
- org.ametys.core.resources.I18nTextResourceHandler
-
- All Implemented Interfaces:
ResourceHandler
,LogEnabled
,Component
,Configurable
,Contextualizable
,Serviceable
- Direct Known Subclasses:
JSResourceHandler
public class I18nTextResourceHandler extends AbstractResourceHandler implements Component
This class generates a translated version of an input file. It is designed to handle the following notation : {{i18n x}}
When encountering this pattern, we instantiate anI18nizableText
with x and try to translate it.
Unknown translations are logged and do not prevent the generation process from continuing.
-
-
Field Summary
Fields Modifier and Type Field Description private static char[]
__I18N_BEGINNING_CHARS
The beginning of a valid declaration for an internationalizable text as charactersprivate static String
__I18N_DEFAULT_CATALOGUE_ID
This configuration parameter specifies the id of the catalogue to be used as default catalogue, allowing to redefine the default catalogue on the pipeline level.private static String
__I18N_LOCALE
private static Pattern
__LOCALE_PATTERN
private I18nUtils
_i18nUtils
Avalon component gathering utility methods concerningI18nizableText
, allowing their translation in several languagesprivate boolean
_isDeclarationValid
Is the last analyzed i18n declaration valid ?-
Fields inherited from class org.ametys.core.resources.AbstractResourceHandler
_cocoonContext, _context, _resolver, _supportedSuffixes
-
Fields inherited from interface org.ametys.core.resources.ResourceHandler
MAX_PRIORITY, MIN_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description I18nTextResourceHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private int
_analyzeI18nDeclaration(char[] srcChars, int candidateBeginIdx, BufferedWriter outWriter, int initialOffset, Source source, Parameters par, Map<String,Object> additionalParameters)
Analyze characters from the key beginning index to the possible closure sequence '}}', and write the appropriate replacement in the output string builderprivate boolean
_testI18nDeclarationPrefix(char[] srcChars, int start)
Test if the given character is the start of an i18n declarationprivate void
_translateKey(char[] srcChars, BufferedWriter outWriter, int candidateBeginIdx, int lastIdx, int initialOffset, String locale, Parameters par)
Try to translate the key and write the output stream with its translation if found, the key itself if notvoid
generateResource(Source source, OutputStream out, Map objectModel, Parameters par, Map<String,Object> additionalParameters)
Generate the resource configured during setup, and output itSerializable
getKey(Source source, Map objectModel, Parameters parameters, Map<String,Object> additionalParameters)
Get the unique key for this resource, for cache purpose.protected String
getLocale(Map<String,Object> additionalParameters)
Retrieve the locale from the parametersvoid
service(ServiceManager serviceManager)
Source
setup(String location, Map objectModel, Parameters par, Map<String,Object> additionalParameters)
Initialize the resource handler with a resource.-
Methods inherited from class org.ametys.core.resources.AbstractResourceHandler
configure, contextualize, getLastModified, getMimeType, getPriority, getSize, getValidity, isSupported
-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
-
-
-
Field Detail
-
__I18N_DEFAULT_CATALOGUE_ID
private static final String __I18N_DEFAULT_CATALOGUE_ID
This configuration parameter specifies the id of the catalogue to be used as default catalogue, allowing to redefine the default catalogue on the pipeline level.- See Also:
- Constant Field Values
-
__I18N_LOCALE
private static final String __I18N_LOCALE
- See Also:
- Constant Field Values
-
__I18N_BEGINNING_CHARS
private static final char[] __I18N_BEGINNING_CHARS
The beginning of a valid declaration for an internationalizable text as characters
-
__LOCALE_PATTERN
private static final Pattern __LOCALE_PATTERN
-
_i18nUtils
private I18nUtils _i18nUtils
Avalon component gathering utility methods concerningI18nizableText
, allowing their translation in several languages
-
_isDeclarationValid
private boolean _isDeclarationValid
Is the last analyzed i18n declaration valid ?
-
-
Constructor Detail
-
I18nTextResourceHandler
public I18nTextResourceHandler()
-
-
Method Detail
-
service
public void service(ServiceManager serviceManager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Overrides:
service
in classAbstractResourceHandler
- Throws:
ServiceException
-
setup
public Source setup(String location, Map objectModel, Parameters par, Map<String,Object> additionalParameters) throws ProcessingException, IOException
Description copied from interface:ResourceHandler
Initialize the resource handler with a resource.- Specified by:
setup
in interfaceResourceHandler
- Overrides:
setup
in classAbstractResourceHandler
- Parameters:
location
- The source uriobjectModel
- The object modelpar
- The parametersadditionalParameters
- Additional parameters that can be filled, and will be transmitted to getKey, getValidity and generateResource- Returns:
- the resolved source
- Throws:
ProcessingException
- If an error occursIOException
- If an error occurs
-
getLocale
protected String getLocale(Map<String,Object> additionalParameters)
Retrieve the locale from the parameters- Parameters:
additionalParameters
- The parameters- Returns:
- The locale, or null
-
generateResource
public void generateResource(Source source, OutputStream out, Map objectModel, Parameters par, Map<String,Object> additionalParameters) throws IOException, ProcessingException
Description copied from interface:ResourceHandler
Generate the resource configured during setup, and output it- Specified by:
generateResource
in interfaceResourceHandler
- Parameters:
source
- The sourceout
- The output stream to write toobjectModel
- The object modelpar
- The sitemap parametersadditionalParameters
- Additional parameters- Throws:
IOException
- If an error occursProcessingException
- If an error occurs
-
_testI18nDeclarationPrefix
private boolean _testI18nDeclarationPrefix(char[] srcChars, int start)
Test if the given character is the start of an i18n declaration- Parameters:
srcChars
- the input file as charactersstart
- the index of the given character- Returns:
- true if this is a start of an i18n declaration, false otherwise
-
_analyzeI18nDeclaration
private int _analyzeI18nDeclaration(char[] srcChars, int candidateBeginIdx, BufferedWriter outWriter, int initialOffset, Source source, Parameters par, Map<String,Object> additionalParameters) throws IOException
Analyze characters from the key beginning index to the possible closure sequence '}}', and write the appropriate replacement in the output string builder- Parameters:
srcChars
- the input file as characterscandidateBeginIdx
- the index at which we started analyzing a viable i18n declarationoutWriter
- the buffered writer where we store the output stringinitialOffset
- the initial offsetsource
- The source using the i18npar
- The declaration parametersadditionalParameters
- The additional parameters- Returns:
- the amount of analyzed characters
- Throws:
IOException
- if an error occurs while writing the output
-
_translateKey
private void _translateKey(char[] srcChars, BufferedWriter outWriter, int candidateBeginIdx, int lastIdx, int initialOffset, String locale, Parameters par) throws IOException
Try to translate the key and write the output stream with its translation if found, the key itself if not- Parameters:
srcChars
- the input source as charactersoutWriter
- the string builder where to writecandidateBeginIdx
- the index at which the i18n declaration startedlastIdx
- the last index analyzedinitialOffset
- the amount of characters that we have to write before the i18n declarationlocale
- The locale to usepar
- The declaration parameters- Throws:
IOException
- if an error occurs while writing the output
-
getKey
public Serializable getKey(Source source, Map objectModel, Parameters parameters, Map<String,Object> additionalParameters)
Description copied from interface:ResourceHandler
Get the unique key for this resource, for cache purpose.- Specified by:
getKey
in interfaceResourceHandler
- Overrides:
getKey
in classAbstractResourceHandler
- Parameters:
source
- The sourceobjectModel
- The object modelparameters
- The parametersadditionalParameters
- Additional parameters- Returns:
- The cache key.
-
-