de.folt.models.datamodel.microsofttranslate
Class MicrosoftTranslate

java.lang.Object
  extended by java.util.Observable
      extended by de.folt.models.datamodel.BasicDataSource
          extended by de.folt.models.datamodel.microsofttranslate.MicrosoftTranslate
All Implemented Interfaces:
DataSource, java.util.Enumeration<MultiLingualObject>, java.util.Observer

public class MicrosoftTranslate
extends BasicDataSource

This class implements a data source which allows to translate a text with Microsoft translate. See http://msdn.microsoft.com/en-us/library/dd576286.aspx or http://sdk.microsofttranslator.com/HTTP/HTTPDemo2.aspx

  Example 
C#
    string translateUri = "http://api.microsofttranslator.com/V1/Http.svc/Translate?appId=myAppId&from=en&to=es";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(translateUri);
    // Request must be HTTP POST to include translation text 
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "text/plain";
    byte[] bytes = Encoding.ASCII.GetBytes("Translate this text");
    Stream os = null;

    try
    {
        // Text is inserted into the body of the request 
        httpWebRequest.ContentLength = bytes.Length;
        os = httpWebRequest.GetRequestStream();
        os.Write(bytes, 0, bytes.Length);
    }
    finally
    {
        // Close stream if successful 
        if (os != null)
        {
            os.Close();
        }
    }

    string output;
    try
    {
        WebResponse response = httpWebRequest.GetResponse();
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        output = reader.ReadToEnd();
    }
    catch (Exception ex)
    {
        // An error may be thrown if the request body is not recognizable as text 
        // An error may be thrown if the from and to parameters are the same 
        output = "Error - " + ex.Message;
    }

    Console.WriteLine("Result: " + output);   
 


Nested Class Summary
 
Nested classes/interfaces inherited from class de.folt.models.datamodel.BasicDataSource
BasicDataSource.BasicDataSourceObserver
 
Constructor Summary
MicrosoftTranslate()
           
MicrosoftTranslate(DataSourceProperties dataSourceProperties)
           
 
Method Summary
 boolean createDataSource(DataSourceProperties dataModelProperties)
          createDataSource creates a new Data source; this is esp. intended for creating a new database, e.g. in MySQL or Ms SQL Server.
 java.lang.String getDataSourceType()
          getDataSourceType get the type of the database
static void main(java.lang.String[] args)
          main
 org.jdom.Element translate(org.jdom.Element transUnit, org.jdom.Element file, XliffDocument xliffDocument, java.lang.String sourceLanguage, java.lang.String targetLanguage, int matchSimilarity, java.util.Hashtable<java.lang.String,java.lang.Object> translationParameters)
          translate translates a trans-unit given the source language, target Language and match similarity
 java.lang.String translate(java.lang.String segment, java.lang.String sourceLanguage, java.lang.String targetLanguage)
           
 
Methods inherited from class de.folt.models.datamodel.BasicDataSource
addData, addMonoLingualObject, addMultiLingualObject, bAuthenticate, bPersist, bSupportMultiThreading, changedMonolingualObjects, checkIfTranslationExistsInDataSource, checkIfTranslationExistsInDataSource, cleanDataSource, clearDataSource, containsKey, containsValue, copyFrom, copyTo, currentTimeMillis, deleteDataSource, exportTmxFile, exportXliffFile, getAllAttributes, getChangedIds, getData, getDataSourceName, getDataSourceProperties, getDefaultDataSourceConfigurationsFileName, getFuzzyTree, getIds, getILogLevel, getLastErrorCode, getMonoLingualObjectFromId, getMonoLingualObjectFromUniqueId, getMultiLingualObjectCache, getMultiLingualObjectFromId, getMultiLingualObjectFromUniqueId, getUniqueIds, hasMoreElements, importTbxFile, importTmxFile, importXliffFile, initEnumeration, isBChanged, isBLoadAttributesLazy, isSyncDataSource, nextElement, removeData, removeDataSource, removeMonoLingualObject, removeMultiLingualObject, runFilterMethod, saveModifiedMonoLingualObject, saveModifiedMultiLingualObject, search, searchRegExp, searchWordBased, setBChanged, setBLoadAttributesLazy, setDataSourceType, setDefaultDataSourceConfigurationsFileName, setILogLevel, setILogLevel, setLastErrorCode, setMultiLingualObjectCache, setOpenTMSPropertiesFile, subSegmentResultsToGlossary, subSegmentTranslate, update, update
 
Methods inherited from class java.util.Observable
addObserver, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MicrosoftTranslate

public MicrosoftTranslate()

MicrosoftTranslate

public MicrosoftTranslate(DataSourceProperties dataSourceProperties)
Parameters:
dataSourceProperties -
Method Detail

main

public static void main(java.lang.String[] args)
main

Parameters:
args -

getDataSourceType

public java.lang.String getDataSourceType()
Description copied from interface: DataSource
getDataSourceType get the type of the database

Specified by:
getDataSourceType in interface DataSource
Overrides:
getDataSourceType in class BasicDataSource
Returns:
the data source type

translate

public org.jdom.Element translate(org.jdom.Element transUnit,
                                  org.jdom.Element file,
                                  XliffDocument xliffDocument,
                                  java.lang.String sourceLanguage,
                                  java.lang.String targetLanguage,
                                  int matchSimilarity,
                                  java.util.Hashtable<java.lang.String,java.lang.Object> translationParameters)
                           throws OpenTMSException
Description copied from interface: DataSource
translate translates a trans-unit given the source language, target Language and match similarity

Specified by:
translate in interface DataSource
Overrides:
translate in class BasicDataSource
Parameters:
transUnit - the trans unit to translate to use
file - the file element currently to translate
xliffDocument - the basic xliff document
sourceLanguage - the source language to use
targetLanguage - the target language to use
matchSimilarity - the similarity (fuzzy) match quality (0 - 100) to use
translationParameters - the hash table contains parameters which control some parameters, e.g. should header/source/target properties be written to alt-trans
Returns:
the modified trans-unit with new translation
Throws:
OpenTMSException

createDataSource

public boolean createDataSource(DataSourceProperties dataModelProperties)
                         throws OpenTMSException
Description copied from interface: DataSource
createDataSource creates a new Data source; this is esp. intended for creating a new database, e.g. in MySQL or Ms SQL Server. In addition it can be used to create a new empty TMX data source.

Specified by:
createDataSource in interface DataSource
Overrides:
createDataSource in class BasicDataSource
Parameters:
dataModelProperties - the parameters of the data source
Returns:
true if success
Throws:
OpenTMSException

translate

public java.lang.String translate(java.lang.String segment,
                                  java.lang.String sourceLanguage,
                                  java.lang.String targetLanguage)