1 4 package com.openedit.modules.translations; 5 6 import java.io.IOException ; 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Comparator ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 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 fieldLanguages; 26 protected String fieldSelectedLang; 27 private static final Log log = LogFactory.getLog(Translation.class); 28 public List getLanguages() 29 { 30 if (fieldLanguages == null) 31 { 32 fieldLanguages = new ArrayList (); 33 } 34 return fieldLanguages; 35 } 36 public List getEditLanguages() 37 { 38 if( getLanguages().size() == 0) 39 { 40 return Collections.EMPTY_LIST; 41 } 42 43 List sub = new ArrayList (); 44 for (Iterator 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 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 getSelectedLang() 73 { 74 return fieldSelectedLang; 75 } 76 77 public void setSelectedLang(String inSelectedLocale) 78 { 79 fieldSelectedLang = inSelectedLocale; 80 } 81 82 public void sort() 83 { 84 Collections.sort(getLanguages(), new Comparator () 85 { 86 public int compare(Object inO1, Object 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 inEid) 96 { 97 for (Iterator 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 webTranslateProperties(Map inProperties, String inLocale) 115 { 116 for (Iterator iter = inProperties.keySet().iterator(); iter.hasNext();) 117 { 118 String name = (String ) 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 text = property.getValue(); 125 String translated = webTranslate(text, inLocale); 126 127 property.setValue(translated, inLocale); 128 } 129 } 130 131 return createTranslationList(inProperties, inLocale); 132 } 133 134 public String webTranslate(String text, String inLocale) 135 { 136 String googlePage = "http://translate.google.com/translate_t"; 137 String 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 translated = null; 147 try 148 { 149 int statusCode = client.executeMethod( method ); 150 151 if( statusCode != -1 ) 152 { 153 String 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 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 e) 173 { 174 e.printStackTrace(); 175 } 176 return translated; 177 } 178 179 private List createTranslationList(Map inProperties, String inLocale) 184 { 185 List transList = new ArrayList (); 186 for (Iterator iter = inProperties.keySet().iterator(); iter.hasNext();) 187 { 188 String name = (String ) iter.next(); 189 if (name.startsWith("text.") || name.equals("title")) 190 { 191 PageProperty property = (PageProperty)inProperties.get(name); 192 List propertyTriple = new ArrayList (); 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 |