WCML_WC_Strings

The WooCommerce Multilingual WCML WC Strings class.

Defined (1)

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

/inc/class-wcml-wc-strings.php  
  1. class WCML_WC_Strings{ 
  2.  
  3. private $translations_from_mo_file = array(); 
  4. private $mo_files = array(); 
  5. private $current_language; 
  6.  
  7. function __construct() { 
  8.  
  9. add_action( 'init', array( $this, 'init' ) ); 
  10. add_action( 'registered_taxonomy', array ( $this, 'translate_attributes_label_in_wp_taxonomies' ), 100, 3 ); 
  11.  
  12. function init() { 
  13. global $pagenow, $sitepress; 
  14.  
  15. $this->current_language = $sitepress->get_current_language(); 
  16. if( $this->current_language == 'all' ) { 
  17. $this->current_language = $sitepress->get_default_language(); 
  18.  
  19. //translate attribute label 
  20. add_filter('woocommerce_attribute_label', array($this, 'translated_attribute_label'), 10, 3); 
  21. add_filter('woocommerce_cart_item_name', array($this, 'translated_cart_item_name'), 10, 3); 
  22. add_filter('woocommerce_checkout_product_title', array($this, 'translated_checkout_product_title'), 10, 2); 
  23.  
  24. if( is_admin() && $pagenow == 'options-permalink.php' ) { 
  25. add_filter( 'gettext_with_context', array( $this, 'category_base_in_strings_language' ), 99, 3 ); 
  26. add_action( 'admin_footer', array( $this, 'show_custom_url_base_translation_links' ) ); 
  27. add_action('admin_footer', array($this, 'show_custom_url_base_language_requirement')); 
  28. add_action( 'woocommerce_product_options_attributes', array ( $this, 'notice_after_woocommerce_product_options_attributes' ) ); 
  29.  
  30. if( !is_admin() ) { 
  31. add_filter( 'woocommerce_attribute_taxonomies', array( $this, 'translate_attribute_taxonomies_labels') ); 
  32.  
  33. add_filter('woocommerce_get_breadcrumb', array($this, 'filter_woocommerce_breadcrumbs' ), 10, 2 ); 
  34.  
  35. function translated_attribute_label($label, $name, $product_obj = false) { 
  36. global $sitepress, $product, $sitepress_settings, $woocommerce_wpml; 
  37.  
  38. $product_id = false; 
  39. $lang = $sitepress->get_current_language(); 
  40.  
  41. if( isset( $_GET[ 'post' ] ) && get_post_type( $_GET[ 'post' ] ) == 'shop_order' ) { 
  42. $lang = $sitepress->get_user_admin_language( get_current_user_id(), true ); 
  43.  
  44. if( $product && is_object( $product ) ) { 
  45. $product_id = WooCommerce_Functions_Wrapper::get_product_id( $product ); 
  46. }elseif( is_numeric( $product_obj ) ) { 
  47. $product_id = $product_obj; 
  48. }elseif( $product_obj ) { 
  49. $product_id = WooCommerce_Functions_Wrapper::get_product_id( $product_obj ); 
  50.  
  51. $name = $woocommerce_wpml->attributes->filter_attribute_name( $name, $product_id, true ); 
  52.  
  53. if( $product_id ) { 
  54.  
  55. $custom_attr_translation = $woocommerce_wpml->attributes->get_attr_label_translations( $product_id, $lang ) ; 
  56.  
  57. if( $custom_attr_translation ) { 
  58. if( isset( $custom_attr_translation[$name] ) ) { 
  59. return $custom_attr_translation[$name]; 
  60.  
  61.  
  62. $trnsl_label = apply_filters( 'wpml_translate_single_string', $label, 'WordPress', 'taxonomy singular name: '.$label, $lang ); 
  63.  
  64. if( $label != $trnsl_label ) { 
  65. return $trnsl_label; 
  66.  
  67. if(is_admin() && !wpml_is_ajax()) { 
  68.  
  69. $string_language = $this->get_string_language( 'taxonomy singular name: '.$label, 'WordPress' ); 
  70.  
  71. if($sitepress->get_user_admin_language( get_current_user_id(), true ) != $string_language) { 
  72. $string_id = icl_get_string_id( 'taxonomy singular name: '.$label, 'WordPress' ); 
  73. $strings = icl_get_string_translations_by_id($string_id); 
  74. if($strings) { 
  75. return $strings[$sitepress->get_user_admin_language( get_current_user_id(), true )]['value']; 
  76. }else{ 
  77. return $label; 
  78.  
  79.  
  80. // backward compatibility for WCML < 3.6.1 
  81. $trnsl_labels = get_option('wcml_custom_attr_translations'); 
  82.  
  83. if( isset( $trnsl_labels[$lang][$name] ) && !empty( $trnsl_labels[$lang][$name] ) ) { 
  84. return $trnsl_labels[$lang][$name]; 
  85.  
  86. return $label; 
  87.  
  88. function translated_cart_item_name($title, $values, $cart_item_key) { 
  89.  
  90. if($values) { 
  91.  
  92. $parent = wp_get_post_parent_id( $values['product_id'] ); 
  93. $tr_product_id = apply_filters( 'translate_object_id', $values['product_id'], 'product', true ); 
  94. $trnsl_title = get_the_title($tr_product_id); 
  95.  
  96. if($parent) { 
  97. $tr_parent = apply_filters( 'translate_object_id', $parent, 'product', true ); 
  98. $trnsl_title = get_the_title( $tr_parent ) . ' → ' . $trnsl_title; 
  99.  
  100. if( strstr( $title, '</a>' ) ) { 
  101. $trnsl_title = sprintf( '<a href="%s">%s</a>', $values['data']->get_permalink(), $trnsl_title ); 
  102. }else{ 
  103. $trnsl_title = $trnsl_title. ' '; 
  104.  
  105. $title = $trnsl_title; 
  106.  
  107. return $title; 
  108.  
  109. function translated_checkout_product_title( $title, $product ) { 
  110.  
  111. if( $product ) { 
  112. $tr_product_id = apply_filters( 'translate_object_id', WooCommerce_Functions_Wrapper::get_product_id( $product ), 'product', true, $this->current_language ); 
  113. $title = get_the_title($tr_product_id); 
  114.  
  115. return $title; 
  116.  
  117. // Catch the default slugs for translation 
  118. function translate_default_slug($translation, $text, $context, $domain) { 
  119. global $sitepress, $woocommerce_wpml; 
  120.  
  121. if ($context == 'slug' || $context == 'default-slug') { 
  122. $wc_slug = $woocommerce_wpml->url_translation->get_woocommerce_product_base(); 
  123. if(is_admin()) { 
  124. $admin_language = $sitepress->get_admin_language(); 
  125. $current_language = $sitepress->get_current_language(); 
  126.  
  127. $strings_language = $this->get_domain_language('woocommerce'); 
  128.  
  129. if ($text == $wc_slug && $domain == 'woocommerce' && $strings_language) { 
  130. $sitepress->switch_lang($strings_language); 
  131. $translation = _x($text, 'URL slug', $domain); 
  132. $sitepress->switch_lang($current_language); 
  133. if(is_admin()) { 
  134. $sitepress->set_admin_language($admin_language); 
  135. }else{ 
  136. $translation = $text; 
  137.  
  138. if(!is_admin()) { 
  139. $sitepress->switch_lang($current_language); 
  140.  
  141. return $translation; 
  142.  
  143.  
  144.  
  145.  
  146. function show_custom_url_base_language_requirement() { 
  147. $category_base = ($c = get_option('category_base') ) ? $c : 'category'; 
  148. ?> 
  149. <script> 
  150. if(jQuery('#woocommerce_permalink_structure').length) { 
  151. jQuery('#woocommerce_permalink_structure').parent().append(jQuery('#wpml_wcml_custom_base_req').html()); 
  152. if(jQuery('input[name="woocommerce_product_category_slug"]').length && jQuery('input[name="woocommerce_product_category_slug"]').val() == '<?php echo $category_base ?>') { 
  153. jQuery('input[name="woocommerce_product_category_slug"]').parent().append('<br><i class="icon-warning-sign"><?php 
  154. _e('You are using the same value as for the regular category base. This is known to create conflicts resulting in urls not working properly.', 'woocommerce-multilingual') ?></i>'); 
  155. </script> 
  156. <?php 
  157.  
  158.  
  159. function show_custom_url_base_translation_links() { 
  160. global $woocommerce_wpml, $sitepress; 
  161. $permalink_options = get_option( 'woocommerce_permalinks' ); 
  162.  
  163. $lang_selector = new WPML_Simple_Language_Selector( $sitepress ); 
  164.  
  165. $bases = array( 'tag_base' => 'product_tag', 'category_base' => 'product_cat', 'attribute_base' => 'attribute', 'product_base' => 'product' ); 
  166.  
  167. foreach( $bases as $key => $base ) { 
  168.  
  169. switch($base) { 
  170. case 'product_tag': 
  171. $input_name = 'woocommerce_product_tag_slug'; 
  172. $value = !empty( $permalink_options['tag_base'] ) ? $permalink_options['tag_base'] : $woocommerce_wpml->url_translation->default_product_tag_base; 
  173. break; 
  174. case 'product_cat': 
  175. $input_name = 'woocommerce_product_category_slug'; 
  176. $value = !empty( $permalink_options['category_base'] ) ? $permalink_options['category_base'] : $woocommerce_wpml->url_translation->default_product_category_base; 
  177. break; 
  178. case 'attribute': 
  179. $input_name = 'woocommerce_product_attribute_slug'; 
  180. $value = !empty( $permalink_options['attribute_base'] ) ? $permalink_options['attribute_base'] : ''; 
  181. break; 
  182. case 'product': 
  183. $input_name = 'product_permalink_structure'; 
  184. if( empty( $permalink_options['product_base'] ) ) { 
  185. $value = _x( 'product', 'default-slug', 'woocommerce' ); 
  186. }else{ 
  187. $value = trim( $permalink_options['product_base'], '/' ); 
  188.  
  189. break; 
  190.  
  191. $language = $this->get_string_language( trim( $value, '/' ), $woocommerce_wpml->url_translation->url_strings_context(), $woocommerce_wpml->url_translation->url_string_name( $base ) ); 
  192.  
  193. if( is_null($language) ) { 
  194. $language = $sitepress->get_default_language(); 
  195.  
  196. echo $lang_selector->render( array( 'id' => $key.'_language_selector', 'name' => $key.'_language', 'selected' => $language, 'show_please_select' => false ) ); ?> 
  197.  
  198. <script> 
  199. var input = jQuery('input[name="<?php echo $input_name ?>"]'); 
  200.  
  201. if(input.length) { 
  202.  
  203. if( '<?php echo $input_name ?>' == 'product_permalink_structure' && jQuery('input[name="product_permalink"]:checked').val() == '' ) { 
  204. input = jQuery('input[name="product_permalink"]:checked').closest('.form-table').find('code').eq(0); 
  205.  
  206. input.parent().append('<div class="translation_controls"></div>'); 
  207.  
  208. if( '<?php echo $input_name ?>' == 'woocommerce_product_attribute_slug' && input.val() == '' ) { 
  209.  
  210. input.parent().find('.translation_controls').append(' '); 
  211.  
  212. }else{ 
  213. input.parent().find('.translation_controls').append('<a href="<?php 
  214. echo admin_url( 'admin.php?page=wpml-wcml&tab=slugs' ) 
  215. ?>"><?php _e('translations', 'woocommerce-multilingual') ?></a>'); 
  216.  
  217. jQuery('#<?php echo $key ?>_language_selector').prependTo( input.parent().find('.translation_controls') ); 
  218. </script> 
  219. <?php } 
  220.  
  221.  
  222. function category_base_in_strings_language($text, $original_value, $context) { 
  223. if($context == 'slug' && ($original_value == 'product-category' || $original_value == 'product-tag')) { 
  224. $text = $original_value; 
  225. return $text; 
  226.  
  227. function product_permalink_slug() { 
  228. $permalinks = get_option( 'woocommerce_permalinks' ); 
  229. $slug = empty( $permalinks['product_base'] ) ? 'product' : trim($permalinks['product_base'], '/'); 
  230.  
  231. return $slug; 
  232.  
  233. function get_domain_language( $domain ) { 
  234. global $sitepress; 
  235.  
  236. $lang_of_domain = new WPML_Language_Of_Domain( $sitepress ); 
  237. $domain_lang = $lang_of_domain->get_language( $domain ); 
  238.  
  239. if ( $domain_lang ) { 
  240. $source_lang = $domain_lang; 
  241. }else{ 
  242. $source_lang = 'en'; 
  243.  
  244. return $source_lang; 
  245.  
  246. function get_string_language( $value, $context, $name = false ) { 
  247. global $wpdb; 
  248.  
  249. if( $name !== false ) { 
  250.  
  251. $string_language = apply_filters( 'wpml_get_string_language', null, $context, $name ); 
  252.  
  253. }else{ 
  254.  
  255. $string_id = icl_get_string_id( $value, $context, $name ); 
  256.  
  257. if( !$string_id ) { 
  258. return 'en'; 
  259.  
  260. $string_object = new WPML_ST_String($string_id, $wpdb); 
  261. $string_language = $string_object->get_language(); 
  262.  
  263.  
  264. if( !$string_language ) { 
  265. return 'en'; 
  266.  
  267. return $string_language; 
  268.  
  269.  
  270. function set_string_language( $value, $context, $name , $language ) { 
  271. global $wpdb; 
  272.  
  273. $string_id = icl_get_string_id( $value, $context, $name ); 
  274.  
  275. $string_object = new WPML_ST_String( $string_id, $wpdb ); 
  276. $string_language = $string_object->set_language( $language ); 
  277.  
  278. return $string_language; 
  279.  
  280.  
  281. /** 
  282. * Filter breadcrumbs 
  283. */ 
  284. function filter_woocommerce_breadcrumbs( $breadcrumbs, $object ) { 
  285. global $sitepress, $woocommerce_wpml; 
  286.  
  287. $current_language = $sitepress->get_current_language(); 
  288. $default_language = $sitepress->get_default_language(); 
  289.  
  290. if( $current_language != $default_language || $default_language != 'en' ) { 
  291.  
  292. $shop_page = get_post( wc_get_page_id( 'shop' ) ); 
  293.  
  294. // If permalinks contain the shop page in the URI prepend the breadcrumb with shop 
  295. // Similar to WC_Breadcrumb::prepend_shop_page 
  296. $trnsl_base = $woocommerce_wpml->url_translation->get_base_translation( 'product', $current_language ); 
  297. if( $trnsl_base['translated_base'] === '' ) { 
  298. $trnsl_base['translated_base'] = $trnsl_base['original_value']; 
  299.  
  300. if ( $shop_page->ID && strstr( $trnsl_base['translated_base'], urldecode( $shop_page->post_name ) ) && get_option( 'page_on_front' ) != $shop_page->ID ) { 
  301. $breadcrumbs_buff = array(); 
  302. $i = 0; 
  303. foreach( $breadcrumbs as $key => $breadcrumb ) { 
  304.  
  305. if( !in_array( $breadcrumb, $breadcrumbs_buff ) ) { 
  306. $breadcrumbs_buff[ $i ] = $breadcrumb; 
  307.  
  308. if( $key === 0 && $breadcrumbs[ 1 ][ 1 ] != get_post_type_archive_link( 'product' ) ) { 
  309. $i++; 
  310. $breadcrumbs_buff[ $i ] = array( $shop_page->post_title, get_post_type_archive_link( 'product' ) ); 
  311. $i++; 
  312. $breadcrumbs = $breadcrumbs_buff; 
  313.  
  314. $breadcrumbs = array_values($breadcrumbs); 
  315.  
  316.  
  317. return $breadcrumbs; 
  318.  
  319. /** 
  320. * Add notice message to users 
  321. */ 
  322. function notice_after_woocommerce_product_options_attributes() { 
  323. global $sitepress; 
  324.  
  325. if( isset( $_GET['post'] ) && $sitepress->get_default_language() != $sitepress->get_current_language() ) { 
  326. $original_product_id = apply_filters( 'translate_object_id', $_GET['post'], 'product', true, $sitepress->get_default_language() ); 
  327.  
  328. printf( '<p>'.__('In order to edit custom attributes you need to use the <a href="%s">custom product translation editor</a>', 'woocommerce-multilingual').'</p>', admin_url('admin.php?page=wpml-wcml&tab=products&prid='.$original_product_id ) ); 
  329.  
  330. function translate_attribute_taxonomies_labels( $attribute_taxonomies ) { 
  331. global $sitepress; 
  332.  
  333. if( is_admin() && !wpml_is_ajax() ) { 
  334.  
  335. foreach( $attribute_taxonomies as $key => $attribute_taxonomy ) { 
  336. $string_language = $this->get_string_language( $attribute_taxonomy->attribute_name, 'WordPress', 'taxonomy singular name: '.$attribute_taxonomy->attribute_name ); 
  337.  
  338. if( $sitepress->get_current_language() == $string_language ) continue; 
  339.  
  340. $string_id = icl_get_string_id( $attribute_taxonomy->attribute_name, 'WordPress', 'taxonomy singular name: '.$attribute_taxonomy->attribute_name ); 
  341. $strings = icl_get_string_translations_by_id( $string_id ); 
  342.  
  343. if($strings && isset($strings[$sitepress->get_current_language()])) { 
  344. $attribute_taxonomies[$key]->attribute_label = $strings[$sitepress->get_current_language()]['value']; 
  345.  
  346.  
  347.  
  348. return $attribute_taxonomies; 
  349.  
  350. function get_translation_from_woocommerce_mo_file( $string, $language, $return_original = true ) { 
  351. global $sitepress; 
  352.  
  353. $original_string = $string; 
  354.  
  355. if ( ! isset( $this->translations_from_mo_file[ $original_string ][ $language ] ) ) { 
  356.  
  357. if ( ! isset( $this->translations_from_mo_file[ $original_string ] ) ) { 
  358. $this->translations_from_mo_file[ $original_string ] = array(); 
  359.  
  360. if ( ! isset( $this->mo_files[ $language ] ) ) { 
  361. $mo = new MO(); 
  362. $mo_file = WP_LANG_DIR . '/plugins/woocommerce-' . $sitepress->get_locale( $language ) . '.mo'; 
  363. if( !file_exists( $mo_file ) ) { 
  364. return $return_original ? $string : null; 
  365.  
  366. $mo->import_from_file( $mo_file ); 
  367. $this->mo_files[ $language ] = &$mo->entries; 
  368.  
  369. if( in_array( $string, array( 'product', 'product-category', 'product-tag' ) ) ) { 
  370. $string = 'slug'. chr(4) .$string; 
  371.  
  372. if( isset( $this->mo_files[ $language ][$string] ) ) { 
  373. $this->translations_from_mo_file[ $original_string ][ $language ] = $this->mo_files[ $language ][$string]->translations[0]; 
  374. } else { 
  375. $this->translations_from_mo_file[ $original_string ][ $language ] = $return_original ? $original_string : null; 
  376.  
  377. return $this->translations_from_mo_file[ $original_string ][ $language ]; 
  378.  
  379.  
  380. function translate_attributes_label_in_wp_taxonomies( $taxonomy, $obj_type, $args ) { 
  381. global $wp_taxonomies, $sitepress; 
  382. $obj_type = array_unique( (array) $obj_type ); 
  383.  
  384. $current_language = $sitepress->get_current_language(); 
  385.  
  386. if( $current_language != 'all' && in_array( 'product', $obj_type ) && substr( $taxonomy, 0, 3) == 'pa_' && isset( $wp_taxonomies[ $taxonomy ] )) { 
  387.  
  388. if( is_array( $args['labels'] ) ) { 
  389. $name = $args['labels']['singular_name']; 
  390. }else{ 
  391. $name = $args['labels']->name; 
  392.  
  393. $wp_taxonomies[$taxonomy]->labels->name = apply_filters( 'wpml_translate_single_string', $name, 'WordPress', 'taxonomy singular name: '.$name, $current_language ); 
  394.  
  395.  
  396.  
  397.