PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, September 1, 2022

[FIXED] how do I generate po file from php array?

 September 01, 2022     localization, mo, po, poedit, translation     No comments   

Issue

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);


Answered By - Joop Eggen
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing