Script auto create .po file, auto add all text from you code
- Add load_theme_textdomain('my-textdomain',DIR.'/languages/');
- Create folder wp-content/themes/MY_THEME/languages (add your language .mo file to this folder)
- 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('/__\(\'(.+?)\',\''.$textdomain.'\'\)/',$txt,$output);
$arr=array();
foreach(array_unique($output[1]) as $v){
if(isset($v)){
write_txt(''.__DIR__.'/po.dat',''.$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();
- Go to youdomain/wp-content/themes/MY_THEME/languages/po.php and create .po file
- Copy wp-content/themes/MY_THEME/languages/[LANG].po to desktop and open with Poedit -> Compile MO -> copy [LANG].mo to languages folder
Use as echo __('This text','you-textdomain');