Last updated on October 18, 2023

WPML translation memory setup

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Utilize WPML translation memory for efficiency.

The WPML plugin is a powerful tool for creating multilingual websites in WordPress. One of its features is the Translation Memory, which allows you to reuse translations for similar or identical content across your site. This can save you time and effort when translating your website.

To set up the WPML Translation Memory, you need to follow these steps:

  1. Install and activate the WPML plugin on your WordPress site.
  2. Go to the WPML settings page by clicking on “WPML” in the WordPress admin menu.
  3. In the “Translation Management” tab, enable the “Translation Memory” option.
  4. Click on the “Translation Memory” link to access the Translation Memory settings.
  5. In the Translation Memory settings, you can configure various options such as the minimum number of words for a translation to be saved in the memory, the maximum number of translations to keep, and whether to include or exclude specific post types or taxonomies from the Translation Memory.
  6. Save your settings.

Once the Translation Memory is set up, WPML will automatically save translations in the memory when you translate content on your site. When you translate similar or identical content in the future, WPML will suggest using the existing translations from the Translation Memory, saving you time and effort.

Here’s an example of how to retrieve translations from the WPML Translation Memory using the wpsnippets_get_translation_from_memory() function:

function wpsnippets_get_translation_from_memory( $original_text, $language ) {
    if ( function_exists( 'wpml_tm_get_translation' ) ) {
        $translation = wpml_tm_get_translation( 'default', $original_text, $language );
        if ( $translation ) {
            return $translation['translated_text'];
        }
    }
    return $original_text;
}

This function checks if the WPML Translation Management plugin is active and retrieves the translation from the Translation Memory using the wpml_tm_get_translation() function. If a translation is found, it returns the translated text; otherwise, it returns the original text.

You can use this function in your theme or plugin to retrieve translations from the Translation Memory and display them on your site.

Examples

Example 1: Setting up WPML Translation Memory

This example demonstrates how to set up the WPML Translation Memory feature in WordPress. The code example shows how to enable the Translation Memory feature and configure its settings.

// Enable WPML Translation Memory
add_filter( 'wpml_tm_enabled', '__return_true' );

// Set Translation Memory options
add_filter( 'wpml_tm_options', 'wpsnippets_set_tm_options' );
function wpsnippets_set_tm_options( $options ) {
    $options['minimum_similarity'] = 80;
    $options['minimum_length'] = 5;
    $options['maximum_length'] = 100;
    return $options;
}

In this code example, we use the wpml_tm_enabled filter to enable the WPML Translation Memory feature. We then use the wpml_tm_options filter to set the Translation Memory options, such as the minimum similarity, minimum length, and maximum length of the translation units.

Example 2: Customizing WPML Translation Memory

This example demonstrates how to customize the WPML Translation Memory feature by adding custom filters to modify its behavior. The code example shows how to exclude specific post types from being included in the Translation Memory.

// Exclude specific post types from Translation Memory
add_filter( 'wpml_tm_translation_units_query_args', 'wpsnippets_exclude_post_types_from_tm' );
function wpsnippets_exclude_post_types_from_tm( $query_args ) {
    $query_args['post_type__not_in'] = array( 'product', 'event' );
    return $query_args;
}

In this code example, we use the wpml_tm_translation_units_query_args filter to modify the query arguments used to fetch translation units for the Translation Memory. We add the post_type__not_in parameter to exclude specific post types (e.g., ‘product’ and ‘event’) from being included in the Translation Memory.

Example 3: Customizing WPML Translation Memory UI

This example demonstrates how to customize the WPML Translation Memory user interface (UI) by adding custom JavaScript code. The code example shows how to add a custom button to the Translation Memory UI.

// Add custom button to Translation Memory UI
add_action( 'admin_footer', 'wpsnippets_add_custom_button_to_tm_ui' );
function wpsnippets_add_custom_button_to_tm_ui() {
    ?>
    <script>
        jQuery( document ).ready( function( $ ) {
            $( '#wpml-tm-ui .button-bar' ).append( '<button class="button">Custom Button</button>' );
        } );
    </script>
    <?php
}

In this code example, we use the admin_footer action hook to add custom JavaScript code to the admin footer. The JavaScript code appends a custom button to the Translation Memory UI by targeting the button bar element within the Translation Memory UI container.

Last updated on October 18, 2023. Originally posted on October 28, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *