Ticket #103: bing-translate.patch

File bing-translate.patch, 9.5 KB (added by Nightgunner5, 22 months ago)
  • gp-includes/google.php

     
    44        if ( !$locale->google_code ) { 
    55                return new WP_Error( 'google_translate', sprintf( "The locale %s isn't supported by Google Translate.", $locale->slug ) ); 
    66        } 
    7         $url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&langpair=' . urlencode( 'en|'.$locale->google_code ); 
     7        $url = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&key=' . gp_const_get( 'GP_GOOGLE_KEY' ) . '&langpair=' . urlencode( 'en|'.$locale->google_code ); 
    88        foreach ( $strings as $string ) { 
    99                $url .= '&q=' . urlencode( $string ); 
    1010        } 
    11         if ( count( $strings ) == 1 ) $url .= '&q='; 
    12         $response = wp_remote_get( $url ); 
     11        if ( !$strings ) return array(); 
     12        $response = wp_remote_get( $url, array( 'headers' => array( 'Referer' => gp_url_current() ) ) ); 
    1313        if ( is_wp_error( $response ) ) { 
    1414                return $response; 
    1515        } 
     
    3939function google_translate_fix( $string ) { 
    4040        $string = preg_replace_callback( '/% (s|d)/i', lambda( '$m', '"%".strtolower($m[1])' ), $string ); 
    4141        $string = preg_replace_callback( '/% (\d+) \$ (s|d)/i', lambda( '$m', '"%".$m[1]."\\$".strtolower($m[2])' ), $string ); 
     42        $string = str_replace( array( ''', '"', '&' ), array( '\'', '"', '&' ), $string ); 
    4243        return $string; 
    4344} 
     45 No newline at end of file 
  • gp-includes/microsoft.php

     
     1<?php 
     2 
     3function bing_translate_batch( $locale, $strings ) { 
     4        if ( !$locale->bing_code ) { 
     5                return new WP_Error( 'bing_translate', sprintf( "The locale %s isn't supported by Bing Translate.", $locale->slug ) ); 
     6        } 
     7        if ( !$strings ) return array(); 
     8 
     9        $response = wp_remote_post( 'http://api.microsofttranslator.com/v2/Http.svc/TranslateArray', array( 'body' => '<TranslateArrayRequest><AppId>' . gp_const_get( 'GP_BING_KEY' ) . '</AppId><Options><Category xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">general</Category><ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType><State xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">GlotPress</State><Uri xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">all</Uri><User xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">all</User></Options><Texts><string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">' . implode( '</string><string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">', array_map( 'esc_html', $strings ) ) . '</string></Texts><From>en</From><To>bg</To></TranslateArrayRequest>', 'headers' => array( 'Content-Type' => 'text/xml' ) ) ); 
     10        if ( is_wp_error( $response ) ) 
     11                return $response; 
     12        $response = wp_remote_retrieve_body( $response ); 
     13        $response = @new SimpleXMLElement( $response ); 
     14 
     15        $translations = array(); 
     16        foreach ( $response->TranslateArrayResponse as $translated ) { 
     17                $translations[] = bing_translate_fix( utf8_encode( $translated->TranslatedText ) ); 
     18        } 
     19        return $translations; 
     20} 
     21 
     22function bing_translate_fix( $string ) { 
     23        $string = preg_replace_callback( '/% (s|d)/i', lambda( '$m', '"%".strtolower($m[1])' ), $string ); 
     24        $string = preg_replace_callback( '/% (\d+) \$ (s|d)/i', lambda( '$m', '"%".$m[1]."\\$".strtolower($m[2])' ), $string ); 
     25        $string = str_replace( array( '&#39;', '&quot;', '&amp;' ), array( '\'', '"', '&' ), $string ); 
     26        return $string; 
     27} 
     28 No newline at end of file 
  • locales/locales.php

    Property changes on: gp-includes\microsoft.php
    ___________________________________________________________________
    Added: svn:eol-style
       + native
    
     
    110110                $ar->country_code = ''; 
    111111                $ar->wp_locale = 'ar'; 
    112112                $ar->slug = 'ar'; 
     113                $ar->bing_code = 'ar'; 
    113114                $ar->google_code = 'ar'; 
    114115                $ar->nplurals = 6; 
    115116                $ar->plural_expression = 'n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5'; 
     
    188189                $bg->country_code = 'bg'; 
    189190                $bg->wp_locale = 'bg_BG'; 
    190191                $bg->slug = 'bg'; 
     192                $bg->bing_code = 'bg'; 
    191193                $bg->google_code = 'bg'; 
    192194 
    193195                $bh = new GP_Locale(); 
     
    311313                $cs->country_code = 'cz'; 
    312314                $cs->wp_locale = 'cs_CZ'; 
    313315                $cs->slug = 'cs'; 
     316                $cs->bing_code = 'cs'; 
    314317                $cs->google_code = 'cs'; 
    315318                $cs->nplurals = 3; 
    316319                $cs->plural_expression = '(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2'; 
     
    361364                $da->country_code = 'dk'; 
    362365                $da->wp_locale = 'da_DK'; 
    363366                $da->slug = 'da'; 
     367                $da->bing_code = 'da'; 
    364368                $da->google_code = 'da'; 
    365369                 
    366370                $de = new GP_Locale(); 
     
    425429                $en->country_code = 'us'; 
    426430                $en->wp_locale = 'en_US'; 
    427431                $en->slug = 'en'; 
     432                $en->bing_code = 'en'; 
    428433                $en->google_code = 'en'; 
    429434 
    430435                $eo = new GP_Locale(); 
     
    484489                $es->country_code = 'es'; 
    485490                $es->wp_locale = 'es_ES'; 
    486491                $es->slug = 'es'; 
     492                $es->bing_code = 'es'; 
    487493                $es->google_code = 'es'; 
    488494                 
    489495                $et = new GP_Locale(); 
     
    494500                $et->country_code = 'ee'; 
    495501                $et->wp_locale = 'et'; 
    496502                $et->slug = 'et'; 
     503                $et->bing_code = 'et'; 
    497504                $et->google_code = 'et'; 
    498505 
    499506                $eu = new GP_Locale(); 
     
    638645                $he->country_code = 'il'; 
    639646                $he->wp_locale = 'he_IL'; 
    640647                $he->slug = 'he'; 
     648                $he->bing_code = 'he'; 
    641649                $he->google_code = 'iw'; 
    642650                $he->rtl = true; 
    643651 
     
    671679                $hu->country_code = 'hu'; 
    672680                $hu->wp_locale = 'hu_HU'; 
    673681                $hu->slug = 'hu'; 
     682                $hu->bing_code = 'hu'; 
    674683                $hu->google_code = 'hu'; 
    675684 
    676685                $hy = new GP_Locale(); 
     
    731740                $it->country_code = 'it'; 
    732741                $it->wp_locale = 'it_IT'; 
    733742                $it->slug = 'it'; 
     743                $it->bing_code = 'it'; 
    734744                $it->google_code = 'it'; 
    735745 
    736746                $ja = new GP_Locale(); 
     
    740750                $ja->country_code = 'jp'; 
    741751                $ja->wp_locale = 'ja'; 
    742752                $ja->slug = 'ja'; 
     753                $ja->bing_code = 'ja'; 
    743754                $ja->google_code = 'ja'; 
    744755                $ja->nplurals = 1; 
    745756                $ja->plural_expression = '0'; 
     
    803814                $ko->country_code = 'kr'; 
    804815                $ko->wp_locale = 'ko_KR'; 
    805816                $ko->slug = 'ko'; 
     817                $ko->bing_code = 'ko'; 
    806818                $ko->google_code = 'ko'; 
    807819                $ko->nplurals = 1; 
    808820                $ko->plural_expression = '0'; 
     
    873885                $lv->country_code = 'lv'; 
    874886                $lv->wp_locale = 'lv'; 
    875887                $lv->slug = 'lv'; 
     888                $lv->bing_code = 'lv'; 
    876889                $lv->google_code = 'lv'; 
    877890                $lv->nplurals = 3; 
    878891                $lv->plural_expression = '(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2)'; 
     
    974987                $nl->country_code = 'nl'; 
    975988                $nl->wp_locale = 'nl'; 
    976989                $nl->slug = 'nl'; 
     990                $nl->bing_code = 'nl'; 
    977991                $nl->google_code = 'nl'; 
    978992 
    979993                $nn = new GP_Locale(); 
     
    9921006                $no->lang_code_iso_639_2 = 'nor'; 
    9931007                $no->country_code = 'no'; 
    9941008                $no->slug = 'no'; 
     1009                $no->bing_code = 'no'; 
    9951010                $no->google_code = 'no'; 
    9961011 
    9971012                $oc = new GP_Locale(); 
     
    10181033                $pl->country_code = 'pl'; 
    10191034                $pl->wp_locale = 'pl_PL'; 
    10201035                $pl->slug = 'pl'; 
     1036                $pl->bing_code = 'pl'; 
    10211037                $pl->google_code = 'pl'; 
    10221038                $pl->nplurals = 3; 
    10231039                $pl->plural_expression = '(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'; 
     
    10421058                $pt->country_code = 'pt'; 
    10431059                $pt->wp_locale = 'pt_PT'; 
    10441060                $pt->slug = 'pt'; 
     1061                $pt->bing_code = 'pt'; 
    10451062                $pt->google_code = 'pt-PT'; 
    10461063 
    10471064                $ro = new GP_Locale(); 
     
    10521069                $ro->country_code = 'ro'; 
    10531070                $ro->wp_locale = 'ro_RO'; 
    10541071                $ro->slug = 'ro'; 
     1072                $ro->bing_code = 'ro'; 
    10551073                $ro->google_code = 'ro'; 
    10561074                $ro->nplurals = 3; 
    10571075                $ro->plural_expression = '(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);'; 
     
    10761094                $ru->country_code = 'ru'; 
    10771095                $ru->wp_locale = 'ru_RU'; 
    10781096                $ru->slug = 'ru'; 
     1097                $ru->bing_code = 'ru'; 
    10791098                $ru->google_code = 'ru'; 
    10801099                $ru->nplurals = 3; 
    10811100                $ru->plural_expression = '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'; 
     
    11171136                $sk->country_code = 'sk'; 
    11181137                $sk->slug = 'sk'; 
    11191138                $sk->wp_locale = 'sk_SK'; 
     1139                $sk->bing_code = 'sk'; 
    11201140                $sk->google_code = 'sk'; 
    11211141                $sk->nplurals = 3; 
    11221142                $sk->plural_expression = '(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2'; 
     
    11291149                $sl->country_code = 'si'; 
    11301150                $sl->wp_locale = 'sl_SI'; 
    11311151                $sl->slug = 'sl'; 
     1152                $sl->bing_code = 'sl'; 
    11321153                $sl->google_code = 'sl'; 
    11331154                $sl->nplurals = 4; 
    11341155                $sl->plural_expression = '(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3)'; 
     
    11741195                $sv->country_code = 'se'; 
    11751196                $sv->wp_locale = 'sv_SE'; 
    11761197                $sv->slug = 'sv'; 
     1198                $sv->bing_code = 'sv'; 
    11771199                $sv->google_code = 'sv'; 
    11781200 
    11791201                $sw = new GP_Locale(); 
     
    12141236                $th->country_code = ''; 
    12151237                $th->wp_locale = 'th'; 
    12161238                $th->slug = 'th'; 
     1239                $th->bing_code = 'th'; 
    12171240                $th->google_code = 'th'; 
    12181241                $th->nplurals = 1; 
    12191242                $th->plural_expression = '0'; 
     
    12451268                $tr->country_code = 'tr'; 
    12461269                $tr->wp_locale = 'tr_TR'; 
    12471270                $tr->slug = 'tr'; 
     1271                $tr->bing_code = 'tr'; 
    12481272                $tr->google_code = 'tr'; 
    12491273                $tr->nplurals = 1; 
    12501274                $tr->plural_expression = '0'; 
     
    13491373                $zh_cn->country_code = 'cn'; 
    13501374                $zh_cn->wp_locale = 'zh_CN'; 
    13511375                $zh_cn->slug = 'zh-cn'; 
     1376                $zh_cn->bing_code = 'zh-CHS'; 
    13521377                $zh_cn->google_code = 'zh-CN'; 
    13531378                $zh_cn->nplurals = 1; 
    13541379                $zh_cn->plural_expression = '0'; 
     
    13821407                $zh_tw->country_code = 'tw'; 
    13831408                $zh_tw->slug = 'zh-tw'; 
    13841409                $zh_tw->wp_locale= 'zh_TW'; 
     1410                $zh_tw->bing_code = 'zh-CHT'; 
    13851411                $zh_tw->google_code = 'zh-TW'; 
    13861412                $zh_tw->nplurals = 1; 
    13871413                $zh_tw->plural_expression = '0';