root/drupal/modules/simple_translation/trunk/simple_translation.module

Revision 11, 6.4 kB (checked in by blundeln, 2 years ago)

Importing simple translation module.

  • Property svn:executable set to
Line 
1 <?
2
3 /*
4  * ===============================
5  * DEBUG STUFF
6  * ===============================
7  */
8
9 if (!function_exists("d")) {
10   function d($message, $item=null, $print=false) {
11     if ($item) {
12       $message .= ": " . print_r($item,true);
13     }
14     if ($print) {
15       print("debug: <pre>$message</pre>");
16     } else {
17       drupal_set_message("debug: <pre>$message</pre>");
18     }
19   }
20 }
21
22 /*
23  * =======================================
24  * HOOKS
25  * =======================================
26  */
27
28
29
30 function simple_translation_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
31
32
33   global $language;
34   global $no_recurse_nid;
35   if ($op != "load" or $no_recurse_nid == $node->nid) {
36     $no_recurse_nid = Null;
37     return;
38   }
39  
40   // Load the translation source node.
41   $sNode = safe_node_load($node->tnid);
42
43   if (!$sNode) {
44     return;
45   }
46
47   // Get the translated node.
48   $tNode = _load_translated_node($sNode, $language);
49  
50   if (!$tNode) {
51     //d("No translation for $sNode->title");
52     return;
53   }
54   //d("translated to $tNode->title");
55
56   // Replace the loaded node with the translated node.
57   //$node = $tNode;
58   $node->title = $tNode->title;
59   $node->body = $tNode->body;
60   $node->teaser = $tNode->teaser;
61   //d("node",$node);
62 }
63
64
65
66 // Translate menu links.
67 // Note, this is a patched hook.
68 function simple_translation_menu_tree_item_alter(&$item) {
69   global $language;
70   $title = $item["link_title"];
71
72   // If HTML, do nothing
73   if (stristr($title, "<") and stristr($title, ">")) {
74     return;
75   }
76
77   // If this points to a node...
78   sscanf($item["link_path"], "node/%d", $nid);
79   if ($nid) {
80     // Replace the menu title with the translated title.
81     // Remember, we hook node_load in this module to translate it.
82     $node = node_load($nid);
83     $item["link_title"] = $node->title;
84     return;
85   }
86  
87   // As a fallback, allow the user to translate other menu titles (e.g. links to other sites, etc.).
88   $item["link_title"] = t($item["link_title"]);
89 }
90
91
92 /* Force node listings to only contain translation source nodes, since we translate these when they are viewed. */
93 function simple_translation_db_rewrite_sql($query, $primary_table, $primary_key, $args = array()){
94   // Adapted from i18n module
95  
96   $query_mods = array();
97
98   switch ($primary_table) {
99     case 'n':
100     case 'node':
101       // No rewrite for queries with subselect ? (views count queries)
102       // @ TO DO Actually these queries look un-rewrittable, check with other developers
103       if (preg_match("/FROM \(SELECT/", $query)) return;
104       // No rewrite for translation module queries
105       if (preg_match("/.*FROM {node} $primary_table WHERE.*$primary_table\.tnid/", $query)) return;
106       // When loading specific nodes, language conditions shouldn't apply
107       if (preg_match("/WHERE.*\s$primary_table.nid\s*=\s*(\d|%d)/", $query)) return;
108       // If language conditions already there, get out
109       if (preg_match("/i18n/", $query)) return;
110      
111       //d("q",array($primary_table, $primary_key, $query));
112
113       // Show only translation sources from translation sets.
114       $query_mods["where"] = "$primary_table.nid = $primary_table.tnid OR $primary_table.language = ''";
115   }
116  
117   return $query_mods;
118 }
119
120
121 function simple_translation_views_pre_render(&$view) {
122   //d("result",$view->result);
123   foreach ($view->result as $item) {
124     if (!$item->nid) { continue; }
125
126     // Remember, our module causes the translated node to be loaded when node_load is called.
127     $node = node_load($item->nid);
128    
129     //assert($node->nid == $node->tnid);
130     //$tNode = _load_translated_node($node, $language);
131     //if (!$tNode) { continue; }
132
133     // Update translatable fields.
134     $item->node_title = $node->title;
135     $item->node_revisions_teaser = node_teaser($node->body);
136     $item->node_revisions_body = $node->body;
137   }
138 }
139
140
141 /* Initialise the module. */
142 function simple_translation_init() {
143  
144   // Need to clear the menu cache to allow menu translation.
145   // TODO: Need to find a better way.
146   menu_cache_clear_all();
147  
148   //drupal_rebuild_theme_registry();
149 }
150
151
152 /*
153  * =======================================
154  * UTILITY FUNCTIONS
155  * =======================================
156  */
157
158 /* Loads a node in such a way that it will not be processed recursively by our nodeapi hook. */
159 // Used to stop us recursing when hooking node_load.
160 $no_recurse_nid = Null;
161 function safe_node_load($nid) {
162   global $no_recurse_nid;
163   $no_recurse_nid = $nid;
164   return node_load($nid);
165 }
166
167
168 /* Loads the translated version of a node or returns the original node if no translation. */
169 function _load_translated_node(&$node, $language) {
170  
171   // Return original if no trans source, non-lang specific node, or the node language is same as user language.
172   if (!($node->tnid and $language->language) or $language->language == $node->language) {
173     return $node;
174   }
175
176   // Try to get the translated version.
177   $tNode = Null;
178   foreach (translation_node_get_translations($node->tnid) as $lang => $translation_node) {
179     if ($lang == $language->language) {
180       $tNode = safe_node_load($translation_node->nid);
181       break;
182     }
183   }
184
185   return $tNode;
186 }
187
188
189 /*
190  * =======================================
191  * OLD STUFF
192  * =======================================
193  */
194
195 function xxxsimple_translation_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
196   // TODO: When incomming node is a translated node, comments of trans source are not dispayed.
197
198   global $language;
199
200   if ($op != "alter") {
201     return;
202   }
203
204   // State what the arguments are.
205   $teaser = $a3;
206   $page = $a4;
207
208   // Get the translation source node of this node
209   $sNode = node_load($node->tnid);
210
211   if (!$sNode) {
212     return;
213   }
214
215   // Get the translated node.
216   $tNode = _load_translated_node($sNode, $language);
217  
218   if (!$tNode) {
219     return;
220   }
221
222   // Sadly need to re-build the node's content, since drupal_render cannot be called more than once on content.
223   $sNode = node_build_content($sNode, $teaser, $page);
224
225   // Render the node and update the originally rendered node.
226   $tNode = node_build_content($tNode, $teaser, $page);
227  
228   // Render the content in the correct language.
229   $sNode->content["body"] = $tNode->content["body"];
230   $renderedContent = drupal_render($sNode->content);
231   $sNode->teaser = $renderedContent;
232   $sNode->body = $renderedContent;
233  
234   // Update the page title.
235   $sNode->title = $tNode->title;
236   drupal_set_title(check_plain($sNode->title));
237
238   // Update the links
239   $sNode->links = module_invoke_all('link', 'node', $tNode, $teaser);
240   drupal_alter('link', $sNode->links, $tNode);
241
242   // Replace $node with the modified $sNode
243   $node = $sNode;
244 }
245
246
247
248
249 ?>
Note: See TracBrowser for help on using the browser.