Top Description Fields Constructors Methods
org.apache.tools.ant.taskdefs.optional.i18n

public Class Translate

extends MatchingTask
Class Inheritance
Imports
java.io.BufferedReader, .BufferedWriter, .File, .IOException, .InputStream, .InputStreamReader, .OutputStreamWriter, java.nio.file.Files, java.util.Hashtable, .List, .Locale, .Map, .Vector, org.apache.tools.ant.BuildException, .DirectoryScanner, .Project, org.apache.tools.ant.taskdefs.MatchingTask, org.apache.tools.ant.types.FileSet, org.apache.tools.ant.util.FileUtils, .LineTokenizer

Translates text embedded in files using Resource Bundle files. Since ant 1.6 preserves line endings

Field Summary

Modifier and TypeField and Description
private String
bundle

Family name of resource bundle

private static final int
BUNDLE_DEFAULT_LANGUAGE

search a bundle matching the language only of the current locale of the computer

private static final int
BUNDLE_DEFAULT_LANGUAGE_COUNTRY

search a bundle matching the language, and the country of the current locale of the computer

private static final int
BUNDLE_DEFAULT_LANGUAGE_COUNTRY_VARIANT

search a bundle matching the language, the country and the variant of the current locale of the computer

private static final int
BUNDLE_MAX_ALTERNATIVES

number of possibilities for the search

private static final int
BUNDLE_NOMATCH

search a bundle matching nothing special

private static final int
BUNDLE_SPECIFIED_LANGUAGE

search a bundle matching the specified language only

private static final int
BUNDLE_SPECIFIED_LANGUAGE_COUNTRY

search a bundle matching the specified language, and the country

private static final int
BUNDLE_SPECIFIED_LANGUAGE_COUNTRY_VARIANT

search a bundle matching the specified language, the country and the variant

private String
bundleCountry

Locale specific country of the resource bundle

private String
bundleEncoding

Resource Bundle file encoding scheme, defaults to srcEncoding

private String
bundleLanguage

Locale specific language of the resource bundle

private long[]
bundleLastModified

Last Modified Timestamp of resource bundle file being used.

private String
bundleVariant

Locale specific variant of the resource bundle

private String
destEncoding

Destination file encoding scheme

private long
destLastModified

Last Modified Timestamp of destination file being used.

private String
endToken

Ending token to identify keys

private static final FileUtils
FILE_UTILS

Used to resolve file names.

private List<FileSet>
filesets

Vector to hold source file sets.

private boolean
forceOverwrite

Whether or not to create a new destination file.

private boolean
loaded

Has at least one file from the bundle been loaded?

private Map<String, String>
resourceMap

Holds key value pairs loaded from resource bundle file

private String
srcEncoding

Source file encoding scheme

private long
srcLastModified

Last Modified Timestamp of source file being used.

private String
startToken

Starting token to identify keys

private File
toDir

Destination directory

Inherited from org.apache.tools.ant.taskdefs.MatchingTask:
fileset

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addFileset(FileSet
the fileset to be added
set
)

Adds a set of files to translate as a nested fileset element.

public void
execute()

Overrides org.apache.tools.ant.Task.execute.

Check attributes values, load resource map and translate
private void
loadResourceMap(InputStream ins)

Load resourceMap with key value pairs.

private void
loadResourceMaps()

Load resource maps based on resource bundle encoding scheme.

private void
processBundle(final String bundleFile, final int i, final boolean checkLoaded)

Process each file that makes up this bundle.

public void
setBundle(String
family name of resource bundle
bundle
)

Sets Family name of resource bundle; required.

public void
setBundleCountry(String
country of the bundle
bundleCountry
)

Sets locale specific country of resource bundle; optional.

public void
setBundleEncoding(String
bundle file encoding scheme
bundleEncoding
)

Sets Resource Bundle file encoding scheme; optional.

public void
setBundleLanguage(String
language of the bundle
bundleLanguage
)

Sets locale specific language of resource bundle; optional.

public void
setBundleVariant(String
locale variant of resource bundle
bundleVariant
)

Sets locale specific variant of resource bundle; optional.

public void
setDestEncoding(String
destination file encoding scheme
destEncoding
)

Sets destination file encoding scheme; optional.

public void
setEndToken(String
ending token to identify keys
endToken
)

Sets ending token to identify keys; required.

public void
setForceOverwrite(boolean
whether or not to overwrite existing files
forceOverwrite
)

Whether or not to overwrite existing file irrespective of whether it is newer than the source file as well as the resource bundle file.

public void
setSrcEncoding(String
source file encoding
srcEncoding
)

Sets source file encoding scheme; optional, defaults to encoding of local system.

public void
setStartToken(String
starting token to identify keys
startToken
)

Sets starting token to identify keys; required.

public void
setToDir(File
destination directory
toDir
)

Sets Destination directory; required.

private void
translate()

Reads source file line by line using the source encoding and searches for keys that are sandwiched between the startToken and endToken.

private void
Inherited from org.apache.tools.ant.taskdefs.MatchingTask:
addaddAndaddContainsaddContainsRegexpaddCustomaddDateaddDependaddDepthaddDifferentaddFilenameaddMajorityaddModifiedaddNoneaddNotaddOraddPresentaddSelectoraddSizeaddTypeappendSelectorcreateExcludecreateExcludesFilecreateIncludecreateIncludesFilecreatePatternSetgetDirectoryScannergetImplicitFileSetgetSelectorshasSelectorsselectorCountselectorElementssetCaseSensitivesetDefaultexcludessetExcludessetExcludesfilesetFollowSymlinkssetIncludessetIncludesfilesetProjectXsetIgnoreXsetItems

Field Detail

bundleback to summary
private String bundle

Family name of resource bundle

BUNDLE_DEFAULT_LANGUAGEback to summary
private static final int BUNDLE_DEFAULT_LANGUAGE

search a bundle matching the language only of the current locale of the computer

BUNDLE_DEFAULT_LANGUAGE_COUNTRYback to summary
private static final int BUNDLE_DEFAULT_LANGUAGE_COUNTRY

search a bundle matching the language, and the country of the current locale of the computer

BUNDLE_DEFAULT_LANGUAGE_COUNTRY_VARIANTback to summary
private static final int BUNDLE_DEFAULT_LANGUAGE_COUNTRY_VARIANT

search a bundle matching the language, the country and the variant of the current locale of the computer

BUNDLE_MAX_ALTERNATIVESback to summary
private static final int BUNDLE_MAX_ALTERNATIVES

number of possibilities for the search

BUNDLE_NOMATCHback to summary
private static final int BUNDLE_NOMATCH

search a bundle matching nothing special

BUNDLE_SPECIFIED_LANGUAGEback to summary
private static final int BUNDLE_SPECIFIED_LANGUAGE

search a bundle matching the specified language only

BUNDLE_SPECIFIED_LANGUAGE_COUNTRYback to summary
private static final int BUNDLE_SPECIFIED_LANGUAGE_COUNTRY

search a bundle matching the specified language, and the country

BUNDLE_SPECIFIED_LANGUAGE_COUNTRY_VARIANTback to summary
private static final int BUNDLE_SPECIFIED_LANGUAGE_COUNTRY_VARIANT

search a bundle matching the specified language, the country and the variant

bundleCountryback to summary
private String bundleCountry

Locale specific country of the resource bundle

bundleEncodingback to summary
private String bundleEncoding

Resource Bundle file encoding scheme, defaults to srcEncoding

bundleLanguageback to summary
private String bundleLanguage

Locale specific language of the resource bundle

bundleLastModifiedback to summary
private long[] bundleLastModified

Last Modified Timestamp of resource bundle file being used.

bundleVariantback to summary
private String bundleVariant

Locale specific variant of the resource bundle

destEncodingback to summary
private String destEncoding

Destination file encoding scheme

destLastModifiedback to summary
private long destLastModified

Last Modified Timestamp of destination file being used.

endTokenback to summary
private String endToken

Ending token to identify keys

FILE_UTILSback to summary
private static final FileUtils FILE_UTILS

Used to resolve file names.

filesetsback to summary
private List<FileSet> filesets

Vector to hold source file sets.

forceOverwriteback to summary
private boolean forceOverwrite

Whether or not to create a new destination file. Defaults to false.

loadedback to summary
private boolean loaded

Has at least one file from the bundle been loaded?

resourceMapback to summary
private Map<String, String> resourceMap

Holds key value pairs loaded from resource bundle file

srcEncodingback to summary
private String srcEncoding

Source file encoding scheme

srcLastModifiedback to summary
private long srcLastModified

Last Modified Timestamp of source file being used.

startTokenback to summary
private String startToken

Starting token to identify keys

toDirback to summary
private File toDir

Destination directory

Constructor Detail

Translateback to summary
public Translate()

Method Detail

addFilesetback to summary
public void addFileset(FileSet set)

Adds a set of files to translate as a nested fileset element.

Parameters
set:FileSet

the fileset to be added

executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

Check attributes values, load resource map and translate

Annotations
@Override
Exceptions
BuildException:
if the required attributes are not set Required :
  • bundle
  • starttoken
  • endtoken
loadResourceMapback to summary
private void loadResourceMap(InputStream ins) throws BuildException

Load resourceMap with key value pairs. Values of existing keys are not overwritten. Bundle's encoding scheme is used.

loadResourceMapsback to summary
private void loadResourceMaps() throws BuildException

Load resource maps based on resource bundle encoding scheme. The resource bundle lookup searches for resource files with various suffixes on the basis of (1) the desired locale and (2) the default locale (basebundlename), in the following order from lower-level (more specific) to parent-level (less specific): basebundlename + "_" + language1 + "_" + country1 + "_" + variant1 basebundlename + "_" + language1 + "_" + country1 basebundlename + "_" + language1 basebundlename basebundlename + "_" + language2 + "_" + country2 + "_" + variant2 basebundlename + "_" + language2 + "_" + country2 basebundlename + "_" + language2 To the generated name, a ".properties" string is appended and once this file is located, it is treated just like a properties file but with bundle encoding also considered while loading.

processBundleback to summary
private void processBundle(final String bundleFile, final int i, final boolean checkLoaded) throws BuildException

Process each file that makes up this bundle.

setBundleback to summary
public void setBundle(String bundle)

Sets Family name of resource bundle; required.

Parameters
bundle:String

family name of resource bundle

setBundleCountryback to summary
public void setBundleCountry(String bundleCountry)

Sets locale specific country of resource bundle; optional.

Parameters
bundleCountry:String

country of the bundle

setBundleEncodingback to summary
public void setBundleEncoding(String bundleEncoding)

Sets Resource Bundle file encoding scheme; optional. Defaults to source file encoding

Parameters
bundleEncoding:String

bundle file encoding scheme

setBundleLanguageback to summary
public void setBundleLanguage(String bundleLanguage)

Sets locale specific language of resource bundle; optional.

Parameters
bundleLanguage:String

language of the bundle

setBundleVariantback to summary
public void setBundleVariant(String bundleVariant)

Sets locale specific variant of resource bundle; optional.

Parameters
bundleVariant:String

locale variant of resource bundle

setDestEncodingback to summary
public void setDestEncoding(String destEncoding)

Sets destination file encoding scheme; optional. Defaults to source file encoding

Parameters
destEncoding:String

destination file encoding scheme

setEndTokenback to summary
public void setEndToken(String endToken)

Sets ending token to identify keys; required.

Parameters
endToken:String

ending token to identify keys

setForceOverwriteback to summary
public void setForceOverwrite(boolean forceOverwrite)

Whether or not to overwrite existing file irrespective of whether it is newer than the source file as well as the resource bundle file. Defaults to false.

Parameters
forceOverwrite:boolean

whether or not to overwrite existing files

setSrcEncodingback to summary
public void setSrcEncoding(String srcEncoding)

Sets source file encoding scheme; optional, defaults to encoding of local system.

Parameters
srcEncoding:String

source file encoding

setStartTokenback to summary
public void setStartToken(String startToken)

Sets starting token to identify keys; required.

Parameters
startToken:String

starting token to identify keys

setToDirback to summary
public void setToDir(File toDir)

Sets Destination directory; required.

Parameters
toDir:File

destination directory

translateback to summary
private void translate() throws BuildException

Reads source file line by line using the source encoding and searches for keys that are sandwiched between the startToken and endToken. The values for these keys are looked up from the hashtable and substituted. If the hashtable doesn't contain the key, they key itself is used as the value. Destination files and directories are created as needed. The destination file is overwritten only if the forceoverwritten attribute is set to true if the source file or any associated bundle resource file is newer than the destination file.

translateOneFileback to summary
private void translateOneFile(File src, File dest) throws IOException