Ticket #103: bing-translate.patch
| File bing-translate.patch, 9.5 KB (added by Nightgunner5, 22 months ago) |
|---|
-
gp-includes/google.php
4 4 if ( !$locale->google_code ) { 5 5 return new WP_Error( 'google_translate', sprintf( "The locale %s isn't supported by Google Translate.", $locale->slug ) ); 6 6 } 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 ); 8 8 foreach ( $strings as $string ) { 9 9 $url .= '&q=' . urlencode( $string ); 10 10 } 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() ) ) ); 13 13 if ( is_wp_error( $response ) ) { 14 14 return $response; 15 15 } … … 39 39 function google_translate_fix( $string ) { 40 40 $string = preg_replace_callback( '/% (s|d)/i', lambda( '$m', '"%".strtolower($m[1])' ), $string ); 41 41 $string = preg_replace_callback( '/% (\d+) \$ (s|d)/i', lambda( '$m', '"%".$m[1]."\\$".strtolower($m[2])' ), $string ); 42 $string = str_replace( array( ''', '"', '&' ), array( '\'', '"', '&' ), $string ); 42 43 return $string; 43 44 } 45 No newline at end of file -
gp-includes/microsoft.php
1 <?php 2 3 function 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 22 function 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( ''', '"', '&' ), 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
110 110 $ar->country_code = ''; 111 111 $ar->wp_locale = 'ar'; 112 112 $ar->slug = 'ar'; 113 $ar->bing_code = 'ar'; 113 114 $ar->google_code = 'ar'; 114 115 $ar->nplurals = 6; 115 116 $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'; … … 188 189 $bg->country_code = 'bg'; 189 190 $bg->wp_locale = 'bg_BG'; 190 191 $bg->slug = 'bg'; 192 $bg->bing_code = 'bg'; 191 193 $bg->google_code = 'bg'; 192 194 193 195 $bh = new GP_Locale(); … … 311 313 $cs->country_code = 'cz'; 312 314 $cs->wp_locale = 'cs_CZ'; 313 315 $cs->slug = 'cs'; 316 $cs->bing_code = 'cs'; 314 317 $cs->google_code = 'cs'; 315 318 $cs->nplurals = 3; 316 319 $cs->plural_expression = '(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2'; … … 361 364 $da->country_code = 'dk'; 362 365 $da->wp_locale = 'da_DK'; 363 366 $da->slug = 'da'; 367 $da->bing_code = 'da'; 364 368 $da->google_code = 'da'; 365 369 366 370 $de = new GP_Locale(); … … 425 429 $en->country_code = 'us'; 426 430 $en->wp_locale = 'en_US'; 427 431 $en->slug = 'en'; 432 $en->bing_code = 'en'; 428 433 $en->google_code = 'en'; 429 434 430 435 $eo = new GP_Locale(); … … 484 489 $es->country_code = 'es'; 485 490 $es->wp_locale = 'es_ES'; 486 491 $es->slug = 'es'; 492 $es->bing_code = 'es'; 487 493 $es->google_code = 'es'; 488 494 489 495 $et = new GP_Locale(); … … 494 500 $et->country_code = 'ee'; 495 501 $et->wp_locale = 'et'; 496 502 $et->slug = 'et'; 503 $et->bing_code = 'et'; 497 504 $et->google_code = 'et'; 498 505 499 506 $eu = new GP_Locale(); … … 638 645 $he->country_code = 'il'; 639 646 $he->wp_locale = 'he_IL'; 640 647 $he->slug = 'he'; 648 $he->bing_code = 'he'; 641 649 $he->google_code = 'iw'; 642 650 $he->rtl = true; 643 651 … … 671 679 $hu->country_code = 'hu'; 672 680 $hu->wp_locale = 'hu_HU'; 673 681 $hu->slug = 'hu'; 682 $hu->bing_code = 'hu'; 674 683 $hu->google_code = 'hu'; 675 684 676 685 $hy = new GP_Locale(); … … 731 740 $it->country_code = 'it'; 732 741 $it->wp_locale = 'it_IT'; 733 742 $it->slug = 'it'; 743 $it->bing_code = 'it'; 734 744 $it->google_code = 'it'; 735 745 736 746 $ja = new GP_Locale(); … … 740 750 $ja->country_code = 'jp'; 741 751 $ja->wp_locale = 'ja'; 742 752 $ja->slug = 'ja'; 753 $ja->bing_code = 'ja'; 743 754 $ja->google_code = 'ja'; 744 755 $ja->nplurals = 1; 745 756 $ja->plural_expression = '0'; … … 803 814 $ko->country_code = 'kr'; 804 815 $ko->wp_locale = 'ko_KR'; 805 816 $ko->slug = 'ko'; 817 $ko->bing_code = 'ko'; 806 818 $ko->google_code = 'ko'; 807 819 $ko->nplurals = 1; 808 820 $ko->plural_expression = '0'; … … 873 885 $lv->country_code = 'lv'; 874 886 $lv->wp_locale = 'lv'; 875 887 $lv->slug = 'lv'; 888 $lv->bing_code = 'lv'; 876 889 $lv->google_code = 'lv'; 877 890 $lv->nplurals = 3; 878 891 $lv->plural_expression = '(n%10==1 && n%100!=11 ? 0 : n != 0 ? 1 : 2)'; … … 974 987 $nl->country_code = 'nl'; 975 988 $nl->wp_locale = 'nl'; 976 989 $nl->slug = 'nl'; 990 $nl->bing_code = 'nl'; 977 991 $nl->google_code = 'nl'; 978 992 979 993 $nn = new GP_Locale(); … … 992 1006 $no->lang_code_iso_639_2 = 'nor'; 993 1007 $no->country_code = 'no'; 994 1008 $no->slug = 'no'; 1009 $no->bing_code = 'no'; 995 1010 $no->google_code = 'no'; 996 1011 997 1012 $oc = new GP_Locale(); … … 1018 1033 $pl->country_code = 'pl'; 1019 1034 $pl->wp_locale = 'pl_PL'; 1020 1035 $pl->slug = 'pl'; 1036 $pl->bing_code = 'pl'; 1021 1037 $pl->google_code = 'pl'; 1022 1038 $pl->nplurals = 3; 1023 1039 $pl->plural_expression = '(n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'; … … 1042 1058 $pt->country_code = 'pt'; 1043 1059 $pt->wp_locale = 'pt_PT'; 1044 1060 $pt->slug = 'pt'; 1061 $pt->bing_code = 'pt'; 1045 1062 $pt->google_code = 'pt-PT'; 1046 1063 1047 1064 $ro = new GP_Locale(); … … 1052 1069 $ro->country_code = 'ro'; 1053 1070 $ro->wp_locale = 'ro_RO'; 1054 1071 $ro->slug = 'ro'; 1072 $ro->bing_code = 'ro'; 1055 1073 $ro->google_code = 'ro'; 1056 1074 $ro->nplurals = 3; 1057 1075 $ro->plural_expression = '(n==1 ? 0 : (n==0 || (n%100 > 0 && n%100 < 20)) ? 1 : 2);'; … … 1076 1094 $ru->country_code = 'ru'; 1077 1095 $ru->wp_locale = 'ru_RU'; 1078 1096 $ru->slug = 'ru'; 1097 $ru->bing_code = 'ru'; 1079 1098 $ru->google_code = 'ru'; 1080 1099 $ru->nplurals = 3; 1081 1100 $ru->plural_expression = '(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'; … … 1117 1136 $sk->country_code = 'sk'; 1118 1137 $sk->slug = 'sk'; 1119 1138 $sk->wp_locale = 'sk_SK'; 1139 $sk->bing_code = 'sk'; 1120 1140 $sk->google_code = 'sk'; 1121 1141 $sk->nplurals = 3; 1122 1142 $sk->plural_expression = '(n==1) ? 0 : (n>=2 && n<=4) ? 1 : 2'; … … 1129 1149 $sl->country_code = 'si'; 1130 1150 $sl->wp_locale = 'sl_SI'; 1131 1151 $sl->slug = 'sl'; 1152 $sl->bing_code = 'sl'; 1132 1153 $sl->google_code = 'sl'; 1133 1154 $sl->nplurals = 4; 1134 1155 $sl->plural_expression = '(n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3)'; … … 1174 1195 $sv->country_code = 'se'; 1175 1196 $sv->wp_locale = 'sv_SE'; 1176 1197 $sv->slug = 'sv'; 1198 $sv->bing_code = 'sv'; 1177 1199 $sv->google_code = 'sv'; 1178 1200 1179 1201 $sw = new GP_Locale(); … … 1214 1236 $th->country_code = ''; 1215 1237 $th->wp_locale = 'th'; 1216 1238 $th->slug = 'th'; 1239 $th->bing_code = 'th'; 1217 1240 $th->google_code = 'th'; 1218 1241 $th->nplurals = 1; 1219 1242 $th->plural_expression = '0'; … … 1245 1268 $tr->country_code = 'tr'; 1246 1269 $tr->wp_locale = 'tr_TR'; 1247 1270 $tr->slug = 'tr'; 1271 $tr->bing_code = 'tr'; 1248 1272 $tr->google_code = 'tr'; 1249 1273 $tr->nplurals = 1; 1250 1274 $tr->plural_expression = '0'; … … 1349 1373 $zh_cn->country_code = 'cn'; 1350 1374 $zh_cn->wp_locale = 'zh_CN'; 1351 1375 $zh_cn->slug = 'zh-cn'; 1376 $zh_cn->bing_code = 'zh-CHS'; 1352 1377 $zh_cn->google_code = 'zh-CN'; 1353 1378 $zh_cn->nplurals = 1; 1354 1379 $zh_cn->plural_expression = '0'; … … 1382 1407 $zh_tw->country_code = 'tw'; 1383 1408 $zh_tw->slug = 'zh-tw'; 1384 1409 $zh_tw->wp_locale= 'zh_TW'; 1410 $zh_tw->bing_code = 'zh-CHT'; 1385 1411 $zh_tw->google_code = 'zh-TW'; 1386 1412 $zh_tw->nplurals = 1; 1387 1413 $zh_tw->plural_expression = '0';
