KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > translations > Translation


1 /*
2  * Created on Dec 6, 2005
3  */

4 package com.openedit.modules.translations;
5
6 import java.io.IOException JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Comparator JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import org.apache.commons.httpclient.HttpClient;
15 import org.apache.commons.httpclient.HttpException;
16 import org.apache.commons.httpclient.NameValuePair;
17 import org.apache.commons.httpclient.methods.PostMethod;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import com.openedit.page.PageProperty;
22
23 public class Translation
24 {
25     protected List JavaDoc fieldLanguages;
26     protected String JavaDoc fieldSelectedLang;
27     private static final Log log = LogFactory.getLog(Translation.class);
28     public List JavaDoc getLanguages()
29     {
30         if (fieldLanguages == null)
31         {
32             fieldLanguages = new ArrayList JavaDoc();
33         }
34         return fieldLanguages;
35     }
36     public List JavaDoc getEditLanguages()
37     {
38         if( getLanguages().size() == 0)
39         {
40             return Collections.EMPTY_LIST;
41         }
42         
43         List JavaDoc sub = new ArrayList JavaDoc();
44         for (Iterator JavaDoc iter = getLanguages().iterator(); iter.hasNext();)
45         {
46             Language lang = (Language) iter.next();
47             if( lang.getRootDirectory().length() > 0)
48             {
49                 sub.add(lang);
50             }
51         }
52         return sub;
53     }
54
55     public void setLanguages(List JavaDoc inLanguages)
56     {
57         fieldLanguages = inLanguages;
58     }
59
60     public void addLanguage(Language inLang)
61     {
62         getLanguages().add(inLang);
63     }
64     public boolean isSelected(Language inLang)
65     {
66         if( inLang.getId().equals(getSelectedLang()))
67         {
68             return true;
69         }
70         return false;
71     }
72     public String JavaDoc getSelectedLang()
73     {
74         return fieldSelectedLang;
75     }
76
77     public void setSelectedLang(String JavaDoc inSelectedLocale)
78     {
79         fieldSelectedLang = inSelectedLocale;
80     }
81
82     public void sort()
83     {
84         Collections.sort(getLanguages(), new Comparator JavaDoc()
85         {
86             public int compare(Object JavaDoc inO1, Object JavaDoc inO2)
87             {
88                 Language lang1 = (Language)inO1;
89                 Language lang2 = (Language)inO2;
90                 return lang1.getId().compareTo(lang2.getId());
91             }
92         });
93     }
94
95     public Language getLanguage(String JavaDoc inEid)
96     {
97         for (Iterator JavaDoc iter = getLanguages().iterator(); iter.hasNext();)
98         {
99             Language lang = (Language) iter.next();
100             if( lang.getId().equals(inEid))
101             {
102                 return lang;
103             }
104         }
105         return null;
106     }
107
108     public void removeLanguage(Language inSelectedlan)
109     {
110         getLanguages().remove(inSelectedlan);
111         setSelectedLang(null);
112     }
113     
114     public List JavaDoc webTranslateProperties(Map JavaDoc inProperties, String JavaDoc inLocale)
115     {
116         for (Iterator JavaDoc iter = inProperties.keySet().iterator(); iter.hasNext();)
117         {
118             String JavaDoc name = (String JavaDoc) iter.next();
119             PageProperty property = (PageProperty)inProperties.get(name);
120             
121             if ((name.startsWith("text.") || name.equals("title"))
122                     && property.getValues().get(inLocale) == null)
123             {
124                 String JavaDoc text = property.getValue();
125                 String JavaDoc translated = webTranslate(text, inLocale);
126             
127                 property.setValue(translated, inLocale);
128             }
129         }
130          
131         return createTranslationList(inProperties, inLocale);
132     }
133
134     public String JavaDoc webTranslate(String JavaDoc text, String JavaDoc inLocale)
135     {
136         String JavaDoc googlePage = "http://translate.google.com/translate_t";
137         String JavaDoc languagePair = "en|" + inLocale;
138         
139         HttpClient client = new HttpClient();
140         PostMethod method = new PostMethod( googlePage );
141         method.addParameter(new NameValuePair("text", text));
142         method.addParameter(new NameValuePair("langpair", languagePair));
143         method.addParameter(new NameValuePair("hl", "en"));
144         method.addParameter(new NameValuePair("ie", "UTF8"));
145
146         String JavaDoc translated = null;
147         try
148         {
149             int statusCode = client.executeMethod( method );
150         
151             if( statusCode != -1 )
152             {
153               String JavaDoc contents = method.getResponseBodyAsString();
154               method.releaseConnection();
155                 
156               int begin = contents.indexOf("<div id=result_box");
157               int end = contents.indexOf("</div>", begin + 1);
158               if (begin > 0 && end > 0)
159               {
160                   String JavaDoc textarea = contents.substring(begin, end);
161                   begin = textarea.indexOf(">") + 1;
162                   translated = textarea.substring(begin).trim();
163               }
164               else
165               {
166                   log.error("Bad response to translation request. " + contents);
167               }
168             }
169         } catch (HttpException e)
170         {
171             e.printStackTrace();
172         } catch (IOException JavaDoc e)
173         {
174             e.printStackTrace();
175         }
176         return translated;
177     }
178     
179 // This method takes a map of page properties and returns a list containing triples of:
180
//1. The name of the property
181
//2. The default version of the property
182
//3. The version of the property in the language specified by inLocale
183
private List JavaDoc createTranslationList(Map JavaDoc inProperties, String JavaDoc inLocale)
184     {
185         List JavaDoc transList = new ArrayList JavaDoc();
186         for (Iterator JavaDoc iter = inProperties.keySet().iterator(); iter.hasNext();)
187         {
188             String JavaDoc name = (String JavaDoc) iter.next();
189             if (name.startsWith("text.") || name.equals("title"))
190             {
191                 PageProperty property = (PageProperty)inProperties.get(name);
192                 List JavaDoc propertyTriple = new ArrayList JavaDoc();
193                 propertyTriple.add(name);
194                 propertyTriple.add(property.getValue());
195                 propertyTriple.add(property.getValue(inLocale));
196                 transList.add(propertyTriple);
197             }
198         }
199         return transList;
200     }
201
202 }
203
Popular Tags