54

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 11

20

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.
5
  • Much better and easier than POEdit. Clean UI. Has all the i18n translation functions in the keyword list by default. Thank you.
    – Paras Shah
    Commented Oct 1, 2018 at 5:21
  • 2
    Used this after realizing that POEdit's WordPress features are paid. Worked perfectly!
    – Swen
    Commented Nov 30, 2018 at 16:17
  • Worked perfectly for me too and easier, very easier than poedit. Commented Apr 17, 2020 at 7:53
  • 1
    Not available for linux :(
    – Tofandel
    Commented Aug 17, 2021 at 11:19
  • Not anymore up-to-date, consider using WP CLI. Commented Nov 8, 2022 at 14:45
57

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.

5
  • 2
    this should be the new accepted answer
    – maysi
    Commented May 26, 2020 at 11:54
  • 2
    I 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
    – charlesdeb
    Commented Dec 31, 2020 at 16:06
  • THE best and simplest way imo
    – wickywills
    Commented Mar 30, 2021 at 13:40
  • This is the most easiest and best and free way to generate pot file. It should be the top answer. Commented Jul 27, 2021 at 12:32
  • 1
    This is the most up-to-date answer because WP CLI is the solution to everything. No more messing another workaround while there is an official and documented way. Commented Nov 8, 2022 at 14:44
44

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:

  1. In the "File" menu select "New"
  2. Select the language that you used in your theme (probably English)
  3. In the "Catalog" menu select "Properties"
  4. Enter the project information in the "Translation properties" tab
  5. Go to the 3rd tab "Sources keywords"
  6. 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.)
  7. Click on the "OK" button at the bottom
  8. In the "File" menu select "Save As.."
  9. 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)
  10. In the "Catalog" menu select "Properties" again
  11. Go to the 2nd tab "Sources paths"
  12. 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)
  13. Next to "Path" click on the "New item" button and enter . (this will make it scan your theme directory and its subdirectories)
  14. Click on the "OK" button at the bottom
  15. In the project window click on "Update" (2nd icon at the top)
  16. 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.
    – Otto
    Commented Jan 30, 2016 at 1:04
  • 1
    It worked really well for my plugin Commented Jan 17, 2017 at 15:12
  • It works for the plugin, but doesnt translate the plugin description from header Commented Feb 27, 2018 at 6:51
  • 1
    PoEdit (the free version) doesn't give the option to save a new file as a .pot file
    – PaulJ
    Commented Dec 10, 2021 at 18:00
27

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? Commented Dec 25, 2014 at 6:57
  • 2
    Yes, just use "wp-plugin" instead of "wp-theme" and it will generate a POT file for a plugin.
    – Otto
    Commented Dec 25, 2014 at 12:39
  • Every other method that does not use the WordPress specific tools is broken in some way.
    – Otto
    Commented Jan 30, 2016 at 1:03
  • Awesome tools! Though it kept throwing errors like "Couldn't generate file" even though I chmod +x the directories..
    – Ismail
    Commented Dec 22, 2016 at 13:14
  • 1
    Why would chmod +x help? Maybe you should try making the directory writable instead of executable.
    – Otto
    Commented Dec 23, 2016 at 9:25
0

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.
    – Otto
    Commented Jan 30, 2016 at 1:05
  • Woot? It's a WP plugin. Commented Oct 16, 2017 at 8:31
0

For anyone who had like to use Poedit instead of Easy Po.

  1. Download (latest version of) Poedit and Install
  2. 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.

  3. 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

0

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).

0

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

dir

0

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.

-1

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
    Commented Jan 30, 2016 at 1:05
-1

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('/__\(\'(.+?)\',\''.$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(); 
  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

Use as echo __('This text','you-textdomain');

1
  • There is no sense on doing so, while there are cli tools already doing the magic, consider using WP CLI for the win. Commented Nov 8, 2022 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.