001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.util;
015:
016: import org.jetbrains.annotations.NonNls;
017: import org.jetbrains.annotations.NotNull;
018: import org.pentaho.reportdesigner.lib.client.util.UncaughtExcpetionsModel;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.URI;
023: import java.net.URISyntaxException;
024: import java.net.URL;
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.Comparator;
028: import java.util.Enumeration;
029: import java.util.List;
030: import java.util.Locale;
031: import java.util.TreeSet;
032: import java.util.jar.JarEntry;
033: import java.util.jar.JarFile;
034: import java.util.logging.Level;
035: import java.util.logging.Logger;
036:
037: /**
038: * User: Martin
039: * Date: 30.08.2006
040: * Time: 19:15:15
041: */
042: public class TranslationUtil {
043: @NotNull
044: @NonNls
045: private static final Logger LOG = Logger
046: .getLogger(TranslationUtil.class.getName());
047:
048: @SuppressWarnings({"HardCodedStringLiteral"})
049: public static void main(@NotNull
050: String[] args) {
051: printLocale(createLocaleFromString("de"));
052: printLocale(createLocaleFromString("de_DE"));
053: printLocale(createLocaleFromString("de_CH"));
054: printLocale(createLocaleFromString("en"));
055: printLocale(createLocaleFromString("en_US_WIN"));
056: printLocale(createLocaleFromString("de__POSIX"));
057: printLocale(createLocaleFromString("fr__MAC"));
058: printLocale(createLocaleFromString("_GB"));
059: }
060:
061: private static void printLocale(@NotNull
062: Locale locale) {
063: //noinspection UseOfSystemOutOrSystemErr
064: System.out.println(locale.getDisplayLanguage() + " _ "
065: + locale.getDisplayCountry() + " _ "
066: + locale.getDisplayVariant());
067: }
068:
069: private TranslationUtil() {
070: }
071:
072: @NotNull
073: public static List<Locale> findLocales() {
074: TreeSet<Locale> locales = new TreeSet<Locale>(
075: new Comparator<Locale>() {
076: public int compare(@NotNull
077: Locale o1, @NotNull
078: Locale o2) {
079: return o1.toString().compareTo(o2.toString());
080: }
081: });
082: //whatever happens, english is supported and the first available locale
083:
084: try {
085: URL url = TranslationUtil.class
086: .getResource("/res/icons/ReportFrameIcon.png");//NON-NLS
087: String urlStr = url.toString();
088: if (LOG.isLoggable(Level.FINE))
089: LOG.log(Level.FINE,
090: "FindTranslationsCommand.execute urlStr = "
091: + urlStr);
092: if (urlStr.startsWith("jar:"))//NON-NLS
093: {
094: //built version
095: int from = "jar:".length();//NON-NLS
096: int to = urlStr.indexOf("/lib/ReportDesigner.jar!/");//NON-NLS
097: String libFolderName = urlStr.substring(from, to)
098: + "/lib/";//NON-NLS
099: String jarFileName = urlStr.substring(from, to)
100: + "/lib/ReportDesigner.jar";//NON-NLS
101:
102: if (LOG.isLoggable(Level.FINE))
103: LOG.log(Level.FINE,
104: "FindTranslationsCommand.execute libFolderName = "
105: + libFolderName);
106: if (LOG.isLoggable(Level.FINE))
107: LOG.log(Level.FINE,
108: "FindTranslationsCommand.execute jarFileName = "
109: + jarFileName);
110:
111: URI jarFileURI = new URI(jarFileName);
112: URI libFolderURI = new URI(libFolderName);
113:
114: JarFile jarFile = new JarFile(new File(jarFileURI));
115: Enumeration<JarEntry> enumeration = jarFile.entries();
116: while (enumeration.hasMoreElements()) {
117: JarEntry jarEntry = enumeration.nextElement();
118: String entryName = jarEntry.getName();
119: if (LOG.isLoggable(Level.FINE))
120: LOG.log(Level.FINE,
121: "FindTranslationsCommand.execute entryName = "
122: + entryName);
123:
124: if (entryName.indexOf("Translations_") != -1
125: && entryName.endsWith(".properties"))//NON-NLS
126: {
127: int start = entryName.indexOf("Translations_")
128: + "Translations_".length();//NON-NLS
129: String localeString = entryName.substring(
130: start, entryName.length()
131: - ".properties".length());//NON-NLS
132: if (LOG.isLoggable(Level.FINE))
133: LOG.log(Level.FINE,
134: "FindTranslationsCommand.execute localeString = "
135: + localeString);
136: locales
137: .add(createLocaleFromString(localeString));
138: }
139: }
140:
141: //scan lib folder
142: File libFolder = new File(new File(libFolderURI), "res");//NON-NLS
143: File[] files = libFolder.listFiles();
144: if (files != null) {
145: for (File file : files) {
146: String entryName = file.getName();
147: //if (LOG.isLoggable(Level.FINE)) LOG.log(Level.FINE, "FindTranslationsCommand.execute entryName = " + entryName);
148: if (entryName.startsWith("Translations_")
149: && entryName.endsWith(".properties"))//NON-NLS
150: {
151: String localeString = entryName.substring(
152: "Translations_".length(), entryName
153: .length()
154: - ".properties".length());//NON-NLS
155: if (LOG.isLoggable(Level.FINE))
156: LOG.log(Level.FINE,
157: "FindTranslationsCommand.execute localeString = "
158: + localeString);
159: locales
160: .add(createLocaleFromString(localeString));
161: }
162: }
163: }
164: } else {
165: //devel version
166: int to = urlStr
167: .indexOf("/res/icons/ReportFrameIcon.png");//NON-NLS
168: String resFolderName = urlStr.substring(0, to)
169: + "/res/";//NON-NLS
170: File resFolder = new File(new URI(resFolderName));
171: File[] files = resFolder.listFiles();
172: if (files != null) {
173: for (File file : files) {
174: String entryName = file.getName();
175: //if (LOG.isLoggable(Level.FINE)) LOG.log(Level.FINE, "FindTranslationsCommand.execute entryName = " + entryName);
176: if (entryName.startsWith("Translations_")
177: && entryName.endsWith(".properties"))//NON-NLS
178: {
179: String localeString = entryName.substring(
180: "Translations_".length(), entryName
181: .length()
182: - ".properties".length());//NON-NLS
183: if (LOG.isLoggable(Level.FINE))
184: LOG.log(Level.FINE,
185: "FindTranslationsCommand.execute localeString = "
186: + localeString);
187: locales
188: .add(createLocaleFromString(localeString));
189: }
190: }
191: }
192: }
193:
194: if (LOG.isLoggable(Level.FINE))
195: LOG.log(Level.FINE,
196: "FindTranslationsCommand.execute locales = "
197: + locales);
198: } catch (URISyntaxException e) {
199: if (LOG.isLoggable(Level.FINE))
200: LOG.log(Level.FINE, "TranslationUtil.findLocales ", e);
201: } catch (IOException e) {
202: if (LOG.isLoggable(Level.FINE))
203: LOG.log(Level.FINE, "TranslationUtil.findLocales ", e);
204: } catch (Throwable throwable) {
205: UncaughtExcpetionsModel.getInstance().addException(
206: throwable);
207: }
208:
209: locales.remove(Locale.ENGLISH);
210:
211: ArrayList<Locale> orderedLocales = new ArrayList<Locale>();
212: orderedLocales.add(Locale.ENGLISH);
213: orderedLocales.addAll(locales);
214:
215: return Collections.unmodifiableList(orderedLocales);
216: }
217:
218: @NotNull
219: public static Locale createLocaleFromString(@NotNull
220: String localeString) {
221: Locale locale;
222: int pos = -1;
223: String temp = localeString.substring(pos + 1);
224: pos = temp.indexOf('_');
225: if (pos == -1) {
226: locale = new Locale(temp, "", "");
227: return locale;
228: }
229:
230: String language = temp.substring(0, pos);
231: temp = temp.substring(pos + 1);
232: pos = temp.indexOf('_');
233: if (pos == -1) {
234: locale = new Locale(language, temp, "");
235: return locale;
236: }
237:
238: String country = temp.substring(0, pos);
239: temp = temp.substring(pos + 1);
240:
241: locale = new Locale(language, country, temp);
242: return locale;
243: }
244: }
|