Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/modules/woo-commerce/front.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,28 @@
exit;
}

/**
* Translate product terms after WooCommerce retrieves them from cache.
*
* WooCommerce product-term cache keys do not include the current language,
* so a persistent object cache may return terms translated during an
* earlier request in another language.
*
* @param array $terms Product terms.
*
* @return array
*/
function qtranxf_wc_translate_product_terms( $terms ) {
return qtranxf_useTermLib( $terms );
}

function qtranxf_wc_add_filters_front(): void {

remove_filter( 'get_post_metadata', 'qtranxf_filter_postmeta', 5 );
add_filter( 'get_post_metadata', 'qtranxf_wc_filter_postmeta', 5, 4 );

add_filter( 'woocommerce_get_product_terms', 'qtranxf_wc_translate_product_terms', 20, 1 );

$front_hooks = array(
'woocommerce_attribute' => 20,
'woocommerce_attribute_label' => 20,
Expand Down
10 changes: 10 additions & 0 deletions src/modules/woo-commerce/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
}

function qtranxf_wc_init_language( array $url_info ): void {
/*
* WooCommerce caches the filtered result of
* `woocommerce_attribute_taxonomies`. Since qTranslate-XT translates
* this result, a persistent object cache would store the language of
* the first request for all subsequent requests.
*/
if ( function_exists( 'wp_cache_add_non_persistent_groups' ) ) {
wp_cache_add_non_persistent_groups( array( 'woocommerce-attributes' ) );
}

if ( $url_info['doing_front_end'] ) {
require_once __DIR__ . '/front.php';
} else {
Expand Down
20 changes: 12 additions & 8 deletions src/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,28 @@ function qtranxf_term_set_i18n_config( $term ) {
*/
function qtranxf_term_use( string $lang, $obj, $taxonomy ) {
global $q_config;

if ( is_array( $obj ) ) {
// handle arrays recursively
// Handle arrays recursively.
foreach ( $obj as $key => $term ) {
$obj[ $key ] = qtranxf_term_use( $lang, $term, $taxonomy );
}

return $obj;
}

if ( is_object( $obj ) ) {
// object conversion
if ( ! isset( $obj->i18n_config ) ) {
qtranxf_term_set_i18n_config( $obj );
if ( isset( $obj->i18n_config['name']['ts'][ $lang ] ) ) {
$obj->name = $obj->i18n_config['name']['ts'][ $lang ];
}
if ( isset( $obj->i18n_config['description']['ts'][ $lang ] ) ) {
$obj->description = $obj->i18n_config['description']['ts'][ $lang ];
}
}

if ( isset( $obj->i18n_config['name']['ts'][ $lang ] ) ) {
$obj->name = $obj->i18n_config['name']['ts'][ $lang ];
}

if ( isset( $obj->i18n_config['description']['ts'][ $lang ] ) ) {
$obj->description =
$obj->i18n_config['description']['ts'][ $lang ];
}
} elseif ( isset( $q_config['term_name'][ $obj ][ $lang ] ) ) {
$obj = $q_config['term_name'][ $obj ][ $lang ];
Expand Down