views:

37

answers:

2

I'm merging two different versions of a translations array. The more recent version has a lot of changes, over several thousand lines of code.

To do this, I loaded and evaluated the new file (which uses the same structure and key names), then loaded and evaluated the older version, overwriting the new untranslated values in the array with the values we already have translated.

So far so good!

However, I want to be able to echo out the constructor for this new merged array so I can cut and paste it into the new translation file, and have job done (apart from completing the rest of the translations..).

The code looks like this (lots of different keys, not just index):

$lang["index"]["chart1_label1"] = "Subscribed";
$lang["index"]["chart1_label2"] = "Unsubscribed";

And the old..

$lang["index"]["chart1_label1"] = "Subscrito";
$lang["index"]["chart1_label2"] = "Não subscrito";

After loading the two files, I end up with a merged $lang array, which I then want to echo out in the same form, so it can be used by the project.

However, when I do something like this..

foreach ($lang as $key => $value) { 
    if (is_array($value)) {
        foreach ($value as $key2 => $value2) {
            echo "$lang['".$key."']"; // ... etc etc
        }
    }
}

..obviously I get "ArrayIndex" etc etc instead of "$lang". How to echo out $lang without it being evaluated..? Once this is working, can add in the rest of the brackets etc (I realise they are missing), but just want to make this part work first.

If there's a better way to do this, all ears too!

Thanks.

edit:

What I was really looking for was this:

"Note: Unlike the three other syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings."

So no need even to escape. May come in handy one day for someone... about merging the arrays, there's a great recursive array merging function on the PHP manual page for array_merge(), which also came in handy.

Thanks to whoever downvoted! Love you too.

+1  A: 

Either escape the $: "\$lang" or use single quotes: '$lang'

Note that you really don't need to post a whole page of backstory, if your entire question just boils down to "how do I output a dollar sign in PHP"

Matti Virkkunen
Knew it was something simple, thanks. The point about putting the problem in context is that its not just about "how duz i maek hambureger", but maybe someone knows a better technique to do exactly the same thing. I thought discussion was the point of forums..? ;)
danp
+1  A: 

there is var_export() function, but I do not understand the purpose of this terrible mess.
It's all too... manual

why not to use a gettext - an industry standard for multi-language site?

or at least some other format, more reliable than plain PHP one?
Programming code is not intended to be written automatically.

Col. Shrapnel
Gettext is great until you want to let someone who has no technical knowledge (ie clients) edit it. Then you realise you're in deep, deep trouble. Thanks for the var_export() tip, I'll look it up.
danp
Also, this ain't my code, I personally go with XML for this kind of stuff...
danp
I don't think a bunch of PHP arrays is any more idiot-safe than .po files.
Matti Virkkunen
@danp yes XML is a nearly the best choice. Though I wonder how it will be handled by someone who has no technical knowledge ;)
Col. Shrapnel
It's easier to build a web admin interface on top of XML than .po files.. :p
danp