Transposh WordPress Translation - Version 0.1.0

Version Description

Download this release

Release Info

Developer oferwald
Plugin Icon 128x128 Transposh WordPress Translation
Version 0.1.0
Comparing to
See all releases

Code changes from version 0.0.3 to 0.1.0

constants.php CHANGED
@@ -29,30 +29,49 @@ define("ENABLE_APC", TRUE);
29
  define("NO_TRANSLATE_CLASS", "no_translate");
30
 
31
  //Supported languages, new languages can be added here
 
32
  $languages = array("en" => "English,us",
 
33
  "ar" => "العربية,sa",
34
  "bg" => "Български,bg",
35
- "zh" => "汉字,cn",
36
- "hr" => "Hrvatski,hr",
 
 
37
  "cs" => "čeština,cz",
38
- "nl" => "Nederlands,nl",
39
- "fi" => "Suomi,fi",
 
 
40
  "fr" => "Français,fr",
41
- "de" => "Deutsch,de",
 
42
  "el" => "Ελληνικά,gr",
43
  "he" => "עברית,il",
44
- "hu" => "magyar,hu",
45
- "it" => "Italiano,it",
46
- "ko" => "우리말,kr",
47
- "pl" => "Polski,pl",
 
 
 
 
 
 
 
48
  "pt" => "Português,pt",
49
  "ro" => "Română,ro",
50
  "ru" => "Русский,ru",
51
- "es" => "Español,es",
 
 
 
52
  "sv" => "svenska,se",
53
- "th" => "ภาษาไทย,th",
54
- "tr" => "Türkçe,tr");
55
-
 
 
56
 
57
  //Language which are read from right to left (rtl)
58
  $rtl_languages = array("ar", "he");
@@ -60,7 +79,7 @@ $rtl_languages = array("ar", "he");
60
  //Define the new capability that will be assigned to roles - translator
61
  define("TRANLSLATOR", 'translator');
62
 
63
- //Option defining whether anonymous translation is allowed.
64
  define("ANONYMOUS_TRANSLATION", "transposh_allow_anonymous_translation");
65
 
66
  //Option defining the list of currentlly viewable languages
@@ -69,19 +88,22 @@ define("VIEWABLE_LANGS", "transposh_viewable_languages");
69
  //Option defining the list of currentlly editable languages
70
  define("EDITABLE_LANGS", "transposh_editable_languages");
71
 
 
 
 
 
 
 
72
  //Option defining the default language
73
  define("DEFAULT_LANG", "transposh_default_language");
74
 
75
- //Define segment id prefix, will be included in span tag.
 
 
 
76
  define("SPAN_PREFIX", "tr_");
77
 
78
  //Define segment id prefix, will be included in img tag.
79
  define("IMG_PREFIX", "tr_img_");
80
 
81
- //Option defining transposh widget appearance
82
- define("WIDGET_TRANSPOSH", "widget_transposh");
83
-
84
- //Option to enable/disable rewrite of permalinks
85
- define("ENABLE_PERMALINKS_REWRITE", "transposh_enable_permalinks");
86
-
87
- ?>
29
  define("NO_TRANSLATE_CLASS", "no_translate");
30
 
31
  //Supported languages, new languages can be added here
32
+ //the array directs from language code to - Native name, flag
33
  $languages = array("en" => "English,us",
34
+ "sq" => "Shqip,al",
35
  "ar" => "العربية,sa",
36
  "bg" => "Български,bg",
37
+ "ca" => "Català", // flag ?
38
+ "zh" => "中文(简体),cn",
39
+ "zh-tw" => "中文(漢字),tw",
40
+ "hr" => "Hrvatski,hr",
41
  "cs" => "čeština,cz",
42
+ "da" => "dansk,dk",
43
+ "nl" => "Nederlands,nl",
44
+ "et" => "Eesti keel,ee",
45
+ "fi" => "Suomi,fi",
46
  "fr" => "Français,fr",
47
+ "ga" => "Galego", //flag ?
48
+ "de" => "Deutsch,de",
49
  "el" => "Ελληνικά,gr",
50
  "he" => "עברית,il",
51
+ "hi" => "हिन्दी; हिंदी,in",
52
+ "hu" => "magyar,hu",
53
+ "id" => "Bahasa Indonesia,id",
54
+ "it" => "Italiano,it",
55
+ "ja" => "日本語 (にほんご/にっぽんご),jp",
56
+ "ko" => "우리말,kr",
57
+ "lv" => "latviešu valoda,lv",
58
+ "lt" => "lietuvių kalba,lt",
59
+ "mt" => "Malti,mt",
60
+ "no" => "Norsk,no",
61
+ "pl" => "Polski,pl",
62
  "pt" => "Português,pt",
63
  "ro" => "Română,ro",
64
  "ru" => "Русский,ru",
65
+ "sr" => "српски језик,rs",
66
+ "sk" => "slovenčina,sk",
67
+ "sl" => "slovenščina,sl",
68
+ "es" => "Español,es",
69
  "sv" => "svenska,se",
70
+ "tl" => "Tagalog,ph",
71
+ "th" => "ภาษาไทย,th",
72
+ "tr" => "Türkçe,tr",
73
+ "uk" => "Українська,ua",
74
+ "vi" => "Tiếng Việt,vn");
75
 
76
  //Language which are read from right to left (rtl)
77
  $rtl_languages = array("ar", "he");
79
  //Define the new capability that will be assigned to roles - translator
80
  define("TRANLSLATOR", 'translator');
81
 
82
+ //Option defining whether anonymous translation is allowed.
83
  define("ANONYMOUS_TRANSLATION", "transposh_allow_anonymous_translation");
84
 
85
  //Option defining the list of currentlly viewable languages
88
  //Option defining the list of currentlly editable languages
89
  define("EDITABLE_LANGS", "transposh_editable_languages");
90
 
91
+ //Option to enable/disable rewrite of permalinks
92
+ define("ENABLE_AUTO_TRANSLATE", "transposh_enable_autotranslate");
93
+
94
+ //Option to enable/disable rewrite of permalinks
95
+ define("ENABLE_PERMALINKS_REWRITE", "transposh_enable_permalinks");
96
+
97
  //Option defining the default language
98
  define("DEFAULT_LANG", "transposh_default_language");
99
 
100
+ //Option defining transposh widget appearance
101
+ define("WIDGET_TRANSPOSH", "transposh_widget");
102
+
103
+ //Define segment id prefix, will be included in span tag. also used as class identifier
104
  define("SPAN_PREFIX", "tr_");
105
 
106
  //Define segment id prefix, will be included in img tag.
107
  define("IMG_PREFIX", "tr_img_");
108
 
109
+ ?>
 
 
 
 
 
 
flags/ba.png CHANGED
Binary file
flags/be.png CHANGED
Binary file
flags/bn.png CHANGED
Binary file
flags/cm.png CHANGED
Binary file
flags/cu.png CHANGED
Binary file
flags/dz.png CHANGED
Binary file
flags/eh.png CHANGED
Binary file
flags/er.png CHANGED
Binary file
flags/ga.png CHANGED
Binary file
flags/gn.png CHANGED
Binary file
flags/gp.png CHANGED
Binary file
flags/gs.png CHANGED
Binary file
flags/gw.png CHANGED
Binary file
flags/hr.png CHANGED
Binary file
flags/jm.png CHANGED
Binary file
flags/kg.png CHANGED
Binary file
flags/km.png CHANGED
Binary file
flags/kr.png CHANGED
Binary file
flags/ky.png CHANGED
Binary file
flags/ls.png CHANGED
Binary file
flags/lu.png CHANGED
Binary file
flags/mz.png CHANGED
Binary file
flags/np.png CHANGED
Binary file
flags/nr.png CHANGED
Binary file
flags/pr.png CHANGED
Binary file
flags/sc.png CHANGED
Binary file
flags/sd.png CHANGED
Binary file
flags/sg.png CHANGED
Binary file
flags/sr.png CHANGED
Binary file
flags/st.png CHANGED
Binary file
flags/tg.png CHANGED
Binary file
flags/tl.png CHANGED
Binary file
flags/tn.png CHANGED
Binary file
flags/tr.png CHANGED
Binary file
flags/tt.png CHANGED
Binary file
flags/us.png CHANGED
Binary file
flags/uz.png CHANGED
Binary file
flags/va.png CHANGED
Binary file
flags/vu.png CHANGED
Binary file
flags/wf.png CHANGED
Binary file
flags/ws.png CHANGED
Binary file
flags/yt.png CHANGED
Binary file
flags/za.png CHANGED
Binary file
flags/zw.png CHANGED
Binary file
js/transposh.js CHANGED
@@ -14,6 +14,7 @@
14
  * along with this program; if not, write to the Free Software
15
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
  */
 
17
  function display_dialog(caption, content)
18
  {
19
  overlib(content,
@@ -46,89 +47,121 @@ function hint(original)
46
  }
47
 
48
  // fetch translation from google translate...
49
- function getgt(lang)
50
  {
51
- google.language.translate($("#tr_original_unescaped").text(), "", lang, function(result) {
52
  if (!result.error) {
53
- $("#tr_translation").attr('value',result.translation);
54
  }
55
  });
56
  }
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  //Open translation dialog
59
- function translate_dialog(original, trans, lang, post_url, segment_id)
60
  {
61
  caption='Edit Translation';
62
-
63
  var dialog = ''+
64
- ('<form id="tr_form" name="transposh_edit_form" method="post" action="' + post_url + '"><div>') +
65
  '<p dir="ltr">Original text<br \/><textarea id="tr_original_unescaped" cols="60" rows="3" readonly="readyonly">' +
66
  original + '</textarea> <\/p>' +
67
  '<p>Translate to<br \/><input type="text" id="tr_translation" name="translation" size="80" value="'+ trans +
68
  '"' + 'onfocus="OLmEdit=1;" onblur="OLmEdit=0;"<\/p>' +
69
- '<input type="hidden" id="tr_original" name="original" value="' + escape(original) +'">' +
70
- '<input type="hidden" id="tr_lang" name="lang" value="'+lang+'">' +
71
  '<input type="hidden" name="translation_posted" value= "1">' +
72
- '<p><input onclick="getgt(\''+lang+'\')" type="button" value="Get Suggestion!"/>&nbsp;<input type="submit" value="Translate"/><\/p>' +
73
  ('<\/div><\/form>');
74
 
75
- display_dialog(caption, dialog);
76
 
77
- // attach handler to form's submit event
78
- $('#tr_form').submit(function() {
79
  var translation = $('#tr_translation').val();
80
- var query = 'original=' + escape(original) +
81
- '&translation=' + translation +
82
- '&lang=' + $('#tr_lang').val() +
83
- '&translation_posted=1';
84
-
85
-
86
- $.ajax({
87
- type: "POST",
88
- url: post_url,
89
- data: query,
90
- success: function(req) {
91
- //rewrite onclick function - in case of re-edit
92
- $("#tr_img_" + segment_id).click(function () {
93
- translate_dialog(original, translation, lang, post_url, segment_id);
94
- });
95
-
96
- //current img
97
- var img = $("#tr_img_" + segment_id).attr('src');
98
- var text_rewrite = translation;
99
-
100
- if(jQuery.trim(translation).length == 0) {
101
- //reset to the original content - the not escaped version
102
- text_rewrite = original;
103
-
104
- //switch to the edit img
105
- img = img.replace(/translate_fix.png/, "translate.png");
106
- }
107
- else {
108
- //switch to the fix img
109
- img = img.replace(/translate.png/, "translate_fix.png");
110
- }
111
-
112
-
113
- //rewrite text
114
- $("#tr_" + segment_id).html(text_rewrite);
115
-
116
- //rewrite image
117
- $("#tr_img_" + segment_id).attr('src', img);
118
-
119
- //close dialog
120
- cClick();
121
- },
122
-
123
- error: function(req) {
124
- alert("Error !!! failed to translate.\n\nServer's message: " + req.statusText);
125
- }
126
-
127
-
128
- });
129
 
130
  // return false to prevent normal browser submit and page navigation
131
  return false;
132
 
133
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  }
14
  * along with this program; if not, write to the Free Software
15
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16
  */
17
+
18
  function display_dialog(caption, content)
19
  {
20
  overlib(content,
47
  }
48
 
49
  // fetch translation from google translate...
50
+ function getgt()
51
  {
52
+ google.language.translate($("#tr_original_unescaped").text(), "", transposh_target_lang, function(result) {
53
  if (!result.error) {
54
+ $("#tr_translation").val(result.translation);
55
  }
56
  });
57
  }
58
 
59
+ //Ajax translation
60
+ function ajax_translate(original,translation,source,segment_id) {
61
+ var token = $("#tr_" + segment_id).attr('token');
62
+ var query = 'token=' + token +
63
+ '&translation=' + translation +
64
+ '&lang=' + transposh_target_lang +
65
+ '&source=' + source +
66
+ '&translation_posted=1';
67
+
68
+ //$("span:contains("+translation+")").css("text-decoration", "underline");
69
+ $.ajax({
70
+ type: "POST",
71
+ url: transposh_post_url,
72
+ data: query,
73
+ success: function(req) {
74
+ var pre_translated = $("#tr_" + segment_id).html();
75
+ var new_text = translation;
76
+ //reset to the original content - the unescaped version if translation is empty
77
+ if(jQuery.trim(translation).length == 0) {
78
+ new_text = original;
79
+ }
80
+ // rewrite text for all matching items at once
81
+ $(".tr_t,.tr_u").filter(function() {return $(this).html() == pre_translated;}).html(new_text)
82
+ .each(function (i) { // handle the image changes
83
+ var img_segment_id = $(this).attr('id').substr($(this).attr('id').lastIndexOf('_')+1);
84
+ //current img
85
+ var img = $("#tr_img_" + img_segment_id).attr('src');
86
+
87
+ //rewrite onclick function - in case of re-edit
88
+ $("#tr_img_" + img_segment_id).click(function () {
89
+ translate_dialog(original, translation, img_segment_id);
90
+ });
91
+
92
+ // handle image
93
+ if(jQuery.trim(translation).length == 0) {
94
+ //switch to the edit img
95
+ img = img.replace(/translate_fix.png/, "translate.png");
96
+ img = img.replace(/translate_auto.png/, "translate.png");
97
+ } else {
98
+ if (source == 1) {
99
+ //switch to the auto img
100
+ img = img.replace(/translate.png/, "translate_auto.png");
101
+ } else {
102
+ //switch to the fix img
103
+ img = img.replace(/translate.png/, "translate_fix.png");
104
+ img = img.replace(/translate_auto.png/, "translate_fix.png");
105
+ }
106
+ }
107
+ //rewrite image
108
+ $("#tr_img_" + img_segment_id).attr('src', img);
109
+
110
+ });
111
+
112
+ //close dialog
113
+ cClick();
114
+ },
115
+
116
+ error: function(req) {
117
+ if (source == 0) {
118
+ alert("Error !!! failed to translate.\n\nServer's message: " + req.statusText);
119
+ }
120
+ }
121
+ });
122
+
123
+ }
124
+
125
  //Open translation dialog
126
+ function translate_dialog(original, trans, segment_id)
127
  {
128
  caption='Edit Translation';
129
+ //alert (this.id);
130
  var dialog = ''+
131
+ ('<form id="tr_form" name="transposh_edit_form" method="post" action="' + transposh_post_url + '"><div>') +
132
  '<p dir="ltr">Original text<br \/><textarea id="tr_original_unescaped" cols="60" rows="3" readonly="readyonly">' +
133
  original + '</textarea> <\/p>' +
134
  '<p>Translate to<br \/><input type="text" id="tr_translation" name="translation" size="80" value="'+ trans +
135
  '"' + 'onfocus="OLmEdit=1;" onblur="OLmEdit=0;"<\/p>' +
 
 
136
  '<input type="hidden" name="translation_posted" value= "1">' +
137
+ '<p><input onclick="getgt()" type="button" value="Get Suggestion!"/>&nbsp;<input type="submit" value="Translate"/><\/p>' +
138
  ('<\/div><\/form>');
139
 
140
+ display_dialog(caption, dialog);
141
 
142
+ // attach handler to form's submit event
143
+ $('#tr_form').submit(function() {
144
  var translation = $('#tr_translation').val();
145
+
146
+ ajax_translate(original,translation,0,segment_id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
  // return false to prevent normal browser submit and page navigation
149
  return false;
150
 
151
  });
152
+
153
+ }
154
+ //function for auto translation
155
+
156
+ function do_auto_translate() {
157
+ $(".tr_u").each(function (i) {
158
+ var translated_id = $(this).attr('id');
159
+ google.language.translate($(this).text(), "", transposh_target_lang, function(result) {
160
+ if (!result.error) {
161
+ var segment_id = translated_id.substr(translated_id.lastIndexOf('_')+1);
162
+ ajax_translate($("#"+translated_id).text(),result.translation,1,segment_id);
163
+ $("#"+translated_id).addClass("tr_t").removeClass("tr_u");
164
+ }
165
+ });
166
+ });
167
  }
parser.php CHANGED
@@ -56,6 +56,9 @@ $is_in_body = FALSE;
56
  //Is current position within the channel tag, i.e. RSS feed
57
  $is_in_channel = FALSE;
58
 
 
 
 
59
  /*
60
  * Parse the html page into tags, identify translateable string which
61
  * will be translated.
@@ -96,7 +99,7 @@ function process_html()
96
  //Mark tag start position
97
  $tag_start = $pos;
98
  $pos = strpos($page, '>', $pos);
99
-
100
  //Mark tag end position
101
  $tag_end = $pos;
102
 
@@ -156,7 +159,6 @@ function process_html()
156
 
157
  }
158
 
159
-
160
  /*
161
  * Determine if the specified element should be skipped. If so the position
162
  * is moved past end of tag.
@@ -208,12 +210,11 @@ function process_tag_init(&$element, $start, $end)
208
  case 'channel':
209
  global $is_in_channel;
210
  $is_in_channel = TRUE;
211
- break;
212
  }
213
 
214
  }
215
 
216
-
217
  /*
218
  * Handle span tags. Looks for 'no_tranlate' identifier that will disable
219
  * translation for the enclosed text.
@@ -239,7 +240,6 @@ function process_span_or_div_tag(&$element, $start, $end)
239
  $element .= "." . NO_TRANSLATE_CLASS;
240
  }
241
 
242
-
243
  /*
244
  * Process html tag. Set the direction for rtl languages.
245
  *
@@ -300,7 +300,7 @@ function get_element()
300
  //check first for a character data section - treat it like an element
301
  if(is_word('![CDATA['))
302
  {
303
- $pos += 8; //skip to end of ![CDATA[
304
  }
305
  else
306
  {
@@ -310,7 +310,7 @@ function get_element()
310
  $pos++;
311
  }
312
  }
313
-
314
 
315
  return substr($page,$start, $pos - $start);
316
  }
@@ -384,7 +384,7 @@ function get_attribute(&$start, &$end, $id)
384
  function process_current_tag()
385
  {
386
  global $page, $pos;
387
-
388
 
389
 
390
  //translate only known elements within the body or channel
@@ -393,10 +393,10 @@ function process_current_tag()
393
  skip_white_space();
394
  $start = $pos;
395
  $page_length = strlen($page);
396
-
397
- // Indicates if the html entity should break into a new translation segment.
398
- $is_breaker = FALSE;
399
-
400
  while($pos < $page_length && $page[$pos] != '<')
401
  {
402
  //will break translation unit when one of the following characters is reached: .,
@@ -408,7 +408,7 @@ function process_current_tag()
408
  translate_text($start);
409
  $start = $end_of_entity;
410
  }
411
-
412
  //skip past entity
413
  $pos = $end_of_entity;
414
  }
@@ -418,6 +418,12 @@ function process_current_tag()
418
  $pos++;
419
  $start = $pos;
420
  }
 
 
 
 
 
 
421
  else
422
  {
423
  $pos++;
@@ -432,15 +438,14 @@ function process_current_tag()
432
 
433
  }
434
 
435
-
436
  /*
437
- * Translate the content of a cdata section. For now we only expect to handle it
438
- * within RSS feeds.
439
  */
440
  function process_cdata_section()
441
  {
442
  global $page, $pos;
443
-
444
 
445
 
446
  //translate only known elements within rss feeds
@@ -449,7 +454,7 @@ function process_cdata_section()
449
  skip_white_space();
450
  $start = $pos;
451
  $page_length = strlen($page);
452
-
453
  while($pos < $page_length && !is_word(']]>'))
454
  {
455
  //will break translation unit when one of the following characters is reached: .,
@@ -460,6 +465,12 @@ function process_cdata_section()
460
  $pos++;
461
  $start = $pos;
462
  }
 
 
 
 
 
 
463
  else
464
  {
465
  $pos++;
@@ -476,14 +487,14 @@ function process_cdata_section()
476
 
477
  /**
478
  * Determines position in page marks a transaltable tag in html page or rss feed section.
479
- * Return TRUE if should be translated otherwise FALSE.
480
  */
481
  function is_translatable_section()
482
  {
483
  global $tags_list, $is_in_channel, $is_in_body;
484
  $rc = FALSE;
485
  $current_tag = end($tags_list);
486
-
487
  if($is_in_body || $current_tag == 'title')
488
  {
489
  $rc = TRUE;
@@ -495,7 +506,7 @@ function is_translatable_section()
495
  }
496
 
497
 
498
- return $rc;
499
  }
500
 
501
  /*
@@ -522,9 +533,7 @@ function is_sentence_breaker($position)
522
  $page[$position] == '[' || $page[$position] == ']' ||
523
  $page[$position] == '"' || $page[$position] == '!' ||
524
  $page[$position] == ':' || $page[$position] == '|' ||
525
- $page[$position] == ';' ||
526
- //break on numbers but not like: 3rd, 4th
527
- (is_digit($position) && !is_a_to_z_character($position+1)))
528
  {
529
  //break the sentence into segments regardless of the next character.
530
  $rc = TRUE;
@@ -536,7 +545,7 @@ function is_sentence_breaker($position)
536
  /*
537
  * Determines if the current position marks the begining of an html
538
  * entity. E.g &amp;
539
- * Return 0 if not an html entity otherwise return the position past this entity. In addition
540
  * the $is_breaker will be set to TRUE if entity should break translation into a new segment.
541
  *
542
  */
@@ -573,6 +582,29 @@ function is_html_entity($position, &$is_breaker)
573
  return 0;
574
  }
575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
576
 
577
  /*
578
  * Determine if the current position in page points to a character in the
@@ -627,32 +659,6 @@ function is_white_space($position)
627
  }
628
  }
629
 
630
- /*
631
- * Skip within buffer past unreadable characters , i.e. white space
632
- * and characters considred to be a sentence breaker. Staring from the specified
633
- * position going either forward or backward.
634
- * param forward - indicate direction going either backward of forward.
635
- */
636
- function skip_unreadable_chars(&$index, $forward=TRUE)
637
- {
638
- global $page, $pos;
639
-
640
- if(!isset($index))
641
- {
642
- //use $pos as the default position if not specified otherwise
643
- $index = &$pos;
644
- }
645
- $start = $index;
646
-
647
- while($index < strlen($page) && $index > 0 &&
648
- (is_white_space($index) || is_sentence_breaker($index)))
649
- {
650
- ($forward ? $index++ : $index--);
651
- }
652
-
653
- return $index;
654
- }
655
-
656
  /*
657
  * Skip within buffer past white space characters , Staring from the specified
658
  * position going either forward or backward.
@@ -676,29 +682,28 @@ function skip_white_space(&$index, $forward=TRUE)
676
  return $index;
677
  }
678
 
679
-
680
  /*
681
  * Check within page buffer position for the given word.
682
  * param word The word to look for.
683
- * param index1 Optional position within the page buffer, if not available then the current
684
- * position ($pos) is used.
685
  * Return TRUE if the word matches otherwise FALSE
686
  */
687
  function is_word($word, $index1)
688
  {
689
  global $page, $pos;
690
  $rc = FALSE;
691
-
692
  if(!isset($index1))
693
  {
694
  //use $pos as the default position if not specified otherwise
695
  $index1 = $pos;
696
  }
697
-
698
  $index2 = 0; //position within word
699
  $word_len = strlen($word);
700
  $page_length = strlen($page);
701
-
702
  while($index1 < $page_length && $index2 < $word_len)
703
  {
704
  if($page[$index1] == $word[$index2])
@@ -711,25 +716,24 @@ function is_word($word, $index1)
711
  break;
712
  }
713
  }
714
-
715
  //check if we have full match
716
  if($index2 == $word_len)
717
  {
718
  $rc = TRUE;
719
  }
720
-
721
  return $rc;
722
  }
723
 
724
-
725
- /**
726
  * Translate the text between the given start position and the current
727
  * position (pos) within the buffer.
728
  */
729
  function translate_text($start)
730
  {
731
 
732
- global $page, $pos, $is_edit_mode;
733
 
734
  //trim white space from the start position going forward
735
  skip_white_space($start);
@@ -756,9 +760,9 @@ function translate_text($start)
756
  return;
757
  }
758
 
759
- $translated_text = fetch_translation($original_text);
760
 
761
- insert_translation($original_text, $translated_text, $start, $end);
762
  }
763
 
764
  /*
@@ -768,29 +772,30 @@ function translate_text($start)
768
  * param start Marks the start position of the text to be replaced within the original page
769
  * param end Marks the end position of the text to be replaced within the original page
770
  */
771
- function insert_translation(&$original_text, &$translated_text, $start, $end)
772
  {
773
- global $segment_id, $is_edit_mode, $tags_list;
774
 
775
  $is_translated = FALSE;
776
 
777
- if(!$is_edit_mode || !in_array('body', $tags_list))
778
- {
779
- if($translated_text != NULL)
780
- {
781
- update_translated_page($start, $end, $translated_text);
782
- }
783
- }
784
- else
785
  {
786
- $span = "<span id=\"" . SPAN_PREFIX . "$segment_id\">";
 
 
 
 
 
 
787
 
788
- if($translated_text == NULL)
789
  {
 
790
  $span .= $original_text . '</span>';
791
  }
792
  else
793
  {
 
794
  $span .= $translated_text . "</span>";
795
  $is_translated = TRUE;
796
  }
@@ -799,20 +804,27 @@ function insert_translation(&$original_text, &$translated_text, $start, $end)
799
  update_translated_page($start, $end, $span);
800
 
801
 
802
- //Insert image to allow editing this segment
803
- $img = get_img_tag($original_text, $translated_text, $segment_id, $is_translated);
804
- update_translated_page($end + 1, - 1, $img);
805
-
 
806
  //Increment only after both text and image are generated so they
807
  //will be the same for each translated segement
808
  $segment_id++;
809
 
810
  }
 
 
 
 
 
 
 
811
 
812
 
813
  }
814
 
815
-
816
  /*
817
  * Scrubs text prior to translation to remove/encode special
818
  * characters.
@@ -833,7 +845,6 @@ function scrub_text(&$text)
833
  return $text;
834
  }
835
 
836
-
837
  /**
838
  * Insert a translated text to the translated page.
839
  * Currentlly assume that we always insert and move forward - not moving
@@ -864,5 +875,4 @@ function update_translated_page($start, $end, $translated_text)
864
  }
865
 
866
  }
867
-
868
  ?>
56
  //Is current position within the channel tag, i.e. RSS feed
57
  $is_in_channel = FALSE;
58
 
59
+ //Indicates whether automatic translation (i.e. google) is enabled for this page
60
+ $enable_auto_translate;
61
+
62
  /*
63
  * Parse the html page into tags, identify translateable string which
64
  * will be translated.
99
  //Mark tag start position
100
  $tag_start = $pos;
101
  $pos = strpos($page, '>', $pos);
102
+
103
  //Mark tag end position
104
  $tag_end = $pos;
105
 
159
 
160
  }
161
 
 
162
  /*
163
  * Determine if the specified element should be skipped. If so the position
164
  * is moved past end of tag.
210
  case 'channel':
211
  global $is_in_channel;
212
  $is_in_channel = TRUE;
213
+ break;
214
  }
215
 
216
  }
217
 
 
218
  /*
219
  * Handle span tags. Looks for 'no_tranlate' identifier that will disable
220
  * translation for the enclosed text.
240
  $element .= "." . NO_TRANSLATE_CLASS;
241
  }
242
 
 
243
  /*
244
  * Process html tag. Set the direction for rtl languages.
245
  *
300
  //check first for a character data section - treat it like an element
301
  if(is_word('![CDATA['))
302
  {
303
+ $pos += 8; //skip to end of ![CDATA[
304
  }
305
  else
306
  {
310
  $pos++;
311
  }
312
  }
313
+
314
 
315
  return substr($page,$start, $pos - $start);
316
  }
384
  function process_current_tag()
385
  {
386
  global $page, $pos;
387
+
388
 
389
 
390
  //translate only known elements within the body or channel
393
  skip_white_space();
394
  $start = $pos;
395
  $page_length = strlen($page);
396
+
397
+ // Indicates if the html entity should break into a new translation segment.
398
+ $is_breaker = FALSE;
399
+
400
  while($pos < $page_length && $page[$pos] != '<')
401
  {
402
  //will break translation unit when one of the following characters is reached: .,
408
  translate_text($start);
409
  $start = $end_of_entity;
410
  }
411
+
412
  //skip past entity
413
  $pos = $end_of_entity;
414
  }
418
  $pos++;
419
  $start = $pos;
420
  }
421
+ else if($end_of_number = is_number($pos))
422
+ {
423
+ //numbers will break translations segements and will not be included in the translation
424
+ translate_text($start);
425
+ $pos = $start = $end_of_number;
426
+ }
427
  else
428
  {
429
  $pos++;
438
 
439
  }
440
 
 
441
  /*
442
+ * Translate the content of a cdata section. For now we only expect to handle it
443
+ * within RSS feeds.
444
  */
445
  function process_cdata_section()
446
  {
447
  global $page, $pos;
448
+
449
 
450
 
451
  //translate only known elements within rss feeds
454
  skip_white_space();
455
  $start = $pos;
456
  $page_length = strlen($page);
457
+
458
  while($pos < $page_length && !is_word(']]>'))
459
  {
460
  //will break translation unit when one of the following characters is reached: .,
465
  $pos++;
466
  $start = $pos;
467
  }
468
+ else if($end_of_number = is_number($pos))
469
+ {
470
+ //numbers will break translations segements and will not be included in the translation
471
+ translate_text($start);
472
+ $pos = $start = $end_of_number;
473
+ }
474
  else
475
  {
476
  $pos++;
487
 
488
  /**
489
  * Determines position in page marks a transaltable tag in html page or rss feed section.
490
+ * Return TRUE if should be translated otherwise FALSE.
491
  */
492
  function is_translatable_section()
493
  {
494
  global $tags_list, $is_in_channel, $is_in_body;
495
  $rc = FALSE;
496
  $current_tag = end($tags_list);
497
+
498
  if($is_in_body || $current_tag == 'title')
499
  {
500
  $rc = TRUE;
506
  }
507
 
508
 
509
+ return $rc;
510
  }
511
 
512
  /*
533
  $page[$position] == '[' || $page[$position] == ']' ||
534
  $page[$position] == '"' || $page[$position] == '!' ||
535
  $page[$position] == ':' || $page[$position] == '|' ||
536
+ $page[$position] == ';')
 
 
537
  {
538
  //break the sentence into segments regardless of the next character.
539
  $rc = TRUE;
545
  /*
546
  * Determines if the current position marks the begining of an html
547
  * entity. E.g &amp;
548
+ * Return 0 if not an html entity otherwise return the position past this entity. In addition
549
  * the $is_breaker will be set to TRUE if entity should break translation into a new segment.
550
  *
551
  */
582
  return 0;
583
  }
584
 
585
+ /*
586
+ * Determines if the current position marks the begining of a number, e.g. 123 050-391212232
587
+ *
588
+ * Return 0 if not a number otherwise return the position past this number.
589
+ */
590
+ function is_number($position)
591
+ {
592
+ global $page;
593
+ $start = $position;
594
+
595
+ while(is_digit($position) || $page[$position] == '-' || $page[$position] == '+' ||
596
+ (($page[$position] == ',' || $page[$position] == '.' || $page[$position] == '\\' || $page[$position] == '/') && is_digit($position+1)))
597
+ {
598
+ $position++;
599
+ }
600
+
601
+ if($position != $start && (is_white_space($position) || $page[$position] == '<'))
602
+ {
603
+ return $position;
604
+ }
605
+
606
+ return 0;
607
+ }
608
 
609
  /*
610
  * Determine if the current position in page points to a character in the
659
  }
660
  }
661
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
662
  /*
663
  * Skip within buffer past white space characters , Staring from the specified
664
  * position going either forward or backward.
682
  return $index;
683
  }
684
 
 
685
  /*
686
  * Check within page buffer position for the given word.
687
  * param word The word to look for.
688
+ * param index1 Optional position within the page buffer, if not available then the current
689
+ * position ($pos) is used.
690
  * Return TRUE if the word matches otherwise FALSE
691
  */
692
  function is_word($word, $index1)
693
  {
694
  global $page, $pos;
695
  $rc = FALSE;
696
+
697
  if(!isset($index1))
698
  {
699
  //use $pos as the default position if not specified otherwise
700
  $index1 = $pos;
701
  }
702
+
703
  $index2 = 0; //position within word
704
  $word_len = strlen($word);
705
  $page_length = strlen($page);
706
+
707
  while($index1 < $page_length && $index2 < $word_len)
708
  {
709
  if($page[$index1] == $word[$index2])
716
  break;
717
  }
718
  }
719
+
720
  //check if we have full match
721
  if($index2 == $word_len)
722
  {
723
  $rc = TRUE;
724
  }
725
+
726
  return $rc;
727
  }
728
 
729
+ /*
 
730
  * Translate the text between the given start position and the current
731
  * position (pos) within the buffer.
732
  */
733
  function translate_text($start)
734
  {
735
 
736
+ global $page, $pos;
737
 
738
  //trim white space from the start position going forward
739
  skip_white_space($start);
760
  return;
761
  }
762
 
763
+ list($translated_text, $source) = fetch_translation($original_text);
764
 
765
+ insert_translation($original_text, $translated_text, $source, $start, $end);
766
  }
767
 
768
  /*
772
  * param start Marks the start position of the text to be replaced within the original page
773
  * param end Marks the end position of the text to be replaced within the original page
774
  */
775
+ function insert_translation(&$original_text, &$translated_text, $source, $start, $end)
776
  {
777
+ global $segment_id, $is_edit_mode, $tags_list, $enable_auto_translate;
778
 
779
  $is_translated = FALSE;
780
 
781
+ if(($is_edit_mode || ($enable_auto_translate && $translated_text == NULL)) && in_array('body', $tags_list))
 
 
 
 
 
 
 
782
  {
783
+ $span_prefix = SPAN_PREFIX;
784
+ // We will mark translated text with tr_t class and untranslated with tr_u
785
+ $span = "<span class=\"$span_prefix";
786
+
787
+ //Use base64 encoding to make that when the page is translated (i.e. update_translation) we
788
+ //get back exactlly the same string without having the client decode/encode it in anyway.
789
+ $token = "token=\"" . base64_encode($original_text) . "\"";
790
 
791
+ if($translated_text == NULL)
792
  {
793
+ $span .= "u\" id=\"{$span_prefix}{$segment_id}\" $token>";
794
  $span .= $original_text . '</span>';
795
  }
796
  else
797
  {
798
+ $span .= "t\" id=\"{$span_prefix}{$segment_id}\" $token>";
799
  $span .= $translated_text . "</span>";
800
  $is_translated = TRUE;
801
  }
804
  update_translated_page($start, $end, $span);
805
 
806
 
807
+ //Insert image to allow editing this segment (only in explicit edit)
808
+ if($is_edit_mode) {
809
+ $img = get_img_tag($original_text, $translated_text, $source, $segment_id, $is_translated);
810
+ update_translated_page($end + 1, - 1, $img);
811
+ }
812
  //Increment only after both text and image are generated so they
813
  //will be the same for each translated segement
814
  $segment_id++;
815
 
816
  }
817
+ else
818
+ {
819
+ if($translated_text != NULL)
820
+ {
821
+ update_translated_page($start, $end, $translated_text);
822
+ }
823
+ }
824
 
825
 
826
  }
827
 
 
828
  /*
829
  * Scrubs text prior to translation to remove/encode special
830
  * characters.
845
  return $text;
846
  }
847
 
 
848
  /**
849
  * Insert a translated text to the translated page.
850
  * Currentlly assume that we always insert and move forward - not moving
875
  }
876
 
877
  }
 
878
  ?>
readme.txt CHANGED
@@ -1,10 +1,10 @@
1
  === Transposh - translation filter for wordpress ===
2
- Contributors: oferwald
3
  Donate link: http://transposh.org/
4
- Tags: translation, widget, filter, bilingual, multilingual, transposh, language, RTL, Hebrew, Spanish, French, Russian, crowdsourcing, context, wiki
5
  Requires at least: 2.5
6
  Tested up to: 2.7.1
7
- Stable tag: 0.0.3
8
 
9
  Transposh filter allows in context quick translation of websites, it allows you to crowd-source the translation to your users
10
 
@@ -17,6 +17,7 @@ Transposh translation filter for WordPress offers a unique approach to blog tran
17
  * Unique interface for choosing viewable/translatable languages
18
  * Multiple selection for widget appearances
19
  * Translation of external plugins with no changes
 
20
  * Fine grained control for advanced users
21
 
22
  ***We are focused on:***
@@ -35,6 +36,7 @@ Technology has been tested on a large dynamic site with millions of monthly page
35
  1. Activate the plugin through the 'Plugins' menu in WordPress
36
  1. Add the widget to your side bar, decide which interface you prefer
37
  1. On the settings page - define which languages you would like viewable, translatable and which language is currently used (default)
 
38
  1. You are good to go
39
 
40
  == Frequently Asked Questions ==
@@ -55,12 +57,19 @@ The correct place for that would be our [development site](http://trac.transposh
55
 
56
  Another .css file might intervene with our in ways we didn't predict yet, either fix that .css alone or contact us
57
 
 
 
 
 
 
 
 
 
58
  = Why should I allow anonymous translation =
59
 
60
  Wiki has proven itself quite a valid model on the Internet. More people have good intentions than bad intentions and that can be
61
  harnessed to get your message out to more people. Future versions will give more focus on preventing spammers from defacing sites
62
 
63
-
64
  == Screenshots ==
65
 
66
  1. This is a screen shot of a site using Transposh widget on the sidebar
@@ -71,6 +80,11 @@ harnessed to get your message out to more people. Future versions will give more
71
 
72
  == Release notes ==
73
 
 
 
 
 
 
74
  * 2009/03/07 - 0.0.3
75
  * Added ability to get suggestions from Google Translate
76
  * Improved support for RSS feeds translation
1
  === Transposh - translation filter for wordpress ===
2
+ Contributors: oferwald, amirperlman
3
  Donate link: http://transposh.org/
4
+ Tags: translation, widget, filter, bilingual, multilingual, transposh, language, RTL, Hebrew, Spanish, French, Russian, English, Arabic, crowdsourcing, context, wiki
5
  Requires at least: 2.5
6
  Tested up to: 2.7.1
7
+ Stable tag: 0.1.0
8
 
9
  Transposh filter allows in context quick translation of websites, it allows you to crowd-source the translation to your users
10
 
17
  * Unique interface for choosing viewable/translatable languages
18
  * Multiple selection for widget appearances
19
  * Translation of external plugins with no changes
20
+ * Automatic translation mode for all content (including comments!)
21
  * Fine grained control for advanced users
22
 
23
  ***We are focused on:***
36
  1. Activate the plugin through the 'Plugins' menu in WordPress
37
  1. Add the widget to your side bar, decide which interface you prefer
38
  1. On the settings page - define which languages you would like viewable, translatable and which language is currently used (default)
39
+ 1. You may also elect to disable/enable the automatic translation mode
40
  1. You are good to go
41
 
42
  == Frequently Asked Questions ==
57
 
58
  Another .css file might intervene with our in ways we didn't predict yet, either fix that .css alone or contact us
59
 
60
+ = What do the colors of the translation icons mean =
61
+
62
+ There are currently three colors used
63
+
64
+ * Red - no translation was done
65
+ * Yellow - translation was done by a robot
66
+ * Green - translation was done by human (or approved by one)
67
+
68
  = Why should I allow anonymous translation =
69
 
70
  Wiki has proven itself quite a valid model on the Internet. More people have good intentions than bad intentions and that can be
71
  harnessed to get your message out to more people. Future versions will give more focus on preventing spammers from defacing sites
72
 
 
73
  == Screenshots ==
74
 
75
  1. This is a screen shot of a site using Transposh widget on the sidebar
80
 
81
  == Release notes ==
82
 
83
+ * 2009/03/22 - 0.1.0
84
+ * Enabled automatic translation for site readers
85
+ * Added many languages to the default list
86
+ * Upgrade database for supporting translation "sources"
87
+ * Fixed installation bug
88
  * 2009/03/07 - 0.0.3
89
  * Added ability to get suggestions from Google Translate
90
  * Improved support for RSS feeds translation
screenshot-1.png CHANGED
Binary file
screenshot-2.png CHANGED
Binary file
screenshot-3.png CHANGED
Binary file
screenshot-4.png CHANGED
Binary file
screenshot-5.png CHANGED
Binary file
tplogo.png ADDED
Binary file
translate_auto.png ADDED
Binary file
transposh.php CHANGED
@@ -4,7 +4,7 @@
4
  Plugin URI: http://transposh.org/#
5
  Description: Translation filter for WordPress, After enabling please set languages at the <a href="options-general.php?page=Transposh">the options page</a> Want to help? visit our development site at <a href="http://trac.transposh.org/">trac.transposh.org</a>.
6
  Author: Team Transposh
7
- Version: 0.0.3
8
  Author URI: http://transposh.org/
9
  License: GPL (http://www.gnu.org/licenses/gpl.txt)
10
  */
@@ -41,7 +41,7 @@ define("TRANSLATIONS_TABLE", "translations");
41
  define("TRANSLATIONS_LOG", "translations_log");
42
 
43
  //Database version
44
- define("DB_VERSION", "1.0");
45
 
46
  //Constant used as key in options database
47
  define("TRANSPOSH_DB_VERSION", "transposh_db_version");
@@ -219,17 +219,21 @@ function rewrite_url_lang_param($url, $lang, $is_edit, $use_params_only)
219
 
220
  /*
221
  * Fetch translation from db or cache.
222
- * Returns the translated string or NULL if not available.
 
223
  */
224
  function fetch_translation($original)
225
  {
226
  global $wpdb, $lang, $table_name;
227
  $translated = NULL;
228
-
 
 
 
229
 
230
  if(ENABLE_APC && function_exists('apc_fetch'))
231
  {
232
- $cached = apc_fetch($original . $lang, $rc);
233
  if($rc === TRUE)
234
  {
235
 
@@ -242,8 +246,8 @@ function fetch_translation($original)
242
 
243
  if($row !== FALSE)
244
  {
245
- $translated = $row->translated;
246
- $translated = stripslashes($translated);
247
 
248
 
249
  }
@@ -259,7 +263,7 @@ function fetch_translation($original)
259
  }
260
 
261
  //update cache
262
- $rc = apc_store($original . $lang, $cache_entry, 3600);
263
  if($rc === TRUE)
264
  {
265
 
@@ -277,32 +281,46 @@ function fetch_translation($original)
277
  */
278
  function insert_javascript_includes()
279
  {
280
- global $plugin_url, $wp_query;
281
 
282
- if (!($wp_query->query_vars[EDIT_PARAM] == "1" ||
283
- $wp_query->query_vars[EDIT_PARAM] == "true"))
 
 
284
  {
285
- //check permission later - for now just make sure we don't load the
286
  //js code when it is not needed
287
  return;
288
  }
289
-
290
-
291
- $overlib_dir = "$plugin_url/js/overlibmws";
292
 
293
- $js = "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws.js\"></script>";
294
- $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_filter.js\"></script>";
295
- $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_modal.js\"></script>";
296
- $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_overtwo.js\"></script>";
297
- $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_scroll.js\"></script>";
298
- $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_shadow.js\"></script>";
299
 
300
- $js .= "\n<script type=\"text/javascript\" src=\"$plugin_url/js/transposh.js\"></script>\n";
301
- $js .= "\n<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></script>\n";
302
- $js .= "\n<script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>\n";
303
- $js .= "\n<script type=\"text/javascript\">google.load(\"language\", \"1\");</script>\n";
304
 
305
- echo $js;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  }
307
 
308
 
@@ -312,7 +330,7 @@ function insert_javascript_includes()
312
  * param segement_id The id (number) identifying this segment. Needs to be
313
  placed within the img tag for use on client side operation (jquery)
314
  */
315
- function get_img_tag($original, $translation, $segment_id, $is_translated = FALSE)
316
  {
317
  global $plugin_url, $lang, $home_url;
318
  $url = $home_url . '/index.php';
@@ -329,12 +347,16 @@ function get_img_tag($original, $translation, $segment_id, $is_translated = FALS
329
  $add_img = "_fix";
330
  }
331
 
 
 
 
 
332
  $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\"
333
- onclick=\"translate_dialog('$original','$translation','$lang','$url', '$segment_id'); return false;\"
334
  onmouseover=\"hint('$original'); return true;\"
335
  onmouseout=\"nd()\" />";
336
-
337
- return $img;
338
  }
339
 
340
 
@@ -369,7 +391,7 @@ function transposh_css()
369
  */
370
  function init_global_vars()
371
  {
372
- global $home_url, $home_url_quoted, $plugin_url, $table_name, $wpdb;
373
 
374
  $home_url = get_option('home');
375
  $local_dir = preg_replace("/.*\//", "", dirname(__FILE__));
@@ -379,9 +401,9 @@ function init_global_vars()
379
  $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted);
380
 
381
  $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;
 
382
  }
383
 
384
-
385
  /*
386
  * A new translation has been posted, update the translation database.
387
  *
@@ -391,14 +413,14 @@ function update_translation()
391
  global $wpdb, $table_name;
392
 
393
  $ref=getenv('HTTP_REFERER');
394
- $original = $_POST['original'];
395
  $translation = $_POST['translation'];
396
  $lang = $_POST['lang'];
 
397
 
398
  if(!isset($original) || !isset($translation) || !isset($lang))
399
  {
400
- logger("Enter " . __FILE__ . " missing params: $original , $translation, $lang," .
401
- $ref, 0);
402
  return;
403
  }
404
 
@@ -409,23 +431,27 @@ function update_translation()
409
  }
410
 
411
  //Decode & remove already escaped character to avoid double escaping
412
- $original = $wpdb->escape(stripslashes(urldecode($original)));
413
  $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation))));
414
-
415
- $update = "REPLACE INTO $table_name (original, translated, lang)
416
- VALUES ('" . $original . "','" . $translation . "','" . $lang . "')";
 
 
 
 
417
 
418
  $result = $wpdb->query($update);
419
 
420
  if($result !== FALSE)
421
  {
422
- update_transaction_log($original, $translation, $lang);
423
 
424
  //Delete entry from cache
425
  if(ENABLE_APC && function_exists('apc_store'))
426
  {
427
- apc_delete($original . $lang);
428
  }
 
429
 
430
  }
431
  else
@@ -442,7 +468,7 @@ function update_translation()
442
  * Update the transaction log
443
  *
444
  */
445
- function update_transaction_log(&$original, &$translation, &$lang)
446
  {
447
  global $wpdb, $user_ID;
448
  get_currentuserinfo();
@@ -457,19 +483,18 @@ function update_transaction_log(&$original, &$translation, &$lang)
457
  $loguser = $user_ID;
458
  }
459
 
460
- $log = "INSERT INTO ".$wpdb->prefix.TRANSLATIONS_LOG." (original, translated, lang, translated_by)
461
- VALUES ('" . $original . "','" . $translation . "','" . $lang . "','".$loguser."')";
462
 
463
  $result = $wpdb->query($log);
464
 
465
  if($result === FALSE)
466
  {
 
467
 
468
  }
469
-
470
  }
471
 
472
-
473
  /*
474
  * Gets the default language setting, i.e. the source language which
475
  * should not be translated.
@@ -496,8 +521,7 @@ function on_init()
496
  {
497
 
498
  init_global_vars();
499
-
500
-
501
  if ($_POST['translation_posted'])
502
  {
503
  update_translation();
@@ -509,7 +533,6 @@ function on_init()
509
  }
510
  }
511
 
512
-
513
  /*
514
  * Page generation completed - flush buffer.
515
  */
@@ -518,7 +541,6 @@ function on_shutdown()
518
  ob_flush();
519
  }
520
 
521
-
522
  /*
523
  * Update the url rewrite rules to include language identifier
524
  *
@@ -529,7 +551,7 @@ function update_rewrite_rules($rules){
529
  if(!get_option(ENABLE_PERMALINKS_REWRITE))
530
  {
531
 
532
- return $rule;
533
  }
534
 
535
  $newRules = array();
@@ -580,61 +602,46 @@ function parameter_queryvars($qvars)
580
  return $qvars;
581
  }
582
 
583
-
584
  /*
585
  * Setup the translation database.
586
  *
587
  */
588
  function setup_db()
589
  {
590
-
591
- global $wpdb;
592
  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
593
 
594
- $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;
 
 
 
595
 
596
- if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
597
- {
598
 
599
- $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL,
600
- lang CHAR(5) NOT NULL,
601
- translated VARCHAR(256),
602
- PRIMARY KEY (original, lang)) ";
 
603
 
604
 
605
  dbDelta($sql);
606
 
607
- //Verify that newly created table is ready for use.
608
- $insert = "INSERT INTO " . $table_name . " (original, translated, lang) " .
609
- "VALUES ('Hello','Hi There','zz')";
610
 
611
- $result = $wpdb->query($insert);
612
 
613
- if($result === FALSE)
614
- {
615
-
616
- }
617
- else
618
- {
619
-
620
- add_option(TRANSPOSH_DB_VERSION, DB_VERSION);
621
- }
622
- }
623
-
624
- $table_name = $wpdb->prefix . TRANSLATIONS_LOG;
625
-
626
- if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name)
627
- {
628
 
629
  $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL,
630
  lang CHAR(5) NOT NULL,
631
  translated VARCHAR(256),
632
  translated_by VARCHAR(15),
 
633
  timestamp TIMESTAMP,
634
  PRIMARY KEY (original, lang, timestamp)) ";
635
 
636
 
637
  dbDelta($sql);
 
638
  }
639
 
640
 
@@ -711,7 +718,7 @@ function plugin_install_error()
711
  echo $admin_msg;
712
 
713
  if (function_exists('deactivate_plugins') ) {
714
- deactivate_plugins("transposh/translate.php", "translate.php");
715
  echo '<br> This plugin has been automatically deactivated.';
716
  }
717
 
@@ -730,40 +737,42 @@ function plugin_loaded()
730
  global $admin_msg;
731
 
732
 
733
- if (get_option(TRANSPOSH_DB_VERSION) == NULL)
 
 
734
  {
735
- $admin_msg = "Failed to locate the translation table <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>";
 
736
 
737
 
738
  //Some error occured - notify admin and deactivate plugin
739
- add_action('admin_notices', 'plugin_install_error');
740
  }
741
 
742
- $db_version = get_option(TRANSPOSH_DB_VERSION);
743
-
744
  if ($db_version != DB_VERSION)
745
  {
746
- $admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ") <br>";
747
 
748
 
749
  //Some error occured - notify admin and deactivate plugin
750
  add_action('admin_notices', 'plugin_install_error');
751
  }
 
752
  }
753
 
754
  /*
755
- * Gets the plugin name to be used in activation/decativation hooks.
756
  * Keep only the file name and its containing directory. Don't use the full
757
- * path as it will break when using symbollic links.
758
  */
759
  function get_plugin_name()
760
  {
761
- $file = __FILE__;
762
  $file = str_replace('\\','/',$file); // sanitize for Win32 installs
763
  $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
764
-
765
  //keep only the file name and its parent directory
766
- $file = preg_replace('/.*(\/[^\/]+\/[^\/]+)$/', '$1', $file);
767
 
768
  return $file;
769
  }
@@ -778,4 +787,6 @@ add_action('shutdown', 'on_shutdown');
778
  add_action( 'plugins_loaded', 'plugin_loaded');
779
  register_activation_hook(get_plugin_name(), 'plugin_activate');
780
  register_deactivation_hook(get_plugin_name(),'plugin_deactivate');
 
 
781
  ?>
4
  Plugin URI: http://transposh.org/#
5
  Description: Translation filter for WordPress, After enabling please set languages at the <a href="options-general.php?page=Transposh">the options page</a> Want to help? visit our development site at <a href="http://trac.transposh.org/">trac.transposh.org</a>.
6
  Author: Team Transposh
7
+ Version: 0.1.0
8
  Author URI: http://transposh.org/
9
  License: GPL (http://www.gnu.org/licenses/gpl.txt)
10
  */
41
  define("TRANSLATIONS_LOG", "translations_log");
42
 
43
  //Database version
44
+ define("DB_VERSION", "1.02");
45
 
46
  //Constant used as key in options database
47
  define("TRANSPOSH_DB_VERSION", "transposh_db_version");
219
 
220
  /*
221
  * Fetch translation from db or cache.
222
+ * Returns An array that contains the translated string and it source.
223
+ * Will return NULL if no translation is available.
224
  */
225
  function fetch_translation($original)
226
  {
227
  global $wpdb, $lang, $table_name;
228
  $translated = NULL;
229
+
230
+
231
+ //The original is saved in db in its escaped form
232
+ $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8'));
233
 
234
  if(ENABLE_APC && function_exists('apc_fetch'))
235
  {
236
+ $cached = apc_fetch($original .'___'. $lang, $rc);
237
  if($rc === TRUE)
238
  {
239
 
246
 
247
  if($row !== FALSE)
248
  {
249
+ $translated_text = stripslashes($row->translated);
250
+ $translated = array($translated_text, $row->source);
251
 
252
 
253
  }
263
  }
264
 
265
  //update cache
266
+ $rc = apc_store($original .'___'. $lang, $cache_entry, 3600);
267
  if($rc === TRUE)
268
  {
269
 
281
  */
282
  function insert_javascript_includes()
283
  {
284
+ global $plugin_url, $wp_query, $lang, $home_url, $enable_auto_translate;
285
 
286
+ $is_edit_param_enabled = ($wp_query->query_vars[EDIT_PARAM] == "1" ||
287
+ $wp_query->query_vars[EDIT_PARAM] == "true");
288
+
289
+ if (!$is_edit_param_enabled && ! $enable_auto_translate)
290
  {
291
+ //TODO: check permission later - for now just make sure we don't load the
292
  //js code when it is not needed
293
  return;
294
  }
 
 
 
295
 
 
 
 
 
 
 
296
 
297
+ $overlib_dir = "$plugin_url/js/overlibmws";
 
 
 
298
 
299
+ if($is_edit_param_enabled)
300
+ {
301
+ $js = "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws.js\"></script>";
302
+ $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_filter.js\"></script>";
303
+ $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_modal.js\"></script>";
304
+ $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_overtwo.js\"></script>";
305
+ $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_scroll.js\"></script>";
306
+ $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_shadow.js\"></script>";
307
+ }
308
+
309
+ if($is_edit_param_enabled || $enable_auto_translate)
310
+ {
311
+ $js .= "\n<script type=\"text/javascript\" src=\"$plugin_url/js/transposh.js\"></script>";
312
+ $js .= "\n<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></script>";
313
+ $js .= "\n<script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>";
314
+ $js .= "\n<script type=\"text/javascript\">google.load(\"language\", \"1\");</script>";
315
+
316
+
317
+ $post_url = $home_url . '/index.php';
318
+ $js .= "\n<script type=\"text/javascript\">var transposh_post_url='$post_url';var transposh_target_lang='$lang';</script>";
319
+
320
+ $js .= "\n<script type=\"text/javascript\">$(document).ready(function() {do_auto_translate();});</script>";
321
+ }
322
+
323
+ echo $js . "\n";
324
  }
325
 
326
 
330
  * param segement_id The id (number) identifying this segment. Needs to be
331
  placed within the img tag for use on client side operation (jquery)
332
  */
333
+ function get_img_tag($original, $translation, $source, $segment_id, $is_translated = FALSE)
334
  {
335
  global $plugin_url, $lang, $home_url;
336
  $url = $home_url . '/index.php';
347
  $add_img = "_fix";
348
  }
349
 
350
+ if ($source == 1) {
351
+ $add_img = "_auto";
352
+ }
353
+
354
  $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\"
355
+ onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\"
356
  onmouseover=\"hint('$original'); return true;\"
357
  onmouseout=\"nd()\" />";
358
+
359
+ return $img;
360
  }
361
 
362
 
391
  */
392
  function init_global_vars()
393
  {
394
+ global $home_url, $home_url_quoted, $plugin_url, $table_name, $wpdb, $enable_auto_translate;
395
 
396
  $home_url = get_option('home');
397
  $local_dir = preg_replace("/.*\//", "", dirname(__FILE__));
401
  $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted);
402
 
403
  $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;
404
+ $enable_auto_translate = get_option(ENABLE_AUTO_TRANSLATE,1) && is_translator();
405
  }
406
 
 
407
  /*
408
  * A new translation has been posted, update the translation database.
409
  *
413
  global $wpdb, $table_name;
414
 
415
  $ref=getenv('HTTP_REFERER');
416
+ $original = base64_decode($_POST['token'], TRUE);
417
  $translation = $_POST['translation'];
418
  $lang = $_POST['lang'];
419
+ $source = $_POST['source'];
420
 
421
  if(!isset($original) || !isset($translation) || !isset($lang))
422
  {
423
+
 
424
  return;
425
  }
426
 
431
  }
432
 
433
  //Decode & remove already escaped character to avoid double escaping
 
434
  $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation))));
435
+
436
+ //The original content is encoded as base64 before it is sent (i.e. token), after we
437
+ //decode it should just the same after it was parsed.
438
+ $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8'));
439
+
440
+ $update = "REPLACE INTO $table_name (original, translated, lang, source)
441
+ VALUES ('" . $original . "','" . $translation . "','" . $lang . "','" . $source . "')";
442
 
443
  $result = $wpdb->query($update);
444
 
445
  if($result !== FALSE)
446
  {
447
+ update_transaction_log($original, $translation, $lang, $source);
448
 
449
  //Delete entry from cache
450
  if(ENABLE_APC && function_exists('apc_store'))
451
  {
452
+ apc_delete($original .'___'. $lang);
453
  }
454
+
455
 
456
  }
457
  else
468
  * Update the transaction log
469
  *
470
  */
471
+ function update_transaction_log(&$original, &$translation, &$lang, $source)
472
  {
473
  global $wpdb, $user_ID;
474
  get_currentuserinfo();
483
  $loguser = $user_ID;
484
  }
485
 
486
+ $log = "INSERT INTO ".$wpdb->prefix.TRANSLATIONS_LOG." (original, translated, lang, translated_by, source)
487
+ VALUES ('" . $original . "','" . $translation . "','" . $lang . "','".$loguser."','".$source."')";
488
 
489
  $result = $wpdb->query($log);
490
 
491
  if($result === FALSE)
492
  {
493
+
494
 
495
  }
 
496
  }
497
 
 
498
  /*
499
  * Gets the default language setting, i.e. the source language which
500
  * should not be translated.
521
  {
522
 
523
  init_global_vars();
524
+
 
525
  if ($_POST['translation_posted'])
526
  {
527
  update_translation();
533
  }
534
  }
535
 
 
536
  /*
537
  * Page generation completed - flush buffer.
538
  */
541
  ob_flush();
542
  }
543
 
 
544
  /*
545
  * Update the url rewrite rules to include language identifier
546
  *
551
  if(!get_option(ENABLE_PERMALINKS_REWRITE))
552
  {
553
 
554
+ return $rules;
555
  }
556
 
557
  $newRules = array();
602
  return $qvars;
603
  }
604
 
 
605
  /*
606
  * Setup the translation database.
607
  *
608
  */
609
  function setup_db()
610
  {
611
+
612
+ global $wpdb;
613
  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
614
 
615
+ $installed_ver = get_option(TRANSPOSH_DB_VERSION);
616
+
617
+ if( $installed_ver != DB_VERSION ) {
618
+ $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;
619
 
 
 
620
 
621
+ $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL,
622
+ lang CHAR(5) NOT NULL,
623
+ translated VARCHAR(256),
624
+ source TINYINT NOT NULL,
625
+ PRIMARY KEY (original, lang)) ";
626
 
627
 
628
  dbDelta($sql);
629
 
 
 
 
630
 
631
+ $table_name = $wpdb->prefix . TRANSLATIONS_LOG;
632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633
 
634
  $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL,
635
  lang CHAR(5) NOT NULL,
636
  translated VARCHAR(256),
637
  translated_by VARCHAR(15),
638
+ source TINYINT NOT NULL,
639
  timestamp TIMESTAMP,
640
  PRIMARY KEY (original, lang, timestamp)) ";
641
 
642
 
643
  dbDelta($sql);
644
+ update_option(TRANSPOSH_DB_VERSION, DB_VERSION);
645
  }
646
 
647
 
718
  echo $admin_msg;
719
 
720
  if (function_exists('deactivate_plugins') ) {
721
+ deactivate_plugins(get_plugin_name(), "translate.php");
722
  echo '<br> This plugin has been automatically deactivated.';
723
  }
724
 
737
  global $admin_msg;
738
 
739
 
740
+ $db_version = get_option(TRANSPOSH_DB_VERSION);
741
+
742
+ if ($db_version != DB_VERSION)
743
  {
744
+ setup_db();
745
+ //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ") <br>";
746
 
747
 
748
  //Some error occured - notify admin and deactivate plugin
749
+ //add_action('admin_notices', 'plugin_install_error');
750
  }
751
 
 
 
752
  if ($db_version != DB_VERSION)
753
  {
754
+ $admin_msg = "Failed to locate the translation table <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>";
755
 
756
 
757
  //Some error occured - notify admin and deactivate plugin
758
  add_action('admin_notices', 'plugin_install_error');
759
  }
760
+
761
  }
762
 
763
  /*
764
+ * Gets the plugin name to be used in activation/decativation hooks.
765
  * Keep only the file name and its containing directory. Don't use the full
766
+ * path as it will break when using symbollic links.
767
  */
768
  function get_plugin_name()
769
  {
770
+ $file = __FILE__;
771
  $file = str_replace('\\','/',$file); // sanitize for Win32 installs
772
  $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
773
+
774
  //keep only the file name and its parent directory
775
+ $file = preg_replace('/.*\/([^\/]+\/[^\/]+)$/', '$1', $file);
776
 
777
  return $file;
778
  }
787
  add_action( 'plugins_loaded', 'plugin_loaded');
788
  register_activation_hook(get_plugin_name(), 'plugin_activate');
789
  register_deactivation_hook(get_plugin_name(),'plugin_deactivate');
790
+
791
+ add_filter('rewrite_rules_array', 'update_rewrite_rules');
792
  ?>
transposh_admin.php CHANGED
@@ -18,8 +18,8 @@
18
 
19
  /*
20
  * Provide the admin page for configuring the translation options. eg. what languages ?
21
- * who is allowed to translate ?
22
- *
23
  */
24
 
25
 
@@ -27,44 +27,47 @@ require_once("constants.php");
27
 
28
 
29
  /*
30
- * Add transposh to the admin menu.
31
  *
32
  */
33
  function transposh_admin_menu()
34
  {
35
- add_options_page('Transposh','Transposh', 6,'Transposh', 'transposh_admin_page');
36
  }
37
 
38
 
39
  /*
40
- * Create the admin page.
41
  *
42
  */
43
  function transposh_admin_page()
44
  {
45
-
46
 
47
- if($_POST['transposh_admin_posted'])
48
- {
49
  update_admin_options();
50
  }
51
 
52
- echo '<div class="wrap alternate">
53
- <h2>Transposh</h2>
54
- <form action="?page=Transposh" method="post">
55
- <h3>Supported Languages</h3>';
 
 
 
 
56
 
57
- insert_supported_langs();
58
- echo '<br/> <h3>Who can translate ?</h3>';
59
- insert_permissions();
60
 
61
- echo '<br/> <h3>Rewrite URLs </h3>';
62
- insert_permalink_rewrite_option();
63
 
64
- echo '<input type="hidden" name="transposh_admin_posted" value="1" />
65
- <p class="submit"><input type="submit" value="Save Changes" /></p>
66
- </form>
67
- </div>';
68
  }
69
 
70
  /*
@@ -73,9 +76,9 @@ function transposh_admin_page()
73
  */
74
  function insert_supported_langs()
75
  {
76
- global $languages, $plugin_url;
77
-
78
- echo '
79
  <script type="text/javascript" >
80
  function chbx_change(lang)
81
  {
@@ -85,59 +88,57 @@ function insert_supported_langs()
85
  var edit = lang + "_edit";
86
  document.getElementById(edit).checked = true;
87
  }
88
-
89
  }
90
  </script>
91
-
92
  <table>
93
  <tr>';
94
-
95
-
96
- $columns = 2;
97
-
98
- for($hdr=0; $hdr < $columns; $hdr++)
99
- {
100
- echo '<th>Language </th> <th>Viewable </th> <th>Translatable </th>
101
- <th>Default</th><th style="padding-right: 80px"> </th>';
102
- }
103
-
104
- echo '</tr>';
105
-
106
- foreach($languages as $code => $lang)
107
- {
108
- list ($language,$flag) = explode (",",$lang);
109
- if($i % $columns == 0)
110
- {
111
- echo '</tr>';
112
- }
113
- echo "\n";
114
-
115
- $i++;
116
-
117
- echo "<td><img src=\"$plugin_url/flags/$flag.png\"/>&nbsp;$language</td>";
118
- echo '<td align="center"> <input type="checkbox" id="' . $code .'_view" name="' .
119
- $code . '_view" onChange="chbx_change(\'' . $code . '\')"' . is_viewable($code) . '/></td>';
120
- echo "\n";
121
- echo '<td align="center"> <input type="checkbox" id="' . $code . '_edit" name="' .
122
- $code . '_edit" ' . is_editable($code). '/></td>';
123
- echo "\n";
124
- echo "<td align=\"center\"><input type=\"radio\" name=\"default_lang\" value=\"$code\"" .
125
- is_default_lang($code). "/> </td>";
126
-
127
- if($i % $columns == 0)
128
- {
129
- echo '</tr>';
130
- }
131
- else
132
- {
133
- echo "<td><style padding-right: 60px></style></td>";
134
- }
135
-
136
- echo "\n";
137
-
138
- }
139
 
140
- echo '</table>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  }
142
 
143
 
@@ -147,16 +148,15 @@ function insert_supported_langs()
147
  */
148
  function is_editable($code)
149
  {
150
- $langs = get_option(EDITABLE_LANGS);
151
-
152
- if(strstr($langs, $code))
153
- {
154
- return "checked";
155
- }
156
-
157
- return "";
158
- }
159
 
 
 
 
 
 
 
 
160
 
161
  /*
162
  * Determine if the given language code is currentlly viewable
@@ -164,13 +164,13 @@ function is_editable($code)
164
  */
165
  function is_viewable($code)
166
  {
167
- $langs = get_option(VIEWABLE_LANGS);
168
- if(strstr($langs, $code))
169
- {
170
- return "checked";
171
- }
172
-
173
- return "";
174
  }
175
 
176
  /*
@@ -179,21 +179,21 @@ function is_viewable($code)
179
  */
180
  function is_default_lang($code)
181
  {
182
- global $languages;
183
-
184
- $default = get_option(DEFAULT_LANG);
185
-
186
- if(!$languages[$default])
187
- {
188
- $default = "en";
189
- }
190
-
191
- if($default == $code)
192
- {
193
- return "checked";
194
- }
195
-
196
- return "";
197
  }
198
 
199
  /*
@@ -202,21 +202,20 @@ function is_default_lang($code)
202
  */
203
  function insert_permissions()
204
  {
205
- global $wp_roles;
206
 
207
- //display known roles and their permission to translate
208
- foreach($wp_roles->get_names() as $role_name => $something)
209
- {
210
- echo '<input type="checkbox" value="1" name="' . $role_name . '" ' . can_translate($role_name) .
211
  '" />' . $role_name . '&nbsp&nbsp&nbsp</input>';
212
- }
213
 
214
- //Add our own custom role
215
- echo '<input type="checkbox" value="1" name="anonymous"' .
216
- can_translate('anonymous') . '" /> Anonymous</input>';
217
  }
218
 
219
-
220
  /*
221
  * Insert the option to enable/disable rewrite of perlmalinks.
222
  * When disabled only parameters will be used to identify the current language.
@@ -224,116 +223,137 @@ function insert_permissions()
224
  */
225
  function insert_permalink_rewrite_option()
226
  {
227
- $checked = "";
228
- if(get_option(ENABLE_PERMALINKS_REWRITE))
229
- {
230
- $checked = 'checked';
231
- }
232
-
233
- echo '<input type="checkbox" value="1" name="enable_permalinks"' .
234
- $checked . '" /> Rewrite URLs to be search engine friendly,
235
- e.g. (http://wordpress.org/<strong> en</strong>).
236
- Requires that permalinks will be enabled. </input>';
 
237
  }
238
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
 
240
  /*
241
- * Indicates whether the given role can translate.
242
- * Return either "checked" or ""
243
  */
244
  function can_translate($role_name)
245
  {
246
- global $wp_roles;
247
- if($role_name != 'anonymous')
248
- {
249
- $role = $wp_roles->get_role($role_name);
250
- if(isset($role) && $role->has_cap(TRANSLATOR))
251
- {
252
- return 'checked';
253
- }
254
- }
255
- else
256
- {
257
- $allow_anonymous = get_option(ANONYMOUS_TRANSLATION);
258
- if($allow_anonymous == "1")
259
- {
260
- return 'checked';
261
- }
262
- }
263
-
264
- return "";
265
  }
266
 
267
  /*
268
- * Handle newly posted admin options.
269
  *
270
- */
271
  function update_admin_options()
272
  {
273
-
274
- global $wp_roles, $languages;
275
- $viewable_langs = array();
276
- $editable_langs = array();
277
-
278
- //update roles and capabilities
279
- foreach($wp_roles->get_names() as $role_name => $something)
280
- {
281
- $role = $wp_roles->get_role($role_name);
282
- if($_POST[$role_name] == "1")
283
- {
284
- $role->add_cap(TRANSLATOR);
285
- }
286
- else
287
- {
288
- $role->remove_cap(TRANSLATOR);
289
- }
290
- }
291
-
292
- //Anonymous needs to be handled differently as it does not have a role
293
- if($_POST['anonymous'] == "1")
294
- {
295
- update_option(ANONYMOUS_TRANSLATION, 1);
296
- }
297
- else
298
- {
299
- update_option(ANONYMOUS_TRANSLATION, 0);
300
- }
301
-
302
-
303
- //Update the list of supported/editable languages
304
- foreach($languages as $code => $lang)
305
- {
306
- if($_POST[$code . '_view'])
307
- {
308
- $viewable_langs[] = $code;
309
- }
310
 
311
- if($_POST[$code . '_edit'])
312
- {
313
- $editable_langs[] = $code;
314
- }
315
- }
316
-
317
- update_option(VIEWABLE_LANGS, implode(',', $viewable_langs));
318
- update_option(EDITABLE_LANGS, implode(',', $editable_langs));
319
- update_option(DEFAULT_LANG, $_POST['default_lang']);
320
-
321
- if(get_option(ENABLE_PERMALINKS_REWRITE) != $_POST['enable_permalinks'])
322
- {
323
- global $wp_rewrite;
324
- update_option(ENABLE_PERMALINKS_REWRITE, $_POST['enable_permalinks']);
325
-
326
- //rewrite rules
327
- add_filter('rewrite_rules_array', 'update_rewrite_rules');
328
- $wp_rewrite->flush_rules();
329
- }
330
-
331
-
332
- echo '<div id="message"class="updated fade">';
333
- echo ('<p> Changes saved</p>');
334
- echo '</div>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
335
  }
336
 
337
  add_action('admin_menu', 'transposh_admin_menu');
338
-
339
  ?>
18
 
19
  /*
20
  * Provide the admin page for configuring the translation options. eg. what languages ?
21
+ * who is allowed to translate ?
22
+ *
23
  */
24
 
25
 
27
 
28
 
29
  /*
30
+ * Add transposh to the admin menu.
31
  *
32
  */
33
  function transposh_admin_menu()
34
  {
35
+ add_options_page('Transposh','Transposh', 6,'Transposh', 'transposh_admin_page');
36
  }
37
 
38
 
39
  /*
40
+ * Create the admin page.
41
  *
42
  */
43
  function transposh_admin_page()
44
  {
45
+
46
 
47
+ if($_POST['transposh_admin_posted'])
48
+ {
49
  update_admin_options();
50
  }
51
 
52
+ echo '<div class="wrap alternate">'.
53
+ '<h2>Transposh</h2>'.
54
+ '<form action="?page=Transposh" method="post">'.
55
+ '<h3>Supported Languages</h3>';
56
+
57
+ insert_supported_langs();
58
+ echo '<br/><h3>Who can translate ?</h3>';
59
+ insert_permissions();
60
 
61
+ echo '<br/><h3>Rewrite URLs</h3>';
62
+ insert_permalink_rewrite_option();
 
63
 
64
+ echo '<br/><h3>Enable automatic translation</h3>';
65
+ insert_auto_translate_option();
66
 
67
+ echo '<input type="hidden" name="transposh_admin_posted" value="1" />'.
68
+ '<p class="submit"><input type="submit" value="Save Changes" /></p>'.
69
+ '</form>'.
70
+ '</div>';
71
  }
72
 
73
  /*
76
  */
77
  function insert_supported_langs()
78
  {
79
+ global $languages, $plugin_url;
80
+
81
+ echo '
82
  <script type="text/javascript" >
83
  function chbx_change(lang)
84
  {
88
  var edit = lang + "_edit";
89
  document.getElementById(edit).checked = true;
90
  }
91
+
92
  }
93
  </script>
94
+
95
  <table>
96
  <tr>';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
+
99
+ $columns = 2;
100
+
101
+ for($hdr=0; $hdr < $columns; $hdr++)
102
+ {
103
+ echo '<th>Language</th><th>Viewable</th><th>Translatable</th>
104
+ <th>Default</th><th style="padding-right: 80px"></th>';
105
+ }
106
+
107
+ echo '</tr>';
108
+
109
+ foreach($languages as $code => $lang)
110
+ {
111
+ list ($language,$flag) = explode (",",$lang);
112
+ if($i % $columns == 0)
113
+ {
114
+ echo '</tr>';
115
+ }
116
+ echo "\n";
117
+
118
+ $i++;
119
+
120
+ echo "<td><img src=\"$plugin_url/flags/$flag.png\"/>&nbsp;$language</td>";
121
+ echo '<td align="center"> <input type="checkbox" id="' . $code .'_view" name="' .
122
+ $code . '_view" onChange="chbx_change(\'' . $code . '\')"' . is_viewable($code) . '/></td>';
123
+ echo "\n";
124
+ echo '<td align="center"> <input type="checkbox" id="' . $code . '_edit" name="' .
125
+ $code . '_edit" ' . is_editable($code). '/></td>';
126
+ echo "\n";
127
+ echo "<td align=\"center\"><input type=\"radio\" name=\"default_lang\" value=\"$code\"" .
128
+ is_default_lang($code). "/> </td>";
129
+
130
+ if($i % $columns == 0)
131
+ {
132
+ echo '</tr>';
133
+ }
134
+ else
135
+ {
136
+ echo "<td><style padding-right: 60px></style></td>";
137
+ }
138
+ echo "\n";
139
+ }
140
+
141
+ echo '</table>';
142
  }
143
 
144
 
148
  */
149
  function is_editable($code)
150
  {
151
+ $langs = get_option(EDITABLE_LANGS);
 
 
 
 
 
 
 
 
152
 
153
+ if(strstr($langs, $code))
154
+ {
155
+ return "checked";
156
+ }
157
+
158
+ return "";
159
+ }
160
 
161
  /*
162
  * Determine if the given language code is currentlly viewable
164
  */
165
  function is_viewable($code)
166
  {
167
+ $langs = get_option(VIEWABLE_LANGS);
168
+ if(strstr($langs, $code))
169
+ {
170
+ return "checked";
171
+ }
172
+
173
+ return "";
174
  }
175
 
176
  /*
179
  */
180
  function is_default_lang($code)
181
  {
182
+ global $languages;
183
+
184
+ $default = get_option(DEFAULT_LANG);
185
+
186
+ if(!$languages[$default])
187
+ {
188
+ $default = "en";
189
+ }
190
+
191
+ if($default == $code)
192
+ {
193
+ return "checked";
194
+ }
195
+
196
+ return "";
197
  }
198
 
199
  /*
202
  */
203
  function insert_permissions()
204
  {
205
+ global $wp_roles;
206
 
207
+ //display known roles and their permission to translate
208
+ foreach($wp_roles->get_names() as $role_name => $something)
209
+ {
210
+ echo '<input type="checkbox" value="1" name="' . $role_name . '" ' . can_translate($role_name) .
211
  '" />' . $role_name . '&nbsp&nbsp&nbsp</input>';
212
+ }
213
 
214
+ //Add our own custom role
215
+ echo '<input type="checkbox" value="1" name="anonymous"' .
216
+ can_translate('anonymous') . '" /> Anonymous</input>';
217
  }
218
 
 
219
  /*
220
  * Insert the option to enable/disable rewrite of perlmalinks.
221
  * When disabled only parameters will be used to identify the current language.
223
  */
224
  function insert_permalink_rewrite_option()
225
  {
226
+ $checked = "";
227
+ if(get_option(ENABLE_PERMALINKS_REWRITE))
228
+ {
229
+ $checked = 'checked';
230
+ }
231
+
232
+ echo '<input type="checkbox" value="1" name="enable_permalinks"'. $checked . '"/>'.
233
+ 'Rewrite URLs to be search engine friendly, '.
234
+ 'e.g. (http://wordpress.org/<strong> en</strong>). '.
235
+ 'Requires that permalinks will be enabled.'.
236
+ '</input>';
237
  }
238
 
239
+ /*
240
+ * Insert the option to enable/disable automatic translation.
241
+ * Enabled by default.
242
+ *
243
+ */
244
+ function insert_auto_translate_option()
245
+ {
246
+ $checked = "";
247
+ if(get_option(ENABLE_AUTO_TRANSLATE,1))
248
+ {
249
+ $checked = 'checked';
250
+ }
251
+
252
+ echo '<input type="checkbox" value="1" name="enable_autotranslate"'.$checked.'"/>'.
253
+ 'Allow automatic translation of pages (currently using Google Translate).'.
254
+ '</input>';
255
+ }
256
 
257
  /*
258
+ * Indicates whether the given role can translate.
259
+ * Return either "checked" or ""
260
  */
261
  function can_translate($role_name)
262
  {
263
+ global $wp_roles;
264
+ if($role_name != 'anonymous')
265
+ {
266
+ $role = $wp_roles->get_role($role_name);
267
+ if(isset($role) && $role->has_cap(TRANSLATOR))
268
+ {
269
+ return 'checked';
270
+ }
271
+ }
272
+ else
273
+ {
274
+ $allow_anonymous = get_option(ANONYMOUS_TRANSLATION);
275
+ if($allow_anonymous == "1")
276
+ {
277
+ return 'checked';
278
+ }
279
+ }
280
+
281
+ return "";
282
  }
283
 
284
  /*
285
+ * Handle newly posted admin options.
286
  *
287
+ */
288
  function update_admin_options()
289
  {
290
+
291
+ global $wp_roles, $languages;
292
+ $viewable_langs = array();
293
+ $editable_langs = array();
294
+
295
+ //update roles and capabilities
296
+ foreach($wp_roles->get_names() as $role_name => $something)
297
+ {
298
+ $role = $wp_roles->get_role($role_name);
299
+ if($_POST[$role_name] == "1")
300
+ {
301
+ $role->add_cap(TRANSLATOR);
302
+ }
303
+ else
304
+ {
305
+ $role->remove_cap(TRANSLATOR);
306
+ }
307
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
308
 
309
+ //Anonymous needs to be handled differently as it does not have a role
310
+ if($_POST['anonymous'] == "1")
311
+ {
312
+ update_option(ANONYMOUS_TRANSLATION, 1);
313
+ }
314
+ else
315
+ {
316
+ update_option(ANONYMOUS_TRANSLATION, 0);
317
+ }
318
+
319
+
320
+ //Update the list of supported/editable languages
321
+ foreach($languages as $code => $lang)
322
+ {
323
+ if($_POST[$code . '_view'])
324
+ {
325
+ $viewable_langs[] = $code;
326
+ }
327
+
328
+ if($_POST[$code . '_edit'])
329
+ {
330
+ $editable_langs[] = $code;
331
+ }
332
+ }
333
+
334
+ update_option(VIEWABLE_LANGS, implode(',', $viewable_langs));
335
+ update_option(EDITABLE_LANGS, implode(',', $editable_langs));
336
+ update_option(DEFAULT_LANG, $_POST['default_lang']);
337
+
338
+ if(get_option(ENABLE_PERMALINKS_REWRITE) != $_POST['enable_permalinks'])
339
+ {
340
+ global $wp_rewrite;
341
+ update_option(ENABLE_PERMALINKS_REWRITE, $_POST['enable_permalinks']);
342
+
343
+ //rewrite rules
344
+ add_filter('rewrite_rules_array', 'update_rewrite_rules');
345
+ $wp_rewrite->flush_rules();
346
+ }
347
+
348
+ if(get_option(ENABLE_AUTO_TRANSLATE,1) != $_POST['enable_autotranslate'])
349
+ {
350
+ update_option(ENABLE_AUTO_TRANSLATE, $_POST['enable_autotranslate']);
351
+ }
352
+
353
+ echo '<div id="message"class="updated fade">';
354
+ echo ('<p> Changes saved</p>');
355
+ echo '</div>';
356
  }
357
 
358
  add_action('admin_menu', 'transposh_admin_menu');
 
359
  ?>
transposh_widget.php CHANGED
@@ -35,13 +35,13 @@ function init_transposh()
35
 
36
 
37
  global $wp_rewrite;
38
-
39
  $ref=getenv('HTTP_REFERER');
40
  $lang = $_POST[LANG_PARAM];
41
 
42
- //remove existing language settings.
43
  $ref = cleanup_url($ref);
44
-
45
  if($lang != "none")
46
  {
47
  $use_params_only = !$wp_rewrite->using_permalinks();
@@ -99,38 +99,40 @@ function transposh_widget($args)
99
  $is_translator = is_translator();
100
 
101
  $is_showing_languages = FALSE;
102
-
103
- echo $before_widget . $before_title . __(no_translate("Transposh")) . $after_title;
104
-
 
105
  switch ($options['style']) {
106
  case 1: // flags
107
  //keep the flags in the same direction regardless of the overall page direction
108
- echo "<div style=\"text-align: left;\" class=\"" . NO_TRANSLATE_CLASS . "\" >";
109
 
110
  global $plugin_url;
111
  $using_permalinks = $wp_rewrite->using_permalinks();
112
-
113
  foreach($languages as $code => $lang2)
114
  {
115
  list($language,$flag) = explode (",",$lang2);
116
 
117
- //remove any language identifier
118
  $page_url = cleanup_url($page_url);
119
-
120
  //Only show languages which are viewable or (editable and the user is a translator)
121
  if(strstr($viewable_langs, $code) ||
122
  ($is_translator && strstr($editable_langs, $code)))
123
  {
124
- $page_url = rewrite_url_lang_param($page_url, $code, $is_edit, !$using_permalinks);
125
-
126
- echo "<a href=\"" . $page_url . "\">
127
- <img src=\"$plugin_url/flags/$flag.png\" title=\"$language\" alt=\"$language\"
128
- style=\"padding: 1px 3px\"/></a>";
 
129
  $is_showing_languages = TRUE;
130
  }
131
  }
132
  echo "</div>";
133
-
134
  // this is the form for the edit...
135
  echo "<form action=\"$page_url\" method=\"post\">";
136
  echo "<input type=\"hidden\" name=\"lang\" id=\"lang\" value=\"$lang\"/>";
@@ -138,7 +140,7 @@ function transposh_widget($args)
138
  default: // language list
139
 
140
  echo "<form action=\"$page_url\" method=\"post\">";
141
- echo "<span class=\"" . NO_TRANSLATE_CLASS . "\" >";
142
  echo "<select name=\"lang\" id=\"lang\" onchange=\"Javascript:this.form.submit();\">";
143
  echo "<option value=\"none\">[Language]</option>";
144
 
@@ -170,7 +172,7 @@ function transposh_widget($args)
170
  ($is_edit ? "checked=\"1\"" : "0") .
171
  "\" onClick=\"this.form.submit();\"/>&nbsp;Edit Translation";
172
  }
173
-
174
  echo "<input type=\"hidden\" name=\"transposh_widget_posted\" value=\"1\"/>";
175
  }
176
  else
@@ -180,29 +182,29 @@ function transposh_widget($args)
180
  }
181
 
182
  echo "</form>";
183
-
 
184
  echo $after_widget;
185
  }
186
 
187
-
188
  /*
189
- *Remove from url any language (or editing) params that were added for our use.
190
- *Return the scrubed url
191
  */
192
  function cleanup_url($url)
193
  {
194
  global $home_url, $home_url_quoted;
195
-
196
  //cleanup previous lang & edit parameter from url
197
  $url = preg_replace("/(" . LANG_PARAM . "|" . EDIT_PARAM . ")=[^&]*/i", "", $url);
198
-
199
-
200
  if(!$home_url)
201
  {
202
  //make sure required home urls are fetched - as they are need now
203
  init_global_vars();
204
  }
205
-
206
  //cleanup lang identifier in permalinks
207
  $url = preg_replace("/$home_url_quoted\/(..\/)/", "$home_url/", $url);
208
 
@@ -233,7 +235,7 @@ function transposh_widget_control()
233
  if ( $options != $newoptions )
234
  {
235
  $options = $newoptions;
236
- update_option('widget_transposh', $options);
237
  }
238
 
239
  $style = $options['style'];
@@ -244,16 +246,14 @@ function transposh_widget_control()
244
  'value="0">Language list</option>';
245
  echo '<option ' . ($style == 1 ? 'selected="selected"' : '') .
246
  'value="1">Flags</option>';
247
-
248
  echo '</select></label></p>
249
  <input type="hidden" name="transposh-submit" id="transposh-submit" value="1" />';
250
-
251
- }
252
 
 
253
 
254
  //Register callback for WordPress events
255
  add_action('init', 'init_transposh',0);
256
  add_action('widgets_init', 'transposh_widget_init');
257
 
258
-
259
  ?>
35
 
36
 
37
  global $wp_rewrite;
38
+
39
  $ref=getenv('HTTP_REFERER');
40
  $lang = $_POST[LANG_PARAM];
41
 
42
+ //remove existing language settings.
43
  $ref = cleanup_url($ref);
44
+
45
  if($lang != "none")
46
  {
47
  $use_params_only = !$wp_rewrite->using_permalinks();
99
  $is_translator = is_translator();
100
 
101
  $is_showing_languages = FALSE;
102
+
103
+ //echo $before_widget . $before_title . __(no_translate("Transposh")) . $after_title;
104
+ echo $before_widget . $before_title . __("Translation") . $after_title;
105
+
106
  switch ($options['style']) {
107
  case 1: // flags
108
  //keep the flags in the same direction regardless of the overall page direction
109
+ echo "<div style=\"text-align: left;\" class=\"" . NO_TRANSLATE_CLASS . "\" >";
110
 
111
  global $plugin_url;
112
  $using_permalinks = $wp_rewrite->using_permalinks();
113
+
114
  foreach($languages as $code => $lang2)
115
  {
116
  list($language,$flag) = explode (",",$lang2);
117
 
118
+ //remove any language identifier
119
  $page_url = cleanup_url($page_url);
120
+
121
  //Only show languages which are viewable or (editable and the user is a translator)
122
  if(strstr($viewable_langs, $code) ||
123
  ($is_translator && strstr($editable_langs, $code)))
124
  {
125
+ //$page_url = cleanup_url($page_url);
126
+ $page_url2 = rewrite_url_lang_param($page_url, $code, $is_edit, !$using_permalinks);
127
+
128
+ echo "<a href=\"" . $page_url2 . "\">".
129
+ "<img src=\"$plugin_url/flags/$flag.png\" title=\"$language\" alt=\"$language\"".
130
+ " style=\"padding: 1px 3px\"/></a>";
131
  $is_showing_languages = TRUE;
132
  }
133
  }
134
  echo "</div>";
135
+
136
  // this is the form for the edit...
137
  echo "<form action=\"$page_url\" method=\"post\">";
138
  echo "<input type=\"hidden\" name=\"lang\" id=\"lang\" value=\"$lang\"/>";
140
  default: // language list
141
 
142
  echo "<form action=\"$page_url\" method=\"post\">";
143
+ echo "<span class=\"" .NO_TRANSLATE_CLASS . "\" >";
144
  echo "<select name=\"lang\" id=\"lang\" onchange=\"Javascript:this.form.submit();\">";
145
  echo "<option value=\"none\">[Language]</option>";
146
 
172
  ($is_edit ? "checked=\"1\"" : "0") .
173
  "\" onClick=\"this.form.submit();\"/>&nbsp;Edit Translation";
174
  }
175
+
176
  echo "<input type=\"hidden\" name=\"transposh_widget_posted\" value=\"1\"/>";
177
  }
178
  else
182
  }
183
 
184
  echo "</form>";
185
+ //echo "<button onClick=\"do_auto_translate();\">translate all</button>";
186
+ echo "<div id=\"credit\">by <a href=\"http://transposh.org\"><img src=\"$plugin_url/tplogo.png\" title=\"Transposh\" alt=\"Transposh\"/></a></div>";
187
  echo $after_widget;
188
  }
189
 
 
190
  /*
191
+ * Remove from url any language (or editing) params that were added for our use.
192
+ * Return the scrubed url
193
  */
194
  function cleanup_url($url)
195
  {
196
  global $home_url, $home_url_quoted;
197
+
198
  //cleanup previous lang & edit parameter from url
199
  $url = preg_replace("/(" . LANG_PARAM . "|" . EDIT_PARAM . ")=[^&]*/i", "", $url);
200
+
201
+
202
  if(!$home_url)
203
  {
204
  //make sure required home urls are fetched - as they are need now
205
  init_global_vars();
206
  }
207
+
208
  //cleanup lang identifier in permalinks
209
  $url = preg_replace("/$home_url_quoted\/(..\/)/", "$home_url/", $url);
210
 
235
  if ( $options != $newoptions )
236
  {
237
  $options = $newoptions;
238
+ update_option(WIDGET_TRANSPOSH, $options);
239
  }
240
 
241
  $style = $options['style'];
246
  'value="0">Language list</option>';
247
  echo '<option ' . ($style == 1 ? 'selected="selected"' : '') .
248
  'value="1">Flags</option>';
249
+
250
  echo '</select></label></p>
251
  <input type="hidden" name="transposh-submit" id="transposh-submit" value="1" />';
 
 
252
 
253
+ }
254
 
255
  //Register callback for WordPress events
256
  add_action('init', 'init_transposh',0);
257
  add_action('widgets_init', 'transposh_widget_init');
258
 
 
259
  ?>