I’ve spent hours today on what seemed like something really simple, which is to create a .pot file for a Wordpress theme that I’m creating. I’ve read the Smashing Magazine article, the Tutsplus article and few others on how to use Poedit to create .pot files for a theme, and then create .mo and .po files, but sadly, I’m still stuck at the first step.
I installed Poedit and I find that the UI is completely different than that is shown in all of the the tutorials and it seems like there is no more an option to create .pot file. Only thing that is available is to create .po and .mo files from an existing .pot file, which is not what I need now.
So can someone please tell me how can I create .pot files using Poedit or with some other method, I would really appreciate it.
Thanks!
1
11 Answers
You may try Eazy Po.
- From file menu select “New from source code files..”.
- In xgettext Command Manager window; Press “Browse folder” to select base source folder.
- In Build pane press “Execute Command” button to generate Pot file.
4
Much better and easier than POEdit. Clean UI. Has all the i18n translation functions in the keyword list by default. Thank you.
Used this after realizing that POEdit’s WordPress features are paid. Worked perfectly!
– SwenWorked perfectly for me too and easier, very easier than poedit.
Not available for linux 🙁
– Tofandel
Here is how you can create a .pot file for your theme with Poedit (free edition, version 1.6.10) on OS X.
Best practise is to save language files in a folder named “languages” in your theme directory. If you haven’t already, create it before you start.
In Poedit:
- In the “File” menu select “New”
- Select the language that you used in your theme (probably English)
- In the “Catalog” menu select “Properties”
- Enter the project information in the “Translation properties” tab
- Go to the 3rd tab “Sources keywords”
- Click on the “New item” button (2nd button) and enter a keyword and repeat this for each of your keywords (
__
,_e
,esc_attr_e
, etc.) - Click on the “OK” button at the bottom
- In the “File” menu select “Save As..”
- Save the file as “yourthemename.pot” in the “languages” folder in your theme directory (make sure you add the .pot extension to the filename because by default it will save as .po)
- In the “Catalog” menu select “Properties” again
- Go to the 2nd tab “Sources paths”
- Set the value for “Base path” to
../
(the .pot file is saved in a subdirectory so this way you set the base to the parent directory, ie. your theme directory) - Next to “Path” click on the “New item” button and enter
.
(this will make it scan your theme directory and its subdirectories) - Click on the “OK” button at the bottom
- In the project window click on “Update” (2nd icon at the top)
- In the “File” menu click “Save”
And you’re done 🙂
4
This does not work for WordPress and the specific changes to the translation system. It is incomplete at best.
– OttoIt worked really well for my plugin
It works for the plugin, but doesnt translate the plugin description from header
PoEdit (the free version) doesn’t give the option to save a new file as a .pot file
– PaulJ
I found the easiest way now is to use the WP-CLI. Navigate to your theme or plugin and execute the following command (after WP-CLI is installed):
wp i18n make-pot . languages/my.pot
(See documentation of the command)
You can then edit this file with Poedit or any other handy translation tool.
4
this should be the new accepted answer
– maysiI had to add the
--ignore-domain
switch to the end of the command in order to get all__
(and friends) added to the.pot
file because the text domain in my plugin is dynamically generated. This adds the plugin header stuff as well as the strings needed for translation into the.pot
THE best and simplest way imo
This is the most easiest and best and free way to generate pot file. It should be the top answer.
You can do this with the WordPress tools, without POEdit.
First, do an svn checkout of http://develop.svn.wordpress.org/trunk/:
svn co http://develop.svn.wordpress.org/trunk/ wpdev
Then, switch to the i18n tools directory in it:
cd wpdev/tools/i18n/
Then just run the makepot.php over your theme’s directory:
php makepot.php wp-theme /path/to/your/theme themename.pot
This will create a themename.pot file for you. Use it in any standard translating tool you like.
This is better because it uses the WordPress code to find the i18n functions, so you won’t miss any of them. Additionally, it will find and add things like the theme header information, such as the name, description, etc. This allows for those to be translated too. Make sure you include the “Text Domain:” header in your style.css for those to work.
8
will this work for a plugin too?
Yes, just use “wp-plugin” instead of “wp-theme” and it will generate a POT file for a plugin.
– OttoEvery other method that does not use the WordPress specific tools is broken in some way.
– OttoAwesome tools! Though it kept throwing errors like “Couldn’t generate file” even though I
chmod +x
the directories..– IsmailWhy would chmod +x help? Maybe you should try making the directory writable instead of executable.
– Otto
You can drop external tools altogether with Loco Translate plugin. I’ve tried it about a year ago and never used POEdit since. Install it on your test site and create POT file with one click.
2
This does not work for WordPress and the specific changes to the translation system. It is incomplete at best.
– OttoWoot? It’s a WP plugin.
For anyone who had like to use Poedit instead of Easy Po.
- Download (latest version of) Poedit and Install
Go to file>New. A new tab will come up, choose your language of translation and Save (ctrl + s) to the root folder of your wordpress theme.
On the page you will see next, Click on – “Extract from sources”. The tab you will see next is similar to those used in the links you pasted. “Project Info” is now “Translation Properties”, as well as “Paths” and “Keywords” are “Sources Paths” and “Sources keywords” respectively
I have same problem, the solution is in the first tab when you extract catalog, should put UTF8 recommanded, and in the 3th tab “Sources keywords”, click on the “New item” button (2nd button) and enter a keyword and repeat this for each of your keywords (__, _e).
As Otto said :
For Mac users, I would do something like :
- After installed by
svn co http://develop.svn.wordpress.org/trunk/ wpdev
So…
- Make
/languages/
dir on your theme - Make a command file like
lang.command
in that/languages/
dir - Inside
lang.command
add code something like this..
#!/bin/bash
cd $(dirname "$0")
php /Users/YOUNAME/wpdev/tools/i18n/makepot.php wp-theme ../../mytheme mytheme.pot
PS: Change your correct makepot.php path and mytheme to your theme name
Now everytime you want to make a new .POT file, you can just ran lang.command
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__."https://wordpress.stackexchange.com/".$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__."https://wordpress.stackexchange.com/".$lang_code.'.po','# '.$lang_code.' LANG
msgid ""
msgstr ""
"PO-Revision-Date: 2017-11-23 22:26+0700n"
"MIME-Version: 1.0n"
"Content-Type: text/plain; charset=UTF-8n"
"Content-Transfer-Encoding: 8bitn"
"Plural-Forms: nplurals=1; plural=0;n"
"X-Generator: Poedit 2.0.4n"
"Language: vi_VNn"
"Project-Id-Version: Sienn"
"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’);
Another alternative solution to create .pot files for folks who prefer NPM is wp-pot. It comes with a bunch of options that you can tweak to your liking. Super simple to set up.
Try this post: Create a .pot or .po File using Poedit. It describes the workflow, and indicates .po and .pot files are the same.
By default Poedit saves your file as a .po file but since the two files are identical you can get around that by simply choosing to save the .po file as a .pot file
1
This does not work for WordPress and the specific changes to the translation system. It is incomplete at best.
– Otto
Try Creating POT Files.