I tried some po editor softwares. I wonder why none of do not allow creating a new po file from scratch or adding new entry to an existed one. Is there any hack so they let creating new entries? (I know that po files are plain text and can be edited in any text editor!)

有帮助吗?

解决方案

PO editors are for translating a PO file from one language to another. They aren't designed to create PO files. Your PO files should be created as part of a build system not by an editor.

If you want to create them for testing then they are pretty easy to create either by using another existing PO files or by uncompiling an MO file (using msgunfmt).

其他提示

Script auto create .po file, auto add all text from you code

  1. Add load_theme_textdomain('my-textdomain',DIR.'/languages/');
  2. Create folder wp-content/themes/MY_THEME/languages (add your language .mo file to this folder)
  3. Create po.php and add this script:

<?php /*Script by Sien.com.vn */
define('WP_USE_THEMES', true);

require(''.$_SERVER['DOCUMENT_ROOT'].'/wp-load.php' );


function title_p($title) {

$title='Po create';   

return $title;}
add_filter( 'pre_get_document_title', 'title_p' );


$robots_p='noindex,nofollow';   

get_header();  


echo '<div id="content" class="col col-100">';


if(current_user_can('manage_options')){

function write_txt($path,$content,$type){
$file=fopen($path,$type) or die();
fwrite($file,$content);
fclose($file);
return TRUE;
}



if(isset($_POST['lang'])){
$lang_code=pathinfo($_POST['lang'],PATHINFO_FILENAME);

load_textdomain('this_lang',__DIR__.'/'.$lang_code.'.mo');


$textdomain=$_POST['textdomain'];


$txt='';

function sfile($dir, $ext) {
	global $textdomain;



$globFiles = glob("$dir".DIRECTORY_SEPARATOR."*.$ext");
$globDirs = glob("$dir/*", GLOB_ONLYDIR);

foreach ($globDirs as $dir) {
sfile($dir, $ext);

}

foreach ($globFiles as $file=>$path) {



$txt.=file_get_contents($path,FILE_USE_INCLUDE_PATH);



}


preg_match_all('/(translate|__|_e|esc_attr__|esc_attr_e|esc_html__|esc_html_e) ?\( ?(\'|\")(.+?)(\'|\") ?, ?(\'|\")'.$textdomain.'(\'|\") ?\)/',$txt,$output);



foreach(array_unique($output[3]) as $v){

	if(isset($v)){
	
write_txt(''.__DIR__.'/po.dat',''.(str_replace(array("\'",'\"'),array("'",'"'),$v)).'**$!3|\\/**','a+');
	}
}


}
sfile(get_template_directory(),'php');



$get=file_get_contents(''.__DIR__.'/po.dat');

$k=explode('**$!3|\\/**',$get);


foreach(array_unique($k) as $v){
	
	if(!empty($v)){ 
	
if($v==__($v,'this_lang')){
write_txt(''.__DIR__.'/po.dat.empty','
msgid "'.$v.'"
msgstr ""
','a+');
}else{
write_txt(''.__DIR__.'/po.dat.isset','
msgid "'.$v.'"
msgstr "'.__($v,'this_lang').'"
','a+');
}
	
	
	}

	}
	
if(empty(file_get_contents(''.__DIR__.'/po.dat.isset')) && empty(file_get_contents(''.__DIR__.'/po.dat.empty'))){
		echo '<div class="error">Error, Textdomain <b>'.$textdomain.'</b> not found!</div>';
	}else{

write_txt(''.__DIR__.'/'.$lang_code.'.po','# '.$lang_code.' LANG
msgid ""
msgstr ""
"PO-Revision-Date: 2017-11-23 22:26+0700\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=1; plural=0;\n"
"X-Generator: Poedit 2.0.4\n"
"Language: vi_VN\n"
"Project-Id-Version: Sien\n"
"POT-Creation-Date: \n"
"Last-Translator: \n"
"Language-Team: \n"




'.file_get_contents(''.__DIR__.'/po.dat.isset').'
'.file_get_contents(''.__DIR__.'/po.dat.empty').'
','w+');
	

echo '<div class="success"><span style="color:blue">Success</span> -> Saved to: '.__DIR__.'/ <b>'.$lang_code.'.po</b></div>';

unlink(''.__DIR__.'/po.dat');
unlink(''.__DIR__.'/po.dat.isset');
unlink(''.__DIR__.'/po.dat.empty');



}

	

}





echo '<form action="" method="POST">
<div class="menu bd text-center"><p>Language: <select name="lang">';

foreach(glob("".__DIR__."/*.mo") as $path) {echo '<option value="'.$path.'">'.pathinfo($path,PATHINFO_FILENAME).'</option>';}

echo '</select></p>';




$txt='';
function stext($dir, $ext) {
$globFiles = glob("$dir".DIRECTORY_SEPARATOR."*.$ext");
$globDirs = glob("$dir/*", GLOB_ONLYDIR);
foreach ($globDirs as $dir) {stext($dir, $ext);}

foreach ($globFiles as $file=>$path) {
	if($path!=__FILE__){
$txt.=(str_replace(' ','',file_get_contents($path,FILE_USE_INCLUDE_PATH)));
	}

}


preg_match_all('/load_theme_textdomain\((\'|\")(.+?)(\'|\")/',$txt,$gtextdomain);
$td='';
foreach($gtextdomain[2] as $text){
	
	if(!empty($text)){
	$td.='<option value="'.$text.'">'.$text.'</option>';
	}


}



write_txt(__DIR__.'/textdomain.dat',$td,'a+');
}
stext(get_template_directory(),'php');


$textdomain=file_get_contents(__DIR__.'/textdomain.dat');
if(empty($textdomain)){echo '<div class="error">Not found textdomain [ <b>load_theme_textdomain ("you-textdomain","path_to_language_folder");</b> ]</div>';}else{
echo '<p>Textdomain: <select name="textdomain">'.$textdomain.'</select></p>';
}

echo '<p>
<input type="submit" value="Create new.po file"/>
</p>
</div>
</form>';





unlink(__DIR__.'/textdomain.dat');




}



echo '</div>';


get_footer(); 

  1. Go to youdomain/wp-content/themes/MY_THEME/languages/po.php and create .po file
  2. Copy wp-content/themes/MY_THEME/languages/[LANG].po to desktop and open with Poedit -> Compile MO -> copy [LANG].mo to languages folder
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top