1

I've a php language file, that lists values in an array.

I want to transform this into .po file.

php file looks like this:

<?php 

$LANG_ = array(

// advanced search buttons
"_search1"              => "Start Search",
"_search2"              => "Hide Search Box",
"_search3"              => "Search",

// page numbering
"_pn1"              => "Page %CURRENT_PAGE% of %TOTAL_PAGES%",
"_pn2"              => "<< First",
"_pn3"              => "Last >>",

// _tpl_article-add.php
"_tpl_article-add1"             => "Article Submission Form",
"_tpl_article-add2"             => "Submit Article",
"_tpl_article-add3"             => "Article Submitted Successfully",


// errors
"_err14"            => "Delete",
"_err144"           => "Display Image",
"_err15"            => "Edit",
"_err16"            => "View",

);
?>

This is just an example, file itself is huge, over 3000 lines. It would kill me to insert every single one of these into a po catalog manually. Is there something that can automate this for me?

I'm using poedit.

Thanks, I'm new to this, so any insight will be useful...

2 Answers2

4

Try php2po which will convert a PHP array into PO files and will also make sure that all of your escaping is correct. The PO file can then be edited in a tool like Poedit or Virtaal.

Dwayne
  • 845
  • 7
  • 11
2

The first entry is the file header, look into an existing gettext file (-po) what is needed. The escaping I did with addslashes; maybe you need to do more.

$fh = fopen("en.po", 'w');
fwrite($fh, "#\n");
fwrite($fh, "msgid \"\"\n");
fwrite($fh,  "msgstr \"\"\n");

foreach ($LANG_ as $key => $value) {
    $key = addslashes($key);
    $value = addslashes($value);
    fwrite($fh, "\n");
    fwrite($fh, "msgid \"$key\"\n");
    fwrite($fh, "msgstr \"$value\"\n");
}
fclose($fh);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • hm, that looks like an awesome solution... oh if I could understand that much php :))) anyhow, I put it together with my array in one php file, and I got syntax error. Something about fclose... –  Jan 05 '12 at 22:46
  • I did not test the code, but `fclose($fh);` looks correct. Maybe missing newline thereafter. – Joop Eggen Jan 05 '12 at 22:56
  • I found it, it was fwrite($fh, "msgstr=\"$value\"\n); missing " after n. I will test it now and report :) –  Jan 05 '12 at 23:03
  • hm, the file I get the en.po seems empty :( I mean when I open it with poedit there is nothing inside, how is this possible? But when i view it with normal text editor lines are there... hm :) u helped me allot already. –  Jan 05 '12 at 23:12
  • I wrote an equal sign after msgid and msgstr that should have been blanks. Will edit answer – Joop Eggen Jan 06 '12 at 08:38