Translate Formatted Strings

Post Reply
User avatar
CCDzine
Ssr. Bludit
Posts: 10
Joined: Thu Sep 07, 2023 11:49 pm
Location: Sierra Nevada Mountains, Califonia
Has thanked: 3 times
Been thanked: 6 times
Contact:

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
Greg Sweet
User avatar
Misteric
Ssr. Bludit
Posts: 16
Joined: Mon Aug 08, 2022 2:55 pm
Has thanked: 3 times
Been thanked: 6 times

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
}
Last edited by Misteric on Wed Sep 13, 2023 8:39 am, edited 1 time in total.
User avatar
CCDzine
Ssr. Bludit
Posts: 10
Joined: Thu Sep 07, 2023 11:49 pm
Location: Sierra Nevada Mountains, Califonia
Has thanked: 3 times
Been thanked: 6 times
Contact:

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' ) );
}
Greg Sweet
Post Reply