php string variables in gettext (forgot one case scenario)
Asked Answered
W

1

1

I've posted a question yesterday but I just realized that the answer doesn't seem to be working for a certain situation. The post was php string variables in gettext and here is what I asked about:

Hello,

How does gettext translate string variables? It doesn't seem to want to do it.. lets say I have $sentence = "Hello World"; and then I want to echo ($sentence); ... how can I do that so that I can translate what's inside $sentence in Poedit?I can use -> echo sprintf(("%s test"), $sentence) and this will print "Hello World test" in the browser but it will appear as "%s test" in Poedit and I won't get the translated version of Hello World inside of Poedit. So how can I use string variables inside Poedit? Thanks!

This has been solved.

The problem occurs if I want to grab data from an XML file and translate it.

I want to be able to do the following:

$foo = $xmlData -> titleText;

and then

echo _($foo); 

or something like

$finalVar = _($foo);

If I look at the php file in the browser I can see the content of $foo on the page but Poedit doesn't pick up the string inside $foo so I can translate it.

(I believe this problem will also occur when translating info within js files)

Thank you for your help,

Simon

Wallas answered 29/8, 2009 at 11:53 Comment(1)
If the strings you want to translate are actually the string in XML file and not the PHP file, you have to run xgettext against the XML file to extract the strings to translate, which you then need to translate. If your translation database / .mo file contains all the required translations, the code using just _($foo) should work just fine.Resupinate
E
1

Obviously you can't make Poedit understand PHP.

EDIT: Have you considered a different approach?

if (true) {
    $foo = _('variable holds true');
}

else {
    $foo = _('variable holds false');
}

echo $foo;

You can have PHP generated the gettext compatible XML and regarding the database, I believe the best solution is to store the translation in the DB itself, however you can also create a script to dump all the keys and values from the database and use gettext on it.

Electroballistics answered 29/8, 2009 at 11:56 Comment(3)
I understand that, but your comment doesn't really help me with my problem. Is there a workaround? I could use a language structure for the XML files but I would prefer to solve this issue with gettext somehow... This problem will also occur if I would get data from a database...Wallas
I'm sorry but if I know how to solve your problem I would have answered it. I don't think it's possible.Electroballistics
Yes I've considered that approach but I was trying to figure out a way to use the XML files I have. I didn't want to generate them, I wanted to use it as a skeleton for things like input fields for categories, meta tags and so on. I think I'm going to just drop XML from the picture or perhaps do what I said before (duplicate the XML files according to the languages). I wish Poedit could compile PHP code, it would be much easier... Thanks for your help.Wallas

© 2022 - 2024 — McMap. All rights reserved.