Multilingual Studio Home > PHP Library > Tutorial

Multilingual Studio PHP Library Tutorial

Contents

  • 0. First steps
  • 1. Using atomic services
  • 2. Samples of atomic services calls
  • 3. Using composite services
  • 0. First steps

    Preliminary Preparation

    In order to use the Language Grid, Language Grid user ID and password are required. Please apply at Language Grid Kyoto Operation Center - Joining the Language Grid if you do not have yours yet.

    Download and Deployment

    Please download the library from the link below. Terms of use can be found in the README-file included in the archive file. The Language Grid PHP Library is compatible with PHP5.3 or higher.

    Unzip the downloaded file, and deploy as follows.

    	
            Application Root
            |- yourfile.php [PHP scripts to use services]
            |- langrid-php-library [Library Root]
                |- MultilingualStudio.php
                |- client
                |- config
                |- ...

    You need to import the library for each client. Please import "MultilingualStudio.php" in the root directory of the library.

    Top↑

    1. Using atomic services

    Atomic services run independently. This example shows how to use a translation service called J-Server (Kyoto-U).

    Service Type Selection and Service Search

    First, please choose the type of service you want to use. Select the service you want to use from Language Grid Service Manager Service Types page. Here we assume that Translation is chosen.

    Next, determine the service to be used. In Language Grid Service Manager Language Services page, if you specify the Service Type as Translation and press the Search button, Services with Service Type: Translation are listed. In this example, we use J-Server (Kyoto-U).

    When you click J-Server (Kyoto-U), details of J-Server (Kyoto-U) service will be displayed. As shown in the column displaying service type (Atomic or Composite), it is an Atomic Service. Also, the WSDL field shows that the URL of WSDL as http://langrid.org/service_manager/wsdl/kyoto1.langrid:KyotoUJServer.

    Check the API

    Service clients with Service Type: Translation are defined as TranslationClient. TranslationClient API page shows that this client has a method called translate.

    Source Code

    Create yourfile.php in the specified location described in 0. First Steps. Conduct the Common Preparations and write the following code. When you run this PHP file, you can conduct text translation using the J-Server (Kyoto-U) service.

        //yourfile.php
        <?php
            // Import Language Grid PHP Library
            require_once dirname(__FILE__).'/langrid-php-library/MultilingualStudio.php';
    
            // Specify the WSDL URL of the service to use
            $url = 'http://langrid.org/service_manager/wsdl/kyoto1.langrid:KyotoUJServer';
    
            try{
                // Specify the appropriate service type (for example Translation) and create a client
                $client = ClientFactory::createTranslationClient($url);
    
                // Set service authentication information ※※Be sure to set UserID and Password※※
                $client->setUserId('someUserId');
                $client->setPassword('somePassword');
                // If the client needs a proxy to connect, please specify as below.
                // $client->setProxy('proxy.yournetwork.org','8080');
    
    
                // Call the service based on the method information in the API manual.
                $result = $client->translate(
                    Language::get('ja'),
                    Language::get('en'),
                    '今日は良い天気です.');
    
                 echo $result;
                 //var_dump($result); 
                 // -> It's fine today.
            } catch(Exception $e) {
                var_dump($e);
            }
        ?>
    
        
    Top↑

    2. Samples of atomic services calls

    Here we introduce two simple samples to call the atomic services.

    2.1 Using BilingualDictionary service

    BilingualDictionary service provides a service to search dictionary data in the service grid.

        <?php
            // Import Language Grid PHP Library
            require_once dirname(__FILE__).'/langrid-php-library/MultilingualStudio.php';
    
            $url = 'http://langrid.org/service_manager/wsdl/Lsd';
    
            try {
                // Create a service client
                $client = ClientFactory::createBilingualDictionaryClient($url);
    
                // Set service authentication information (this can be omitted if you use default authentication in the whole service [described below]).
                $client->setUserId('someUserId');
                $client->setPassword('somePassword');
                
                // If the client needs proxy to connect, please specify as below.
                // $client->setProxy('proxy.yournetwork.org','8080');
    
                // Call the service
                $result = $client->search(Language::get('ja'), Language::get('en'), '科学', MatchingMethod::COMPLETE);
    
                var_dump($result);
                // array(2) { [0]=> string(7) "science" [1]=> string(3) "sci" }
            } catch(Exception $e) {
                var_dump($e);
            }
       ?>
    Top↑

    2.2 Using TextToSpeech service

    TextToSpeech service converts a string in to synthesized speech in the specified language.

        <?php
            // Import Language Grid PHP Library
            require_once dirname(__FILE__).'/langrid-php-library/MultilingualStudio.php';
    
            $url = 'http://langrid.org/service_manager/wsdl/VoiceText';
    
            try {
                // Create a service client
                $client = ClientFactory::createTextToSpeechClient($url);
    
                // Set service authentication information (this can be omitted if you use default authentication in the whole service [described below]).
                $client->setUserId('someUserId');
                $client->setPassword('somePassword');
                // If the client needs proxy to connect, please specify as below.
                // $client->setProxy('proxy.yournetwork.org','8080');
                
                
                // Call the service
                $result = $client->speak(
                    Language::get('ja'),  // Language of the original data (acquired by getSupportedLanguages)
                    'こんにちは',          // Text to be synthesized with voice conversion()
                    'man',               // Type of voice used (acquired by getSupportedVoiceTypes) 
                    'audio/x-wav'        // Format of audio data(acquired by getSupportedAudioTypes)
                );
    
                // Write the received data. Note that you need write permission of the directory in which you create this file.
                // Hello.wav will only be stored in the server which you run this script, and will not be downloaded.
                $fp = fopen("Hello.wav", "w"); 
                fwrite($fp, $result->audio);
                fclose($fp);
    
            } catch(Exception $e) {
                var_dump($e);
            }
       ?>
    Top↑

    3. Using composite services

    Composite service is a service that is composed of multiple atomic services. In order to use composite services, developer needs to select a single composite service, and also specify the additional services that the composite service requires. For example, for the composite service of "translation using a dictionary", developer must specify the "translation service" and "dictionary service".

    The following example shows how to use the composite service Translation with a bilingual dictionary (TranslationCombinedWithBilingualDictionary).

    Select a service type and Search the service

    First, choose the type of service you want to use from Language Grid Service Manager Service Type Page. Here, we choose translation with a dictionary (TranslationWithTemporalDictionary).

    Next, determine the service to use. In Language Grid Service Manager Language Services Page, if you specify Service Type as TranslationWithTemporalDictionary and press the Search button, services with Service Type as TranslationWithTemporalDictionary are listed. Here, Translation Service Combined With Bilingual Dictionary is chosen as the composite service to be used.

    When you click Translation Service Combined With Bilingual Dictionary, details of a composite service, Translation Service Combined With Bilingual Dictionary service are displayed. As shown in the column displaying service type (Atomic or Composite), it is Composite Service. The column "WSDL" shows that the URL of WSDL is http://langrid.org/service_manager/wsdl/kyoto1.langrid:TranslationCombinedWithBilingualDictionary.

    Select the service to bind

    Binding Services are listed in the column of Service in Use. You need to assign a specific service to Invocation Name of the service that is listed. For example, you need to assign specific services such as Google or J-Server translation service to TranslationPL of Invocation Name.

    In TranslationCombinedWithBilingualDictionary composite service, you can specify three services -- BilingualDictionaryPL, MorphologicalAnalysisPL, and TranslationPL.

    Search services specified in TranslationPL. The service type, TranslationPL, shows that the type is Translation. In Language Grid Service Manager language services page, when you specify Service Type as Translation and press the Search button, services with Service Type as Translation are listed. Here, you can assign the listed services to TranslationPL. Click the link of the service you want to use and make a note of the service ID.

    Finding service assigned to BilingualDictionaryPL and MorphologicalAnalysisPL is similar to TranslationPL described above.

    Confirm API

    The service client with Service Type as TranslationWithTemporalDictionary is TranslationWithTemporalDictionaryClient. TranslationWithTemporalDictionaryClient API page shows that it has the method translate. Translate with a dictionary uses this method.

    Source Code

    Create yourfile.php in the specified location in 0. First Steps. Conduct Common Preparations and write the following code. When you run this PHP file, you can conduct text translation using Translation Combined With Bilingual Dictionary.

        <?php
            // Import Language Grid PHP Library
            require_once dirname(__FILE__).'/langrid-php-library/MultilingualStudio.php';
    
            $url = 'http://langrid.org/service_manager/wsdl/TranslationCombinedWithBilingualDictionary';
    
            try{
                // Create a service client
                $client = ClientFactory::createTranslationWithTemporalDictionaryClient($url);
    
                // Set service authentication information (this can be omitted if you use default authentication in the whole service [described below]).
                $client->setUserId('someUserId');
                $client->setPassword('somePassword');	
                // If the client needs proxy to connect, please specify as below.
                // $client->setProxy('proxy.yournetwork.org','8080');
                
                // Setting the service to be bound
                // Write Invocation Name and the service to be bound in pairs.
                
                // Specify the tourism dictionary
                $client->addBindings(new BindingNode("BilingualDictionaryPL", "KyotoTourismDictionaryDb"));
                
                // Service: "morphological analysis" can be omitted (omitted here)
                
                // Specify the translation engine. You can specify Google translation, J-Server, and so on.
                // $client->addBindings(new BindingNode("TranslationPL", "GoogleTranslate"));
                $client->addBindings(new BindingNode("TranslationPL", "KyotoUJServer"));
    
                // Call the service
                $result = $client->translate(
                    Language::get('ja'),
                    Language::get('en'),
                    '京都の比叡山を含む東山は東山36峰とも呼ばれています.',
                    array(
                        new Translation('東山36峰', array('HIGASHIYAMA36HOU')) // You can also pass a local dictionary independently of the public dictionary.
                    ),
                    Language::get('en'));
                var_dump($result);
      			 // -> Hiei in Kyoto Higashiyama is also known as the HIGASHIYAMA36HOU.
            } catch(Exception $e) {
                var_dump($e);
            }
       ?>

    Supplement to the source code
    Specify the name of external service to use in addBindings. In this sample, KyotoTourismDictionaryDb is specified as a a reference dictionary when translating text. As a result, the accuracy of travel-related translation (words that are registered in the KyotoTourismDictionaryDb) is improved. Translation engine is specified as J-Server. Translation results vary for each translation engine.

    Top↑