Page 1 of 1

Translate Formatted Strings

Posted: Sun Sep 10, 2023 2:16 am
by CCDzine
Hi, folks.

I am new here, WordPress/ClassicPress guy that learned about Bludit six days ago. I am building a theme and I would like to know the best way to handle the translation of strings output by s/printf() functions.

Example:

Code: Select all

$string = sprintf(
    $L->get( 'My string with %s a function in the middle' ),
    my_function()
);
Thanks

Re: Translate Formatted Strings

Posted: Tue Sep 12, 2023 2:19 pm
by Misteric
You can use some php functions:
- strstr() (case sensitive),
- stristr() (case insenstive),

Put it into an IF statement, like this:

Code: Select all

if (strstr( $L->get( 'String with %s a function' ), '%s')) {
  // do something
}

Re: Translate Formatted Strings

Posted: Tue Sep 12, 2023 3:35 pm
by CCDzine
Thank you. That got me thinking about it the other way around. I ended up with...

Code: Select all

$string = $L->get( 'default-string' );
if ( strstr( $L->get( 'replace-string' ), '%replace%' ) ) {
	$string = str_replace( '%replace%', my_function(), $L->get( 'replace-string' ) );
}