001: package net.sourceforge.squirrel_sql.plugins.i18n;
002:
003: import net.sourceforge.squirrel_sql.client.IApplication;
004: import net.sourceforge.squirrel_sql.fw.util.StringManager;
005: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
006:
007: import java.io.File;
008: import java.io.FileOutputStream;
009: import java.io.PrintWriter;
010: import java.io.IOException;
011: import java.util.Properties;
012: import java.util.Locale;
013: import java.util.Arrays;
014: import java.util.prefs.Preferences;
015: import java.net.URL;
016:
017: public class I18nBundle implements Comparable<I18nBundle> {
018:
019: private static final StringManager s_stringMgr = StringManagerFactory
020: .getStringManager(I18nProps.class);
021:
022: private I18nProps _defaultProps;
023: private Locale _locale;
024: private File _workDir;
025: private URL[] _sourceUrls;
026: private Integer _missingTranslationsCount;
027:
028: private static final String PREF_KEY_INCLUDE_TIMESTAMP = "SquirrelSQL.i18n.includeTimestamp";
029:
030: /**
031: * The localizations that already exist in SQuirreL.
032: * When missing props are generated for the first time this
033: * Props file will be copied to the work dir.
034: */
035: private I18nProps _localizedProps;
036:
037: public I18nBundle(I18nProps defaultProps, Locale locale,
038: File workDir, URL[] sourceUrls) {
039: _defaultProps = defaultProps;
040: _locale = locale;
041: _workDir = workDir;
042: _sourceUrls = sourceUrls;
043: }
044:
045: private void initMissingTranslationsCount() {
046: if (null != _missingTranslationsCount) {
047: return;
048: }
049:
050: Properties buf = _defaultProps.getTranslateableProperties();
051: if (null != _localizedProps) {
052: _localizedProps.removeProps(buf);
053: }
054:
055: if (null != _workDir) {
056: File pathInWorkDir = getPathRelativeTo(_workDir);
057: if (pathInWorkDir.exists()) {
058: new I18nProps(pathInWorkDir, _sourceUrls)
059: .removeProps(buf);
060: }
061: }
062:
063: _missingTranslationsCount = Integer.valueOf(buf.size());
064: }
065:
066: public void setLocalizedProp(I18nProps localizedProps) {
067: _localizedProps = localizedProps;
068: }
069:
070: public String toString() {
071: return _defaultProps.getPath();
072: }
073:
074: public String getName() {
075: return _defaultProps.getName();
076: }
077:
078: public Integer getMissingTranslationsCount() {
079: initMissingTranslationsCount();
080: return _missingTranslationsCount;
081: }
082:
083: public void writeMissingProps(IApplication app, File workDir) {
084: try {
085: Properties propsToAppend = _defaultProps
086: .getTranslateableProperties();
087:
088: File toAppendTo = getPathRelativeTo(workDir);
089: if (toAppendTo.exists()) {
090: new I18nProps(toAppendTo, _sourceUrls)
091: .removeProps(propsToAppend);
092: } else {
093: toAppendTo.getParentFile().mkdirs();
094: if (null != _localizedProps) {
095: _localizedProps.copyTo(toAppendTo);
096:
097: Object[] params = new Object[] {
098: _localizedProps.getPath(),
099: toAppendTo.getPath() };
100:
101: app.getMessageHandler().showMessage(
102: s_stringMgr.getString("I18n.PropsCopyMsg",
103: params));
104: // i18n[I18n.PropsCopyMsg=Copied existing translations from {0} to {1}]
105:
106: new I18nProps(toAppendTo, _sourceUrls)
107: .removeProps(propsToAppend);
108: }
109: }
110:
111: FileOutputStream fos = new FileOutputStream(toAppendTo,
112: true);
113: PrintWriter pw = new PrintWriter(fos);
114:
115: String includeTimestamp = Preferences.userRoot().get(
116: PREF_KEY_INCLUDE_TIMESTAMP, "true");
117:
118: if (includeTimestamp.equals("true")) {
119: //i18n[I18n.TranlationsGenerationMessage=\n#\n#Missing
120: //translation generated by I18n Plugin on {0}\n#]
121: String msg = s_stringMgr.getString(
122: "I18n.TranlationsGenerationMessage",
123: new java.util.Date());
124: pw.println(msg);
125: }
126:
127: String[] keys = propsToAppend.keySet().toArray(
128: new String[0]);
129: Arrays.sort(keys);
130:
131: for (int i = 0; i < keys.length; i++) {
132: String val = propsToAppend.getProperty(keys[i]);
133:
134: pw.println();
135: pw.println("#" + keys[i] + "="
136: + I18nUtils.normalizePropVal(val));
137: pw.println("#" + keys[i] + "=");
138: }
139:
140: pw.flush();
141: fos.flush();
142:
143: pw.close();
144: fos.close();
145:
146: Object[] params = new Object[] {
147: Integer.valueOf(propsToAppend.size()),
148: toAppendTo.getPath() };
149:
150: app.getMessageHandler()
151: .showMessage(
152: s_stringMgr.getString(
153: "I18n.TranslationsGenerationCount",
154: params));
155: // i18n[I18n.TranslationsGenerationCount=Generated {0} templates to {1}]
156: } catch (IOException e) {
157: throw new RuntimeException(e);
158: }
159:
160: }
161:
162: File getPathRelativeTo(File parentDir) {
163: File toAppendTo = new File(parentDir.getPath() + File.separator
164: + getName());
165: String localizedFileName = _defaultProps
166: .getLocalizedFileName(_locale);
167: toAppendTo = new File(toAppendTo.getParent() + File.separator
168: + localizedFileName);
169: return toAppendTo;
170: }
171:
172: public int compareTo(I18nBundle other) {
173: return getName().compareTo(other.getName());
174: }
175:
176: /**
177: * @see java.lang.Object#hashCode()
178: */
179: @Override
180: public int hashCode() {
181: final int prime = 31;
182: int result = 1;
183: result = prime * result
184: + ((getName() == null) ? 0 : getName().hashCode());
185: return result;
186: }
187:
188: /**
189: * @see java.lang.Object#equals(java.lang.Object)
190: */
191: @Override
192: public boolean equals(Object obj) {
193: if (this == obj)
194: return true;
195: if (obj == null)
196: return false;
197: if (getClass() != obj.getClass())
198: return false;
199: final I18nBundle other = (I18nBundle) obj;
200: if (getName() == null) {
201: if (other.getName() != null)
202: return false;
203: } else if (!getName().equals(other.getName()))
204: return false;
205: return true;
206: }
207:
208: }
|