суббота, 27 марта 2010 г.

Using Google Translate in ABAP

*&---------------------------------------------------------------------*
*& Report  ZSIT_GOOGLE_TRANSLATE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsit_google_translate.

*&---------------------------------------------------------------------*
*&      Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS p_text   TYPE text100 DEFAULT 'Hello World! Using Webservice in SAP ABAP!'.
PARAMETERS p_src    TYPE zesit_net310_google_lang AS LISTBOX VISIBLE LENGTH 15 DEFAULT 'EN' .
PARAMETERS p_trg    TYPE zesit_net310_google_lang AS LISTBOX VISIBLE LENGTH 15 DEFAULT 'RU' .
* I create ED and Domain zesit_net310_google_lang like char2
* with values EN and RU
*&---------------------------------------------------------------------*
*&      Types and Data
*&---------------------------------------------------------------------*
DATA:  http_client    TYPE REF TO if_http_client ,
       http_url       TYPE string                ,
       p_content      TYPE string                ,
       p_result       TYPE string                .

*&---------------------------------------------------------------------*
*&      Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .

  CONCATENATE 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q='
              p_text '&'
              'langpair=' p_src '|' p_trg
              INTO http_url.

* Creation of new IF_HTTP_Client object
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = http_url
    IMPORTING
      client             = http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.

  http_client->request->set_header_field( name  = '~request_method'
                                          value = 'GET' ).
* Send the request
  http_client->send( ).

* Reterive the result
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.
* Get character feed
  p_content = http_client->response->get_cdata( ).

  p_result = p_content.
* Processing string for result
  SHIFT p_content LEFT UP TO '{"translatedText":"'.
  SHIFT p_content LEFT BY 19 PLACES.
  SPLIT p_content AT '"}' INTO p_result p_content.

  write:/ 'Источник = ', p_text,
        / 'Перевод  = ', p_result.

Комментариев нет: