2012-10-23 106 views
0

我嘗試了一些po editor softwares。我想知道爲什麼沒有一個不允許從頭開始創建一個新的po文件或將新的條目添加到現有的文件中。有沒有黑客,所以他們讓創造新的條目? (我知道po文件是純文本,可以在任何文本編輯器中編輯!)用po編輯器創建新的po文件

+0

你不解釋你爲什麼要創建一個新的PO文件,而不是讓你的軟件構建系統創建它們。 – Dwayne

+0

@Dwayne我想使用PO文件製作一個PHP網站多語言 – PHPst

回答

1

PO編輯器用於將PO文件從一種語言翻譯爲另一種語言。它們不是爲創建PO文件而設計的。您的PO文件應該作爲構建系統的一部分而不是由編輯器創建。

如果您想創建它們進行測試,那麼通過使用其他現有PO文件或通過解壓縮MO文件(使用msgunfmt)可以很容易地創建它們。

+0

我想使用PO文件製作一個MVC PHP網站多語言。那裏有任何可以從PHP文件製作PO文件的工具嗎? – PHPst

+0

查看[php2po](http://translate.sourceforge.net/wiki/toolkit/php2po) - 它將轉換字符串數組和PHP開發人員常用於本地化的類似結構。如果您使用Gettext,則使用xgettext提取本地化字符串。 – Dwayne

1

腳本自動創建.po文件,自動從您的代碼添加的所有文字

  1. 添加load_theme_textdomain( '我的-textdomain',DIR '/語言/');
  2. 創建文件夾的wp-content /主題/ MY_THEME /語言(添加你的語言的.mo文件,這個文件夾)
  3. 創建po.php並添加這個腳本:

<?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) { 
 
\t 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){ 
 

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

 

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

 

 

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

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

 

 
foreach(array_unique($k) as $v){ 
 
\t 
 
\t if(!empty($v)){ 
 
\t 
 
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+'); 
 
} 
 
\t 
 
\t 
 
\t } 
 

 
\t } 
 
\t 
 
if(empty(file_get_contents(''.__DIR__.'/po.dat.isset')) && empty(file_get_contents(''.__DIR__.'/po.dat.empty'))){ 
 
\t \t echo '<div class="error">Error, Textdomain <b>'.$textdomain.'</b> not found!</div>'; 
 
\t }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+'); 
 
\t 
 

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

 

 

 
} 
 

 
\t 
 

 
} 
 

 

 

 

 

 
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) { 
 
\t if($path!=__FILE__){ 
 
$txt.=(str_replace(' ','',file_get_contents($path,FILE_USE_INCLUDE_PATH))); 
 
\t } 
 

 
} 
 

 

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

 

 
} 
 

 

 

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

  • 轉到youdomain /可溼性粉劑內容/主題/ MY_THEME /語言/ po.php並創建.po文件
  • 複製的wp-content /主題/ MY_THEME /語言/ [LANG]的.po到桌面並打開poEdit的 - >編譯MO - >拷貝[LANG] .MO到語言文件夾
  • +0

    謝謝,我希望將來能夠使用你的代碼。 – PHPst