This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweglot.module
301 lines (243 loc) · 8.11 KB
/
weglot.module
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**
* @file
* Module weglot
*/
use Weglot\Client\Factory\Languages;
use Weglot\Client\Api\TranslateEntry;
use Weglot\Client\Api\WordEntry;
use Weglot\Client\Client;
use Weglot\Client\Endpoint\Translate;
use Weglot\Client\Api\Enum\BotType;
use Weglot\Client\Api\Enum\WordType;
use Weglot\Util\Url;
use Weglot\Util\Server;
use Weglot\Parser\Parser;
use Weglot\Parser\ConfigProvider\ManualConfigProvider;
use Weglot\Parser\ConfigProvider\ServerConfigProvider;
define('WEGLOT_VERSION', '1.5');
if(!class_exists('Weglot\Util\Url')){
return;
}
/**
* Helper function
*
* @return array
* An array with original and destination language available
*/
function get_code_original_destination_language() {
global $language;
$original_language = language_default();
$code_original_language = $original_language->language;
$code_destination_language = $language->language;
return array(
'original' => $code_original_language,
'destination' => $code_destination_language
);
}
/**
* Get Weglot URL Object
*
* @return Weglot\Util\Url
* It is an object define if an URL is translable
*/
function weglot_get_url_object() {
$code_languages = get_code_original_destination_language();
$destination_languages = locale_language_list();
if (array_key_exists($code_languages['original'], $destination_languages)) {
unset($destination_languages[$code_languages['original']]);
}
$destination_languages = array_keys($destination_languages);
$weglot_url = new Url(
Server::fullUrl($_SERVER),
$code_languages['original'],
$destination_languages
);
$exclude_urls = variable_get("weglot_exclude_url", "");
if (empty($exclude_urls)) {
$exclude_urls = array();
}
else{
$exclude_urls = explode(',', $exclude_urls);
}
$weglot_url->setExcludedUrls($exclude_urls);
return $weglot_url;
}
ob_start('weglot_treat_page');
/**
* Parse and translate DOM
*
* @param string $dom
* Current DOM
* @return string
* DOM Translated
*/
function weglot_treat_page($dom) {
if (empty($dom) || arg(0) === "admin") { // Empty or only front
return prepare_render_treat_page($dom);
}
$api_key = variable_get("weglot_api_key", '');
if (!$api_key) {
return prepare_render_treat_page($dom);
}
$code_languages = get_code_original_destination_language();
$weglot_url = weglot_get_url_object();
$params_treat_page = array();
if ($weglot_url->isTranslable()) {
$params_treat_page["widget"] = widget_html();
}
if ($code_languages["destination"] === $code_languages["original"]) {
return prepare_render_treat_page($dom, $params_treat_page);
}
if (!$weglot_url->isTranslable()) {
drupal_goto($weglot_url->getForLanguage($code_languages["original"]));
return;
}
$client = new Client($api_key);
$config = new ServerConfigProvider();
$exclude_blocks = array("#toolbar", "#overlay", ".page-admin", "#comment-body-add-more-wrapper");
$options_exclude_blocks = variable_get("weglot_exclude_blocks", '');
$options_exclude_blocks = explode(",", $options_exclude_blocks);
$exclude_blocks = array_merge($exclude_blocks, $options_exclude_blocks);
$parser = new Parser($client, $config, $exclude_blocks);
$content = $parser->translate($dom, $code_languages["original"], $code_languages["destination"]);
return prepare_render_treat_page($content, $params_treat_page);
}
/**
* Render treat page
*
* @param string $dom
* DOM you want render
* @param array $params
* Params use for change the DOM. Like a widget selector
* @return string
* The new DOM with the changes
*/
function prepare_render_treat_page($dom, $params = array()) {
if (isset($params["widget"])) {
return str_replace('</body>', $params["widget"] . '</body>', $dom);
}
return $dom;
}
/**
* Implements hook_preprocess_page().
*/
function weglot_preprocess_page(&$vars, $hook) {
if (arg(0) !== "admin") { // Only front
drupal_add_js(drupal_get_path('module', 'weglot') . '/weglot-front.js');
drupal_add_css(drupal_get_path('module', 'weglot') . '/weglot-front.css');
}
}
/**
* Implements hook_permission().
*/
function weglot_permission() {
return array(
'access weglot' => array('title' => t('Access weglot'))
);
}
/**
* Implements hook_block_info().
*/
function weglot_block_info() {
$block['weglot'] = array(
'info' => t('Block info de weglot')
);
return $block;
}
/**
* Implements hook_menu().
*/
function weglot_menu() {
$items = array();
$items['admin/settings/weglot'] = array(
'title' => 'Weglot',
'description' => 'Weglot module settings control',
'page callback' => 'drupal_get_form',
'page arguments' => array('admin_weglot'),
'access arguments' => array('Modify Weglot settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_preprocess_html().
*/
function weglot_preprocess_html(&$variables) {
// Add conditional stylesheets for admin pages on admin theme.
if (arg(0)=="admin") {
// reference your own stylesheet
drupal_add_css(drupal_get_path('module', 'weglot') . '/weglot.css');
drupal_add_js(drupal_get_path('module', 'weglot') . '/weglot.js');
}
}
/**
* Callback from weglot_menu
*/
function admin_weglot() {
$form['general'] = array(
'#type' => 'fieldset',
'#title' => t('General Configuration'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
);
$form['general']['weglot_api_key'] = array(
'#type' => 'textfield',
'#title' => t('Api Key'),
'#default_value' => variable_get('weglot_api_key', ''),
'#description' => t("Log in to <a target='_blank' href='https://weglot.com/register-wordpress
'>Weglot</a> to get your API key."),
'#required' => TRUE
);
$form['exclusion'] = array(
'#type' => 'fieldset',
'#title' => t('Translation exclusion (optional)'),
'#description' => t('By default, every page is translated. You can exclude parts of a page or a full page here.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['exclusion']['weglot_exclude_url'] = array(
'#type' => 'textarea',
'#title' => 'Exclude URL here',
'#description' => t('You can write regex.'),
'#default_value' => variable_get('weglot_exclude_url', '')
);
$form['exclusion']['weglot_exclude_blocks'] = array(
'#type' => 'textarea',
'#title' => 'Exclude blocks',
'#description' => t('Enter CSS selectors, separated by commas.'),
'#default_value' => variable_get('weglot_exclude_blocks', '')
);
return system_settings_form($form);
}
/**
* Get widget selector
*
* @return string
* Return widget selector
*/
function widget_html() {
$code_languages = get_code_original_destination_language();
$is_dropdown = TRUE;
$flag_class = 'wg-flags ';
$tag = $is_dropdown ? 'div' : 'li';
$list_tag = $is_dropdown ? '<ul>' : '';
$widget = sprintf('<!--Weglot %s-->', WEGLOT_VERSION);
$widget .= "<aside data-wg-notranslate='' id='weglot-selector' class='wg-default wg-drop country-selector closed'>";
$weglot_url = weglot_get_url_object();
$list_languages = locale_language_list();
$widget .= sprintf('<%s data-wg-notranslate="" class="wgcurrent wg-li wg-flags %s"><a href="#" onclick="return false;">%s</a></%s>', $tag, $code_languages["destination"], $list_languages[$code_languages["destination"]], $tag);
$widget .= $list_tag;
foreach ($list_languages as $key_code => $lang) {
if ($key_code === $code_languages['destination']) {
continue;
}
$widget .= sprintf('<li class="wg-li %s">', $flag_class . $key_code);
$url_switch = $weglot_url->getForLanguage($key_code);
$widget .= sprintf('<a data-wg-notranslate href="%s">%s</a>', $url_switch, $list_languages[$key_code]);
$widget .= '</li>';
}
$widget .= $list_tag;
$widget .= "</aside>";
return $widget;
}