how do I generate po file from php array?
-
14-04-2021 - |
Question
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...
Solution
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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow