Package org.ametys.core.resources
Class I18nTextResourceHandler
- java.lang.Object
-
- org.ametys.runtime.plugin.component.AbstractLogEnabled
-
- org.ametys.core.resources.DefaultResourceHandler
-
- org.ametys.core.resources.I18nTextResourceHandler
-
- All Implemented Interfaces:
ResourceHandler
,LogEnabled
,Contextualizable
,Serviceable
- Direct Known Subclasses:
JSResourceHandler
public class I18nTextResourceHandler extends DefaultResourceHandler implements Contextualizable
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 Pattern
__LOCALE_PATTERN
protected Context
_context
The application contextprivate I18nUtils
_i18nUtils
Avalon component gathering utility methods concerningI18nizableText
, allowing their translation in several languagesprivate boolean
_isDeclarationValid
Is the last analyzed i18n declaration valid ?private String
_locale
-
Fields inherited from class org.ametys.core.resources.DefaultResourceHandler
_objectModel, _parameters, _readForDownload, _requestedLocation, _resolver, _source
-
-
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)
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)
Try to translate the key and write the output stream with its translation if found, the key itself if notvoid
contextualize(Context context)
void
generate(OutputStream out)
Generate the resource configured during setup, and output itSerializable
getKey()
Get the unique key for this resource, for cache purpose.protected String
getLocale()
Retrieve the locale from the parametersvoid
service(ServiceManager serviceManager)
Source
setup(String location, Map objectModel, Parameters parameters, boolean readForDownload)
Initialize the resource handler with a resource.-
Methods inherited from class org.ametys.core.resources.DefaultResourceHandler
getLastModified, getLength, getMimeType, getValidity
-
Methods inherited from class org.ametys.runtime.plugin.component.AbstractLogEnabled
getLogger, setLogger
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.ametys.core.resources.ResourceHandler
shouldUseSourceContentLength
-
-
-
-
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_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
-
contextualize
public void contextualize(Context context) throws ContextException
- Specified by:
contextualize
in interfaceContextualizable
- Throws:
ContextException
-
service
public void service(ServiceManager serviceManager) throws ServiceException
- Specified by:
service
in interfaceServiceable
- Overrides:
service
in classDefaultResourceHandler
- Throws:
ServiceException
-
setup
public Source setup(String location, Map objectModel, Parameters parameters, boolean readForDownload) throws ProcessingException, IOException
Description copied from interface:ResourceHandler
Initialize the resource handler with a resource.- Specified by:
setup
in interfaceResourceHandler
- Overrides:
setup
in classDefaultResourceHandler
- Parameters:
location
- The source uriobjectModel
- The object modelparameters
- The parametersreadForDownload
- if the resource is to be downloaded and not rendered.- Returns:
- the resolved source
- Throws:
ProcessingException
- If an error occursIOException
- If an error occurs
-
getLocale
protected String getLocale()
Retrieve the locale from the parameters- Returns:
- The locale, or null
-
generate
public void generate(OutputStream out) throws IOException, ProcessingException
Description copied from interface:ResourceHandler
Generate the resource configured during setup, and output it- Specified by:
generate
in interfaceResourceHandler
- Overrides:
generate
in classDefaultResourceHandler
- Parameters:
out
- The output stream to write to- 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) 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 offset- 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) 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 declaration- Throws:
IOException
- if an error occurs while writing the output
-
getKey
public Serializable getKey()
Description copied from interface:ResourceHandler
Get the unique key for this resource, for cache purpose.- Specified by:
getKey
in interfaceResourceHandler
- Overrides:
getKey
in classDefaultResourceHandler
- Returns:
- The cache key.
-
-