java.lang.Object
java.io.Reader
java.io.FilterReader
net.i2p.util.TranslateReader
- All Implemented Interfaces:
Closeable, AutoCloseable, Readable
Filters a reader, replacing the gettext-style tags described below as they are read.
Strings are tagged with _("translateme") or _("translate {0} me", "foo")
Max two parameters. String and parameters must be double-quoted (no ngettext, no tagged parameters). Escape quotes inside quote with \". Commas and spaces between args are optional. Entire tag (from '_' to ')') must be on one line. Multiple tags allowed on one line.
Also will extract strings to a dummy java file for postprocessing by xgettext - see main(String[]).
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static enumThe states of the parser, all of which may transition to START.private static classHook writing the tags out as a properties file.private static interfaceCallback handling each translated string.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final StringBuilderPending string or parameter for translation.Parsed string and parameters.private final StringThe bundle being read.private final I2PAppContextThe app context.private TranslateReader.TagHookThe hook handling the tags found.private final StringBuilderParse in progress.private final StringBuilderParsed and translated.private TranslateReader.SCurrent state of the parser.private static final intMost arguments a tag may take.Fields inherited from class FilterReader
inConstructor Summary
ConstructorsConstructorDescriptionTranslateReader(I2PAppContext ctx, String bundle, InputStream in) May be null for tagging only.TranslateReader(I2PAppContext ctx, String bundle, Reader in) May be null for tagging only.Method Summary
Modifier and TypeMethodDescriptionvoidclose()private intflushit()Flush _inBuf to _outBuf, reset state, and return next char or -1.static voidDo not comment out, used to extract tags as a part of the build process.voidmark(int readLimit) booleanprivate intparse()Reads the file and passes the strings to the hook.private intpopit()Return next char from _outBuf or -1.private voidpushit(char c) Put in the pending parse buf.intread()intread(char[] cbuf, int off, int len) booleanready()voidreset()longskip(long n) private static voidTag PO files with translation metadata.private static voidRuns the reader over one file from the command line.private voidClear _inBuf, translate _args to _outBuf, reset state.Methods inherited from class Reader
nullReader, of, read, read, readAllAsString, readAllLines, transferTo
Field Details
_bundle
The bundle being read._ctx
The app context._inBuf
Parse in progress._outBuf
Parsed and translated._argBuf
Pending string or parameter for translation._args
_state
Current state of the parser._hook
The hook handling the tags found.MAX_ARGS
Constructor Details
TranslateReader
May be null for tagging only.- Parameters:
ctx- the context the bundle is looked up throughbundle- may be null for tagging onlyin- UTF-8- Throws:
IOException- declared but never thrown; the stream is only wrapped here
TranslateReader
May be null for tagging only.- Parameters:
ctx- the context the bundle is looked up throughbundle- may be null for tagging onlyin- the reader the tag text is pulled from
Method Details
read
- Overrides:
readin classFilterReader- Throws:
IOException
parse
Reads the file and passes the strings to the hook.- Returns:
- the number of tags processed
- Throws:
IOException- if the file cannot be read
read
- Overrides:
readin classFilterReader- Throws:
IOException
skip
- Overrides:
skipin classFilterReader- Throws:
IOException
ready
- Overrides:
readyin classFilterReader- Throws:
IOException
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterReader- Throws:
IOException
mark
public void mark(int readLimit) - Overrides:
markin classFilterReader
reset
- Overrides:
resetin classFilterReader- Throws:
IOException
markSupported
public boolean markSupported()- Overrides:
markSupportedin classFilterReader
pushit
private void pushit(char c) Put in the pending parse buf.- Parameters:
c- the character to append to the pending input buffer
flushit
private int flushit()Flush _inBuf to _outBuf, reset state, and return next char or -1.- Returns:
- the next character from the output buffer, or -1 when it is empty
popit
private int popit()Return next char from _outBuf or -1.- Returns:
- the next character from the output buffer, or -1 when it is empty
translate
private void translate()Clear _inBuf, translate _args to _outBuf, reset state.main
Do not comment out, used to extract tags as a part of the build process.- Parameters:
args- the subcommand (test or tag) followed by its file arguments
test
Runs the reader over one file from the command line.- Parameters:
file- the file to read- Throws:
IOException- if the file cannot be read
tag
Tag PO files with translation metadata.- Parameters:
files- command line arguments; files[0] is ignored, the rest name the input files and the output file- Throws:
IOException- if a directory listing fails or a file cannot be read or written