📄 translate.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs.optional.i18n;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.Hashtable;import java.util.Locale;import java.util.Vector;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.DirectoryScanner;import org.apache.tools.ant.Project;import org.apache.tools.ant.taskdefs.MatchingTask;import org.apache.tools.ant.types.FileSet;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.LineTokenizer;/** * Translates text embedded in files using Resource Bundle files. * Since ant 1.6 preserves line endings * */public class Translate extends MatchingTask { /** * search a bundle matching the specified language, the country and the variant */ private static final int BUNDLE_SPECIFIED_LANGUAGE_COUNTRY_VARIANT = 0; /** * search a bundle matching the specified language, and the country */ private static final int BUNDLE_SPECIFIED_LANGUAGE_COUNTRY = 1; /** * search a bundle matching the specified language only */ private static final int BUNDLE_SPECIFIED_LANGUAGE = 2; /** * search a bundle matching nothing special */ private static final int BUNDLE_NOMATCH = 3; /** * search a bundle matching the language, the country and the variant * of the current locale of the computer */ private static final int BUNDLE_DEFAULT_LANGUAGE_COUNTRY_VARIANT = 4; /** * search a bundle matching the language, and the country * of the current locale of the computer */ private static final int BUNDLE_DEFAULT_LANGUAGE_COUNTRY = 5; /** * search a bundle matching the language only * of the current locale of the computer */ private static final int BUNDLE_DEFAULT_LANGUAGE = 6; /** * number of possibilities for the search */ private static final int BUNDLE_MAX_ALTERNATIVES = BUNDLE_DEFAULT_LANGUAGE + 1; /** * Family name of resource bundle */ private String bundle; /** * Locale specific language of the resource bundle */ private String bundleLanguage; /** * Locale specific country of the resource bundle */ private String bundleCountry; /** * Locale specific variant of the resource bundle */ private String bundleVariant; /** * Destination directory */ private File toDir; /** * Source file encoding scheme */ private String srcEncoding; /** * Destination file encoding scheme */ private String destEncoding; /** * Resource Bundle file encoding scheme, defaults to srcEncoding */ private String bundleEncoding; /** * Starting token to identify keys */ private String startToken; /** * Ending token to identify keys */ private String endToken; /** * Whether or not to create a new destination file. * Defaults to <code>false</code>. */ private boolean forceOverwrite; /** * Vector to hold source file sets. */ private Vector filesets = new Vector(); /** * Holds key value pairs loaded from resource bundle file */ private Hashtable resourceMap = new Hashtable(); /** * Used to resolve file names. */ private static final FileUtils FILE_UTILS = FileUtils.getFileUtils(); /** * Last Modified Timestamp of resource bundle file being used. */ private long[] bundleLastModified = new long[BUNDLE_MAX_ALTERNATIVES]; /** * Last Modified Timestamp of source file being used. */ private long srcLastModified; /** * Last Modified Timestamp of destination file being used. */ private long destLastModified; /** * Has at least one file from the bundle been loaded? */ private boolean loaded = false; /** * Sets Family name of resource bundle; required. * @param bundle family name of resource bundle */ public void setBundle(String bundle) { this.bundle = bundle; } /** * Sets locale specific language of resource bundle; optional. * @param bundleLanguage langage of the bundle */ public void setBundleLanguage(String bundleLanguage) { this.bundleLanguage = bundleLanguage; } /** * Sets locale specific country of resource bundle; optional. * @param bundleCountry country of the bundle */ public void setBundleCountry(String bundleCountry) { this.bundleCountry = bundleCountry; } /** * Sets locale specific variant of resource bundle; optional. * @param bundleVariant locale variant of resource bundle */ public void setBundleVariant(String bundleVariant) { this.bundleVariant = bundleVariant; } /** * Sets Destination directory; required. * @param toDir destination directory */ public void setToDir(File toDir) { this.toDir = toDir; } /** * Sets starting token to identify keys; required. * @param startToken starting token to identify keys */ public void setStartToken(String startToken) { this.startToken = startToken; } /** * Sets ending token to identify keys; required. * @param endToken ending token to identify keys */ public void setEndToken(String endToken) { this.endToken = endToken; } /** * Sets source file encoding scheme; optional, * defaults to encoding of local system. * @param srcEncoding source file encoding */ public void setSrcEncoding(String srcEncoding) { this.srcEncoding = srcEncoding; } /** * Sets destination file encoding scheme; optional. Defaults to source file * encoding * @param destEncoding destination file encoding scheme */ public void setDestEncoding(String destEncoding) { this.destEncoding = destEncoding; } /** * Sets Resource Bundle file encoding scheme; optional. Defaults to source file * encoding * @param bundleEncoding bundle file encoding scheme */ public void setBundleEncoding(String bundleEncoding) { this.bundleEncoding = bundleEncoding; } /** * 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. * @param forceOverwrite whether or not to overwrite existing files */ public void setForceOverwrite(boolean forceOverwrite) { this.forceOverwrite = forceOverwrite; } /** * Adds a set of files to translate as a nested fileset element. * @param set the fileset to be added */ public void addFileset(FileSet set) { filesets.addElement(set); } /** * Check attributes values, load resource map and translate * @throws BuildException if the required attributes are not set * Required : <ul> * <li>bundle</li> * <li>starttoken</li> * <li>endtoken</li> * </ul> */ public void execute() throws BuildException { if (bundle == null) { throw new BuildException("The bundle attribute must be set.", getLocation()); } if (startToken == null) { throw new BuildException("The starttoken attribute must be set.", getLocation()); } if (endToken == null) { throw new BuildException("The endtoken attribute must be set.", getLocation()); } if (bundleLanguage == null) { Locale l = Locale.getDefault(); bundleLanguage = l.getLanguage(); } if (bundleCountry == null) { bundleCountry = Locale.getDefault().getCountry(); } if (bundleVariant == null) { Locale l = new Locale(bundleLanguage, bundleCountry); bundleVariant = l.getVariant(); } if (toDir == null) { throw new BuildException("The todir attribute must be set.", getLocation());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -