M_I18N

The NextGEN Gallery M I18N class.

Defined (1)

The class is defined in the following location(s).

/products/photocrati_nextgen/modules/i18n/module.i18n.php  
  1. class M_I18N extends C_Base_Module 
  2. function define($id = 'pope-module',  
  3. $name = 'Pope Module',  
  4. $description = '',  
  5. $version = '',  
  6. $uri = '',  
  7. $author = '',  
  8. $author_uri = '',  
  9. $context = FALSE) 
  10. parent::define( 
  11. 'photocrati-i18n',  
  12. 'Internationalization',  
  13. "Adds I18N resources and methods",  
  14. '0.3',  
  15. 'https://www.imagely.com/languages/',  
  16. 'Imagely',  
  17. 'https://www.imagely.com' 
  18. ); 
  19.  
  20. function _register_adapters() 
  21. // Provides translating the name & description of images, albums, and galleries 
  22. $this->get_registry()->add_adapter('I_Image_Mapper', 'A_I18N_Image_Translation'); 
  23. $this->get_registry()->add_adapter('I_Album_Mapper', 'A_I18N_Album_Translation'); 
  24. $this->get_registry()->add_adapter('I_Gallery_Mapper', 'A_I18N_Gallery_Translation'); 
  25. $this->get_registry()->add_adapter('I_Displayed_Gallery', 'A_I18N_Displayed_Gallery_Translation'); 
  26.  
  27. // qTranslate requires we disable "Hide Untranslated Content" during routed app requests like 
  28. // photocrati-ajax, when uploading new images, or retrieving dynamically altered (watermarked) images 
  29. $this->get_registry()->add_adapter('I_Routing_App', 'A_I18N_Routing_App'); 
  30.  
  31. function _register_hooks() 
  32. add_action('init', array(&$this, 'register_translation_hooks'), 2); 
  33.  
  34. function register_translation_hooks() 
  35. $fs = C_Fs::get_instance(); 
  36. $dir = str_replace( 
  37. $fs->get_document_root('plugins'),  
  38. '',  
  39. $fs->get_abspath('lang', 'photocrati-i18n') 
  40. ); 
  41.  
  42. // Load text domain 
  43. load_plugin_textdomain('nggallery', false, $dir); 
  44.  
  45. // Hooks to register image, gallery, and album name & description with WPML 
  46. add_action('ngg_image_updated', array($this, 'register_image_strings')); 
  47. add_action('ngg_album_updated', array($this, 'register_album_strings')); 
  48. add_action('ngg_created_new_gallery', array($this, 'register_gallery_strings')); 
  49.  
  50. // do not let WPML translate posts we use as a document store 
  51. add_filter('get_translatable_documents', array($this, 'wpml_translatable_documents')); 
  52.  
  53. if (class_exists('SitePress')) 
  54. // Copy AttachToPost entries when duplicating posts to another language 
  55. add_action('icl_make_duplicate', array($this, 'wpml_adjust_gallery_id'), 10, 4); 
  56. add_action('save_post', array($this, 'wpml_set_gallery_language_on_save_post'), 101, 1); 
  57.  
  58. // see function comments 
  59. add_filter('ngg_displayed_gallery_cache_params', array($this, 'set_qtranslate_cache_parameters')); 
  60. add_filter('ngg_displayed_gallery_cache_params', array($this, 'set_wpml_cache_parameters')); 
  61.  
  62. /** 
  63. * When QTranslate is active we must add its language & url-mode settings as display parameters 
  64. * so as to generate a unique cache for each language. 
  65. * @param array $arr 
  66. * @return array 
  67. */ 
  68. function set_qtranslate_cache_parameters($arr) 
  69. if (empty($GLOBALS['q_config']) || !defined('QTRANS_INIT')) 
  70. return $arr; 
  71.  
  72. global $q_config; 
  73. $arr['qtranslate_language'] = $q_config['language']; 
  74. $arr['qtranslate_url_mode'] = $q_config['url_mode']; 
  75.  
  76. return $arr; 
  77.  
  78. /** 
  79. * See notes on set_qtranslate_cache_paramters() 
  80. * @param array $arr 
  81. * @return array 
  82. */ 
  83. function set_wpml_cache_parameters($arr) 
  84. if (empty($GLOBALS['sitepress']) || !defined('WPML_ST_VERSION')) 
  85. return $arr; 
  86.  
  87. global $sitepress; 
  88. $settings = $sitepress->get_settings(); 
  89. $arr['wpml_language'] = ICL_LANGUAGE_CODE; 
  90. $arr['wpml_url_mode'] = $settings['language_negotiation_type']; 
  91.  
  92. return $arr; 
  93.  
  94. /** 
  95. * Registers gallery strings with WPML 
  96. * @param object $gallery 
  97. */ 
  98. function register_gallery_strings($gallery_id) 
  99. if (function_exists('icl_register_string')) 
  100. $gallery = C_Gallery_Mapper::get_instance()->find($gallery_id); 
  101. if ($gallery) 
  102. if (isset($gallery->title) && !empty($gallery->title)) 
  103. icl_register_string('plugin_ngg', 'gallery_' . $gallery->{$gallery->id_field} . '_name', $gallery->title, TRUE); 
  104. if (isset($gallery->galdesc) && !empty($gallery->galdesc)) 
  105. icl_register_string('plugin_ngg', 'gallery_' . $gallery->{$gallery->id_field} . '_description', $gallery->galdesc, TRUE); 
  106.  
  107. /** 
  108. * Registers image strings with WPML 
  109. * @param object $image 
  110. */ 
  111. function register_image_strings($image) 
  112. if (function_exists('icl_register_string')) 
  113. if (isset($image->description) && !empty($image->description)) 
  114. icl_register_string('plugin_ngg', 'pic_' . $image->{$image->id_field} . '_description', $image->description, TRUE); 
  115. if (isset($image->alttext) && !empty($image->alttext)) 
  116. icl_register_string('plugin_ngg', 'pic_' . $image->{$image->id_field} . '_alttext', $image->alttext, TRUE); 
  117.  
  118. /** 
  119. * Registers album strings with WPML 
  120. * @param object $album 
  121. */ 
  122. function register_album_strings($album) 
  123. if (function_exists('icl_register_string')) 
  124. if (isset($album->name) && !empty($album->name)) 
  125. icl_register_string('plugin_ngg', 'album_' . $album->{$album->id_field} . '_name', $album->name, TRUE); 
  126. if (isset($album->albumdesc) && !empty($album->albumdesc)) 
  127. icl_register_string('plugin_ngg', 'album_' . $album->{$album->id_field} . '_description', $album->albumdesc, TRUE); 
  128.  
  129. /** 
  130. * NextGEN stores some data in custom posts that MUST NOT be automatically translated by WPML 
  131. * @param array $icl_post_types 
  132. * @return array $icl_post_types without any NextGEN custom posts 
  133. */ 
  134. function wpml_translatable_documents($icl_post_types = array()) 
  135. $nextgen_post_types = array( 
  136. 'ngg_album',  
  137. 'ngg_gallery',  
  138. 'ngg_pictures',  
  139. 'display_type',  
  140. 'gal_display_source',  
  141. 'lightbox_library',  
  142. 'photocrati-comments' 
  143. ); 
  144. foreach ($icl_post_types as $ndx => $post_type) { 
  145. if (in_array($post_type->name, $nextgen_post_types)) 
  146. unset($icl_post_types[$ndx]); 
  147. return $icl_post_types; 
  148.  
  149. function wpml_adjust_gallery_id($master_post_id, $lang, $post_array, $id) 
  150. if (!isset($post_array['post_type']) || $post_array['post_type'] == "displayed_gallery") 
  151. return; 
  152.  
  153. $re = "|preview/id--(\d+)|mi"; 
  154. if (preg_match_all($re, $post_array['post_content'], $gallery_ids)) 
  155. foreach($gallery_ids[1] as $index => $gallery_id) { 
  156. $translated_gallery_id = apply_filters('wpml_object_id', (int)$gallery_id, "displayed_gallery", true, $lang); 
  157. $search[$index] = "preview/id--" . $gallery_id; 
  158. $replace[$index] = "preview/id--" . $translated_gallery_id; 
  159.  
  160. $post_array['post_content'] = str_replace($search, $replace, $post_array['post_content']); 
  161.  
  162. $to_save = array( 
  163. 'ID' => $id,  
  164. 'post_content' => $post_array['post_content'] 
  165. ); 
  166.  
  167. add_filter('ngg_cleanup_displayed_galleries', '__return_false', 10, 1); 
  168. wp_update_post($to_save); 
  169. add_filter('ngg_cleanup_displayed_galleries', '__return_true', 11, 1); 
  170.  
  171. function wpml_set_gallery_language_on_save_post($post_id) 
  172. if (wp_is_post_revision($post_id)) 
  173. return; 
  174.  
  175. if (isset($_POST['icl_ajx_action']) && $_POST['icl_ajx_action'] == 'make_duplicates') 
  176. return; 
  177.  
  178. $post = get_post($post_id); 
  179.  
  180. if ($post->post_type == 'displayed_gallery') 
  181. return; 
  182.  
  183. if (preg_match_all("#<img.*http(s)?://(.*)?" . NGG_ATTACH_TO_POST_SLUG . "(=|/)preview(/|&|&)id(=|--)(\\d+).*?>#mi", $post->post_content, $matches, PREG_SET_ORDER)) 
  184. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  185. foreach ($matches as $match) { 
  186. // Find the displayed gallery 
  187. $displayed_gallery_id = $match[6]; 
  188. add_filter('wpml_suppress_filters', '__return_true', 10, 1); 
  189. $displayed_gallery = $mapper->find($displayed_gallery_id, TRUE); 
  190. add_filter('wpml_suppress_filters', '__return_false', 11, 1); 
  191. if ($displayed_gallery) 
  192. $displayed_gallery_type = apply_filters('wpml_element_type', 'displayed_gallery'); 
  193.  
  194. // set language of this gallery 
  195. $displayed_gallery_lang = apply_filters('wpml_post_language_details', null, $displayed_gallery->ID); 
  196. $post_language = apply_filters('wpml_post_language_details', null, $post_id); 
  197.  
  198. if (!$displayed_gallery_lang || $displayed_gallery_lang['language_code'] != $post_language['language_code']) 
  199. if ($post_language) 
  200. $args = array( 
  201. 'element_id' => $displayed_gallery->ID,  
  202. 'element_type' => $displayed_gallery_type,  
  203. 'language_code' => $post_language['language_code'] 
  204. ); 
  205. do_action('wpml_set_element_language_details', $args); 
  206.  
  207. // duplicate gallery to other languages 
  208. $is_translated = apply_filters('wpml_element_has_translations', '', $displayed_gallery->ID, $displayed_gallery_type); 
  209. if (!$is_translated) 
  210. do_action('wpml_admin_make_post_duplicates', $displayed_gallery->ID); 
  211.  
  212. static function translate($in, $name = null) 
  213. if (function_exists('langswitch_filter_langs_with_message')) 
  214. $in = langswitch_filter_langs_with_message($in); 
  215.  
  216. if (function_exists('polyglot_filter')) 
  217. $in = polyglot_filter($in); 
  218.  
  219. if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) 
  220. $in = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($in); 
  221.  
  222. if (is_string($name) && !empty($name) && function_exists('icl_translate')) 
  223. $in = icl_translate('plugin_ngg', $name, $in, true); 
  224.  
  225. $in = apply_filters('localization', $in); 
  226.  
  227. return $in; 
  228.  
  229. static function mb_pathinfo($path, $options=null) 
  230. $ret = array( 
  231. 'dirname' => '',  
  232. 'basename' => '',  
  233. 'extension' => '',  
  234. 'filename' => '' 
  235. ); 
  236. $pathinfo = array(); 
  237. if (preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $pathinfo)) 
  238. if (array_key_exists(1, $pathinfo)) 
  239. $ret['dirname'] = $pathinfo[1]; 
  240. if (array_key_exists(2, $pathinfo)) 
  241. $ret['basename'] = $pathinfo[2]; 
  242. if (array_key_exists(5, $pathinfo)) 
  243. $ret['extension'] = $pathinfo[5]; 
  244. if (array_key_exists(3, $pathinfo)) 
  245. $ret['filename'] = $pathinfo[3]; 
  246. switch ($options) { 
  247. case PATHINFO_DIRNAME: 
  248. case 'dirname': 
  249. return $ret['dirname']; 
  250. case PATHINFO_BASENAME: 
  251. case 'basename': 
  252. return $ret['basename']; 
  253. case PATHINFO_EXTENSION: 
  254. case 'extension': 
  255. return $ret['extension']; 
  256. case PATHINFO_FILENAME: 
  257. case 'filename': 
  258. return $ret['filename']; 
  259. default: 
  260. return $ret; 
  261.  
  262. static function mb_basename($path) 
  263. $separator = " qq "; 
  264. $path = preg_replace("/[^ ]/u", $separator . "\$0" . $separator, $path); 
  265. $base = basename($path); 
  266. return str_replace($separator, "", $base); 
  267.  
  268. function get_type_list() 
  269. return array( 
  270. 'A_I18N_Displayed_Gallery_Translation' => 'adapter.i18n_displayed_gallery_translation.php',  
  271. 'A_I18N_Image_Translation' => 'adapter.i18n_image_translation.php',  
  272. 'A_I18N_Album_Translation' => 'adapter.i18n_album_translation.php',  
  273. 'A_I18N_Gallery_Translation' => 'adapter.i18n_gallery_translation.php',  
  274. 'A_I18N_Routing_App' => 'adapter.i18n_routing_app.php' 
  275. );