translationproxy-service.class.php
3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @package wpml-core
* @subpackage wpml-core
*/
require_once dirname( __FILE__ ) . '/translationproxy-api.class.php';
class TranslationProxy_Service {
public $id;
public $name;
public $description;
public $default_service;
public $has_translator_selection = true; // Todo: read this from service properties
public $delivery_method;
public $project_details_url;
public $custom_text_url;
public $has_language_pairs;
public $languages_map;
public $url;
public $logo_url;
public $create_project_url;
public $add_language_pair_url;
public $new_job_url;
public $custom_fields;
public $custom_fields_data;
public $select_translator_iframe_url;
public $translator_contact_iframe_url;
public $quote_iframe_url;
public $batch_name_max_length;
public static function is_authenticated( $service ) {
// for services that do not require authentication return true by default
if ( ! TranslationProxy::service_requires_authentication( $service ) ) {
return true;
}
return isset( $service->custom_fields_data ) && $service->custom_fields_data ? true : false;
}
public static function list_services() {
return TranslationProxy_Api::proxy_request( '/services.json' );
}
public static function get_service( $service_id ) {
$service = TranslationProxy_Api::proxy_request( "/services/$service_id.json" );
$service->languages_map = self::languages_map( $service );
return $service;
}
public static function get_service_by_suid( $suid ) {
$service = TranslationProxy_Api::proxy_request( "/services/$suid.json" );
$service->languages_map = self::languages_map( $service );
return $service;
}
public static function languages_map( $service ) {
$languages_map = array();
$languages = TranslationProxy_Api::proxy_request( "/services/{$service->id}/language_identifiers.json" );
foreach ( $languages as $language ) {
$languages_map[ $language->iso_code ] = $language->value;
}
return $languages_map;
}
public static function get_language( $service, $language ) {
if ( ! empty( $service->languages_map ) and array_key_exists( $language, $service->languages_map ) ) {
$language = $service->languages_map[ $language ];
}
return $language;
}
/**
* Returns a WPML readable string that allows to tell translation service and translator id
* (typically used for translators dropdowns)
*
* @param int|float|string|bool $translation_service_id
* @param int|float|string|bool $translator_id
*
* @return string
*/
public static function get_wpml_translator_id( $translation_service_id = false, $translator_id = false ) {
if ( $translation_service_id === false ) {
$translation_service_id = TranslationProxy::get_current_service_id();
}
$result = 'ts-' . $translation_service_id;
if ( $translator_id !== false ) {
$result .= '-' . $translator_id;
}
return $result;
}
/**
* @param string $translator_id
*
* @return array Returns a two elements array, respectively containing translation_service and translator_id
*/
public static function get_translator_data_from_wpml( $translator_id ) {
$result = array();
if ( is_numeric( $translator_id ) ) {
$result['translation_service'] = 'local';
$result['translator_id'] = $translator_id;
} else {
$translator_data = explode( '-', $translator_id );
$result = array();
$result['translation_service'] = $translator_data[1];
$result['translator_id'] = isset( $translator_data[2] ) ? $translator_data[2] : 0;
}
return $result;
}
}