Publisher does not support the Fluid field type. Please do not contact asking when support will be available.

If you purchased an add-on from expressionengine.com, be sure to visit boldminded.com/claim to add the license to your account here on boldminded.com.

Ticket: Alternate functionality for translate_phrase

Status Resolved
Add-on / Version Publisher latest
Severity
EE Version 7.x

BoldMinded (Brian)

Apr 22, 2024

Description of the problem

Allow the user to enter a phrase with replacements like page %s1 of %s2 to define the order of parameters that get replaced, in case some languages switch the order around like of %s2, page %s1. That way it can still be used in the template like {exp:publisher:translate_phrase name="page_text" replace="{current_page}|{total_pages}”}

/**
 * @param $phrase
 * @param $replace
 * @return string
 */
private function phraseStringReplace($phrase, $replace)
{
    // Pad the replacement array with extra empty string values to avoid ever passing too few arguments to vsprintf
    $replacements = explode('|', $replace) + array_fill(0, self::MAX_PHRASE_PLACEHOLDER_VARIABLES, '');

    // alternate phrase syntax using %s1, %s2, etc to define the order of the parameters
    if (stripos($phrase, '%s1') !== false) {
        $orderedReplace = [];
        foreach ($replacements as $i => $r) {
            $orderedReplace['%s' . ($i+1)] = $r;
        }

        // https://www.php.net/manual/en/function.vsprintf.php#119959
        return str_replace(array_keys($orderedReplace), array_values($orderedReplace), $phrase);
    } else {
        return vsprintf($phrase, $replacements);
    }
}
#1

BoldMinded (Brian)

Just tested this locally. Will be added to the next release.

Login to reply