KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > LocaleComparator


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002,2003 Fraunhofer Gesellschaft
5  * Fraunhofer Institut for Computer Architecture and Software Technology
6  * All Rights Reserved.
7  *
8  * Please visit http://snipsnap.org/ for updates and contact.
9  *
10  * --LICENSE NOTICE--
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  * --LICENSE NOTICE--
25  */

26 package org.snipsnap.util;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Properties JavaDoc;
33 import java.util.TreeSet JavaDoc;
34
35 public class LocaleComparator {
36   public static void main(String JavaDoc args[]) {
37     if(args.length >= 2) {
38       File JavaDoc messagesFile = new File JavaDoc(args[0]);
39       File JavaDoc referenceFile = new File JavaDoc(args[1]);
40       Properties JavaDoc messages = new Properties JavaDoc();
41       Properties JavaDoc reference = new Properties JavaDoc();
42
43       try {
44         messages.load(new FileInputStream JavaDoc(messagesFile));
45         reference.load(new FileInputStream JavaDoc(referenceFile));
46       } catch (IOException JavaDoc e) {
47         System.err.println("Error: "+e.getMessage());
48       }
49
50       Properties JavaDoc changes[] = compareBundles(messages, reference);
51       if(!changes[0].isEmpty()) {
52         System.out.println("There are "+changes[0].size()+" missing messages in '"+messagesFile+"':");
53         Iterator JavaDoc it = new TreeSet JavaDoc(changes[0].keySet()).iterator();
54         while(it.hasNext()) {
55           String JavaDoc key = (String JavaDoc)it.next();
56           System.out.println(key+"\t=\t"+changes[0].getProperty(key));
57         }
58       }
59       if(!changes[1].isEmpty()) {
60         System.out.println("The following " + changes[1].size() + " messages are not translated in '" + messagesFile + "':");
61         System.out.println("Ignore them if they are the same in other languages.");
62         Iterator JavaDoc it = new TreeSet JavaDoc(changes[1].keySet()).iterator();
63         while (it.hasNext()) {
64           String JavaDoc key = (String JavaDoc) it.next();
65           System.out.println(key + "\t=\t" + changes[1].getProperty(key));
66         }
67       }
68     } else {
69       System.err.println("usage: LocaleComparator <messages file> <reference file>");
70     }
71     System.exit(0);
72   }
73
74   public static Properties JavaDoc[] compareBundles(Properties JavaDoc bundle, Properties JavaDoc refBundle) {
75     Properties JavaDoc missingKeys = new Properties JavaDoc();
76     Properties JavaDoc nonTranslatedKeys = new Properties JavaDoc();
77
78     Iterator JavaDoc refIt = refBundle.keySet().iterator();
79     while(refIt.hasNext()) {
80       String JavaDoc key = (String JavaDoc)refIt.next();
81       String JavaDoc value = refBundle.getProperty(key).trim();
82       if(!bundle.containsKey(key)) {
83         missingKeys.setProperty(key, value);
84       } else if(value.equals(bundle.getProperty(key).trim())) {
85         nonTranslatedKeys.setProperty(key, value);
86       }
87     }
88     return new Properties JavaDoc[] { missingKeys, nonTranslatedKeys };
89   }
90
91 }
92
Popular Tags