1 | /* ----------------------------------------------------------------------------- |
---|
2 | file: google_translate.js |
---|
3 | file version: 2.0.0 |
---|
4 | date: 2008-05-25 |
---|
5 | ------------------------------------------------------------------------------ |
---|
6 | author: grum at grum.dnsalias.com |
---|
7 | << May the Little SpaceFrog be with you >> |
---|
8 | ------------------------------------------------------------------------------ |
---|
9 | |
---|
10 | this classes provides base functions to use Google Translate AJAX API |
---|
11 | >> http://code.google.com/apis/ajaxlanguage/ |
---|
12 | |
---|
13 | ------------------------------------------------------------------------------ |
---|
14 | HISTORY VERSION |
---|
15 | v2.0.0 + adapted for piwigo |
---|
16 | + add of a 5th&6th parameters for the google_translate function |
---|
17 | |
---|
18 | -------------------------------------------------------------------------- */ |
---|
19 | |
---|
20 | google.load("language", "1"); |
---|
21 | |
---|
22 | |
---|
23 | var global_google_translate_plugin_objdest; |
---|
24 | var global_google_translate_plugin_objproperty; |
---|
25 | var global_google_translate_plugin_objcallback; |
---|
26 | var global_google_translate_plugin_objcallback_param; |
---|
27 | |
---|
28 | |
---|
29 | function google_translate(text, pfrom, pto, objdest, objproperty) |
---|
30 | { |
---|
31 | /* |
---|
32 | ** args needed ** |
---|
33 | 1st arg : text to translate |
---|
34 | 2nd arg : translate from lang ("en", "fr", "es", ...) |
---|
35 | 3rd arg : translate to lang ("en", "fr", "es", ...) |
---|
36 | 4th arg : target of result (id) |
---|
37 | 5th arg : affected propertie ('value' or 'innerHTML') |
---|
38 | ** facultative args ** |
---|
39 | 6th arg : pointer on a function definition (callback is made when |
---|
40 | translation is done ; notice that translation is made asynchronous) |
---|
41 | 7th arg : arg for the callback (or array of arg if callbakc need more than |
---|
42 | one parameter) |
---|
43 | */ |
---|
44 | if(arguments.length>=6) |
---|
45 | { |
---|
46 | global_google_translate_plugin_objcallback=arguments[5]; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | global_google_translate_plugin_objcallback=null; |
---|
51 | } |
---|
52 | |
---|
53 | if(arguments.length>=7) |
---|
54 | { |
---|
55 | if(arguments[6].pop) |
---|
56 | { |
---|
57 | global_google_translate_plugin_objcallback_param=arguments[6]; |
---|
58 | } |
---|
59 | else |
---|
60 | { |
---|
61 | global_google_translate_plugin_objcallback_param=new Array(arguments[6]); |
---|
62 | } |
---|
63 | } |
---|
64 | else |
---|
65 | { |
---|
66 | global_google_translate_plugin_objcallback_param=null; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | global_google_translate_plugin_objdest = objdest; |
---|
71 | global_google_translate_plugin_objproperty = objproperty; |
---|
72 | google.language.translate(text, pfrom, pto, google_translate_do); |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | function google_translate_do(result) |
---|
77 | { |
---|
78 | if (!result.error) |
---|
79 | { |
---|
80 | if(global_google_translate_plugin_objproperty=='value') |
---|
81 | { |
---|
82 | global_google_translate_plugin_objdest.value = result.translation; |
---|
83 | } |
---|
84 | else if(global_google_translate_plugin_objproperty=='innerHTML') |
---|
85 | { |
---|
86 | global_google_translate_plugin_objdest.innerHTML = result.translation; |
---|
87 | } |
---|
88 | if(global_google_translate_plugin_objcallback!=null) |
---|
89 | { |
---|
90 | if(global_google_translate_plugin_objcallback_param!=null) |
---|
91 | { |
---|
92 | global_google_translate_plugin_objcallback.apply(null, global_google_translate_plugin_objcallback_param); |
---|
93 | } |
---|
94 | else |
---|
95 | { |
---|
96 | global_google_translate_plugin_objcallback(); |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|