使用po编辑器创建新的po文件

时间:2012-10-23 17:58:34

标签: translation gettext po

我尝试了一些po editor softwares。我想知道为什么没有一个不允许从头创建一个新的po文件或添加新的条目到现有的。是否有任何黑客攻击,以便创建新条目? (我知道po文件是纯文本,可以在任何文本编辑器中编辑!)

2 个答案:

答案 0 :(得分:1)

PO编辑器用于将PO文件从一种语言翻译成另一种语言。它们不是为创建PO文件而设计的。您的PO文件应该作为构建系统的一部分创建,而不是由编辑器创建。

如果要创建它们以进行测试,则可以通过使用其他现有PO文件或通过取消编译MO文件(使用msgunfmt)来轻松创建它们。

答案 1 :(得分:1)

脚本自动创建.po文件,自动添加代码中的所有文本

  1. 添加load_theme_textdomain('my-textdomain', DIR 。'/ languages /');
  2. 创建文件夹wp-content / themes / MY_THEME / languages(将您的语言.mo文件添加到此文件夹)
  3. 创建 po.php 并添加此脚本:
  4. <?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. 转到 youdomain / wp-content / themes / MY_THEME / languages / po.php 并创建.po文件
    2. wp-content / themes / MY_THEME / languages / [LANG] .po 复制到桌面并使用 Poedit - &gt;打开编译MO - &gt;将[LANG] .mo复制到语言文件夹