質問

i'm trying to use gettext to translate the string in my site

gettext doesn't have problem detecting strings such as

<? echo _("Donations"); ?>

or

<? echo _("Donate to this site");?>

but obviously, usually we'll use codes like this in our site

<? echo _("$siteName was developed with one thing in mind"); ?>

Of course in the website, the $siteName is displayed correctly as

   My Website was developed with one thing in mind

if we put

$siteName = "My Website";

previously.

My problem is, i'm using poedit to extract all the strings in my codes that needs to be translated, and it seems poedit doesn't extract all string with php codes like I described above. So how do I get poedit extract strings with php code inside it too? Or is there any other tools I should use?

役に立ちましたか?

解決

One possibility is to use sprintf. Just make sure you keep the percent (%) in the poedit string!

echo sprintf( _("This %s can be translated "), 'string');

Or when using multiple variables

echo vsprintf( _("This %s can be %s"), ['string', 'translated']);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top