Plugin Directory

Changeset 3028672


Ignore:
Timestamp:
01/30/2024 10:24:45 AM (2 years ago)
Author:
BinaryMoon
Message:
  • Tweak replacements to only replace whole words and not the middle of words.
Location:
translate-words/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • translate-words/trunk/includes/frontend.php

    r2755276 r3028672  
    2323    $replace = array_column( $overrides, 'overwrite' );
    2424
     25    if ( empty( $keys ) || empty( $replace ) ) {
     26        return $translated_string;
     27    }
     28
     29    /**
     30     * We perform two replacements here: first case-sensitive, then case-insensitive.
     31     *
     32     * The reason for this two-step process is to handle scenarios where we have
     33     * translations that are identical except for their case. By doing a case-sensitive
     34     * replacement first, we ensure that these translations are correctly replaced
     35     * with their exact matches.
     36     *
     37     * After the case-sensitive replacement, we perform a case-insensitive replacement
     38     * to catch any remaining translations that were not matched in the first step due
     39     * to case differences.
     40     *
     41     * This process ensures that we prioritize exact matches while still providing
     42     * a fallback for any remaining matches, regardless of case.
     43     *
     44     * It's important to maintain this two-step process to handle all possible
     45     * translation scenarios correctly. Therefore, do not remove or alter this
     46     * replacement strategy without a thorough understanding of its implications.
     47     */
     48
    2549    /**
    2650     * Do a case sensitive replacement.
     
    2852     * but with different cases.
    2953     */
    30     $translated_string = str_replace( $keys, $replace, $translated_string );
     54    $search_keys = array_map(
     55        function( $key ) {
     56            return '/\b' . preg_quote( $key, '/' ) . '\b/';
     57        },
     58        $keys
     59    );
     60
     61    $translated_string = preg_replace( $search_keys, $replace, $translated_string );
    3162
    3263    /**
     
    3566     * backwards compatability.
    3667     */
    37     $translated_string = str_ireplace( $keys, $replace, $translated_string );
     68    $search_keys = array_map(
     69        function( $key ) {
     70            return '/\b' . preg_quote( $key, '/' ) . '\b/i';
     71        },
     72        $keys
     73    );
     74
     75    $translated_string = preg_replace( $search_keys, $replace, $translated_string );
    3876
    3977    return $translated_string;
  • translate-words/trunk/readme.txt

    r2874825 r3028672  
    33Tags: gettext, ngettext, string translations, translate
    44Requires at least: 4.9.0
    5 Tested up to: 6.2.0
     5Tested up to: 6.5.0
    66Stable tag: trunk
    77Requires PHP: 7.0
     
    4444== Changelog ==
    4545
     46= 1.2.5 - 30th January 2024 =
     47* Tweak replacements to only replace whole words and not the middle of words.
     48
    4649= 1.2.4 - 4th March 2023 =
    4750* Fix PHP 8.2 error.
  • translate-words/trunk/translate-wp-words.php

    r2874822 r3028672  
    33 * Plugin Name: Translate Words
    44 * Description: Thanks to this plugin you can translate all the strings of your portal through the admin panel.
    5  * Version: 1.2.4
     5 * Version: 1.2.5
    66 * Author: Ben Gillbanks
    77 * Author URI: https://www.binarymoon.co.uk/
Note: See TracChangeset for help on using the changeset viewer.