<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Geert De Deckere</title>
	
	<link>http://geertdedeckere.be</link>
	<description />
	<lastBuildDate>Thu, 01 Nov 2012 19:20:25 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/geertdedeckere" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="geertdedeckere" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Loading WordPress language files the right way</title>
		<link>http://geertdedeckere.be/article/loading-wordpress-language-files-the-right-way</link>
		<comments>http://geertdedeckere.be/article/loading-wordpress-language-files-the-right-way#comments</comments>
		<pubDate>Sun, 18 Mar 2012 09:56:56 +0000</pubDate>
		<dc:creator>Geert De Deckere</dc:creator>
				<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://geertdedeckere.be/?p=7</guid>
		<description><![CDATA[WordPress provides a well-equipped toolbox for making plugins and themes translatable. However, you would be surprised to see how many plugins make it hard to properly add a translation for it. A setup for loading language files you will often encounter in WordPress plugins looks like this: class My_Plugin { public function __construct() { load_plugin_textdomain('my-plugin', [...]]]></description>
				<content:encoded><![CDATA[<p>WordPress provides a well-equipped toolbox for making plugins and themes translatable. However, you would be surprised to see how many plugins make it hard to properly add a translation for it. A setup for loading language files you will often encounter in WordPress plugins looks like this:</p>
<pre><code class="language-php">class My_Plugin {

    public function __construct()
    {
        load_plugin_textdomain('my-plugin', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
    }
}

$my_plugin = new My_Plugin;</code></pre>
<p>As soon as the plugin file is loaded, the plugin constructor will be triggered. In there, <code>load_plugin_textdomain()</code> is called and gets told to load the applicable mo-files from the subdirectory “languages” of the plugin’s directory. That looks fine. You just drop your translation files in that directory and everybody is happy. Then the plugin gets updated and your custom translation is deleted. Ouch! Clearly, <strong>putting your own language files somewhere in the plugin’s directory is not a good idea</strong>.</p>
<p>A look at the source of the <code>load_plugin_textdomain()</code> function shows that after building the path to the mo-file, it simply calls the more general <code>load_textdomain()</code> function. That functions provides some useful hooks you can use to load your custom translation files without having to touch the plugin code.</p>
<h2>Call <code>load_plugin_textdomain()</code> during <code>init</code></h2>
<p>The plugin setup shown above has one big problem, though. By the time you want to hook into the <code>load_textdomain()</code> function, in your theme’s <code>functions.php</code> file for example, it will be too late already since the <code>load_plugin_textdomain()</code> call gets done immediately when the plugin is loaded. That is why when developing a plugin you should <strong>call <code>load_plugin_textdomain()</code> during the <code>init</code> action and not earlier</strong>. Let’s fix that.</p>
<pre lang="php">class My_Plugin {

    public function __construct()
    {
        add_action('init', array($this, 'load_plugin_textdomain'));
    }

    public function load_plugin_textdomain()
    {
        load_plugin_textdomain('my-plugin', FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
    }
}</pre>
<p>Now you have the chance to hook into <code>load_textdomain()</code>. Before we get there, it is worth noting that function calls for translatable strings, like <code>__()</code> or <code>_e()</code>, executed before the language files are loaded will be useless, as shown below. Be sure to bind the whole plugin initialisation to the <code>init</code> action to prevent this problem.</p>
<pre lang="php">public function __construct()
{
    add_action('init', array($this, 'load_plugin_textdomain'));
    $text = __('I will not be translated!', 'my-plugin');
}</pre>
<h2><code>load_textdomain()</code> hooks</h2>
<p>Back to <code>load_textdomain()</code>. This functions provides three useful hooks, all of which get passed the language domain and path to the mo-file as arguments, albeit in different orders.</p>
<h3>Filter: <code>override_load_textdomain</code></h3>
<p>This filter allows you to block the loading of a certain language file by returning <code>TRUE</code>. Even though this is not what we are looking for now, here is a quick example to block any language file that would get loaded from the plugin’s own “languages” directory. This code could go in your theme’s <code>functions.php</code> file.</p>
<pre lang="php">add_filter('override_load_textdomain', 'block_my_plugin_default_language_files', 10, 3);

function block_my_plugin_default_language_files($override, $domain, $mofile)
{
    if ('my-plugin' === $domain &amp;&amp; plugin_dir_path($mofile) === WP_PLUGIN_DIR.'/my-plugin/languages/')
        return TRUE;

    return $override;
}</pre>
<h3>Filter: <code>load_textdomain_mofile</code></h3>
<p>This filter allows you to change the path of the mo-file to load. This means that the original mo-file will not be loaded anymore. You basically replace the language file of the plugin with your own. This is a viable option, yet having the plugin’s default language file as a fallback is often a more preferable setup.</p>
<pre lang="php">add_filter('load_textdomain_mofile', 'replace_my_plugin_default_language_files', 10, 2);

function replace_my_plugin_default_language_files($mofile, $domain)
{
    if ('my-plugin' === $domain)
        return WP_LANG_DIR.'/my-plugin/'.$domain.'-'.get_locale().'.mo';

    return $mofile;
}</pre>
<h3>Action: <code>load_textdomain</code></h3>
<p>This action looks like your best option. It allows you to load one or more additional language files on top of the plugin’s language files. The default language files of the plugin will be loaded after yours.</p>
<pre lang="php">add_action('load_textdomain', 'load_custom_language_files_for_my_plugin', 10, 2);

function load_custom_language_files_for_my_plugin($domain, $mofile)
{
    // Note: the plugin directory check is needed to prevent endless function nesting
    // since the new load_textdomain() call will apply the same hooks again.
    if ('my-plugin' === $domain &amp;&amp; plugin_dir_path($mofile) === WP_PLUGIN_DIR.'/my-plugin/languages/')
    {
        load_textdomain('my-plugin', WP_LANG_DIR.'/my-plugin/'.$domain.'-'.get_locale().'.mo');
    }
}</pre>
<h2>Load from the WordPress languages directory by default</h2>
<p>The <code>load_textdomain()</code> functions allows for a lot of flexibility. Yet as a plugin developer you can make it even easier for the people using your plugin. By default you could look in the WordPress languages directory for translations of your plugin. That way custom translations would only need to be dropped in the subdirectory by the name of your plugin, in the WordPress languages directory. No need for the user to attach to any hooks in the code, and no problems during a plugin update.</p>
<p>Note that the default WordPress languages directory is “wp-content/languages/” but it can be overridden in <code>wp-config.php</code> by defining <code>WP_LANG_DIR</code> yourself.</p>
<pre lang="php">public function load_plugin_textdomain()
{
    $domain = 'my-plugin';
    // The "plugin_locale" filter is also used in load_plugin_textdomain()
    $locale = apply_filters('plugin_locale', get_locale(), $domain);

    load_textdomain($domain, WP_LANG_DIR.'/my-plugin/'.$domain.'-'.$locale.'.mo');
    load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
}</pre>
<p>Finally, I would like to point out that is important to <strong>load custom user language files from <code>WP_LANG_DIR</code> <em>before</em> you load the language files that ship with the plugin</strong>. When multiple mo-files are loaded for the same domain, the first found translation will be used. This way the language files provided by the plugin will serve as a fallback for strings not translated by the user.</p>
<p>All in all by following a few practical guidelines you can make the loading of translations for your plugin a joy instead of a frustration.</p>
]]></content:encoded>
			<wfw:commentRss>http://geertdedeckere.be/article/loading-wordpress-language-files-the-right-way/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Saving a HTTP request by combining screen and print styles</title>
		<link>http://geertdedeckere.be/article/saving-a-http-request-by-combining-screen-and-print-styles</link>
		<comments>http://geertdedeckere.be/article/saving-a-http-request-by-combining-screen-and-print-styles#comments</comments>
		<pubDate>Sun, 19 Apr 2009 13:58:44 +0000</pubDate>
		<dc:creator>Geert De Deckere</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://geertdedeckere.be/?p=13</guid>
		<description><![CDATA[Minimizing HTTP requests is a very effective way for speeding up your website. In his book “High Performance Web Sites”, Steve Souders talks about this practice as the number one rule to front-end optimization. Some well-known techniques to reduce HTTP requests include using CSS sprites and combining multiple scripts and stylesheets into single files. Ideally [...]]]></description>
				<content:encoded><![CDATA[<p>Minimizing HTTP requests is a very effective way for speeding up your website. In his book “<a href="http://oreilly.com/catalog/9780596529307/">High Performance Web Sites</a>”, Steve Souders talks about this practice as the number one rule to front-end optimization.</p>
<p>Some well-known techniques to reduce HTTP requests include using <a href="http://alistapart.com/articles/sprites">CSS sprites</a> and combining multiple scripts and stylesheets into single files. Ideally you should be using only one stylesheet. However, how many times have you seen the following two lines in the <code>&lt;head&gt;</code> of a HTML page?</p>
<pre><code class="language-markup">&lt;link rel="stylesheet" type="text/css" href="screen.css" media="screen" /&gt;
&lt;link rel="stylesheet" type="text/css" href="print.css" media="print" /&gt;</code></pre>
<h2>Combining screen and print styles</h2>
<p>I get the impression many people are unaware of the fact you can combine screen and print styles into a single stylesheet. Actually you can combine all media types, and it is easy to do.</p>
<h3>Linking to your stylesheet</h3>
<pre><code class="language-markup">&lt;link rel="stylesheet" type="text/css" href="styles.css" media="screen, print" /&gt;</code></pre>
<p>You may be wondering why I did not use <code>media="all"</code> or simply omitted the <code>media</code> attribute. By stating the media types the stylesheet applies to right there in the <code>&lt;link&gt;</code>, user agents may decide not to load the stylesheet at all if it does not apply to the current device. Another possible HTTP request less.</p>
<h3>Splitting up your stylesheet</h3>
<p>All that is left to do is telling the browser which parts of <code>styles.css</code> apply to screen and which parts apply to print. The example below is self-explanatory.</p>
<pre lang="css">
@media screen {
	body { font-size:14px; }
}

@media print {
	body { font-size:10pt; }
	h1, h2, h3 { page-break-after:avoid; }
}
</pre>
<p>Styles that apply to both screen and print do not need not be wrapped in an <code>@media</code> rule in this case since the stylesheet itself applies to both media types already, as specified in the <code>&lt;link&gt;</code>. Just for the sake of completeness, though, note that you can specify multiple media types separated by commas.</p>
<pre lang="css">
@media screen, print {
	body { line-height:1.4; }
}
</pre>
<p>To freshen up your knowledge about <code>@media</code> rules, I recommend you have a look at the CSS specification for <a href="http://www.w3.org/TR/CSS21/media.html">media types</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://geertdedeckere.be/article/saving-a-http-request-by-combining-screen-and-print-styles/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing Codebench</title>
		<link>http://geertdedeckere.be/article/introducing-codebench</link>
		<comments>http://geertdedeckere.be/article/introducing-codebench#comments</comments>
		<pubDate>Sat, 18 Apr 2009 15:30:13 +0000</pubDate>
		<dc:creator>Geert De Deckere</dc:creator>
				<category><![CDATA[Kohana]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Regex]]></category>

		<guid isPermaLink="false">http://geertdedeckere.be/?p=14</guid>
		<description><![CDATA[For a long time I have been using a quick-and-dirty benchmark.php file to optimize bits of PHP code, many times regex-related stuff. The file contained not much more than a gettimeofday function wrapped around a for loop. It worked, albeit not very efficiently. Something more solid was needed. I set out to create a far [...]]]></description>
				<content:encoded><![CDATA[<p>For a long time I have been using a quick-and-dirty <code>benchmark.php</code> file to optimize bits of PHP code, many times regex-related stuff. The file contained not much more than a <a href="http://php.net/gettimeofday"><code>gettimeofday</code></a> function wrapped around a <code>for</code> loop. It worked, albeit not very efficiently. Something more solid was needed. I set out to create a far more usable piece of software to aid in the everlasting quest to squeeze every millisecond out of those regular expressions.</p>
<h2>Codebench goals</h2>
<h3>Benchmark multiple regular expressions at once</h3>
<p>Being able to compare the speed of an arbitrary amount of regular expressions would be tremendously useful. In case you are wondering – yes, I had been writing down benchmark times for each regex, uncommenting them one by one. You get the idea. Those days should be gone forever now.</p>
<h3>Benchmark multiple subjects at once</h3>
<p>What gets overlooked too often when testing and optimizing regular expressions is the fact that speed can vastly differ depending on the subjects, also known as input or target strings. Just because your regular expression matches, say, a valid email address quickly, does not necessarily mean it will quickly realize when an <em>invalid</em> email is provided. I plan to write a follow-up article with hands-on regex examples to demonstrate this point. Anyway, Codebench allows you to create an array of subjects which will be passed to each benchmark.</p>
<h3>Make it flexible enough to work for all PCRE functions</h3>
<p>Initially I named the module “Regexbench”. I quickly realized, though, it would be flexible enough to benchmark all kinds of PHP code, hence the change to “Codebench”. While tools specifically built to help profiling PCRE functions, like <a href="http://php.net/preg_match"><code>preg_match</code></a> or <a href="http://php.net/preg_replace"><code>preg_replace</code></a>, definitely have their use, more flexibility was needed here. You should be able to compare all kinds of constructions like combinations of PCRE functions and native PHP string functions.</p>
<h3>Create clean and portable benchmark cases</h3>
<p>Throwing valuable benchmark data away every time I needed to optimize another regular expression had to stop. A clean file containing the complete set of all regex variations to compare, together with the set of subjects to test them against, would be more than welcome. Moreover, it would be easy to exchange benchmark cases with others.</p>
<h3>Visualize the benchmarks</h3>
<p>Obviously providing a visual representation of the benchmark results, via simple graphs, would make interpreting them easier. Having not to think about Internet Explorer for once, made writing CSS a whole lot more easy and fun. It resulted in some fine graphs which are fully resizable.</p>
<p>Below are two screenshots of Codebench in action. <code>Valid_Color</code> is a class made for benchmarking different ways to validate hexadecimal HTML color values, e.g. <code>#FFF</code>. If you are interested in the story behind the actual regular expressions, take a look at <a href="http://forum.kohanaphp.com/comments.php?DiscussionID=2192">this topic in the Kohana forums</a>.</p>
<p class="figure x-large">
<a class="left large" href="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot1.png"><img src="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot1-480x249.png" alt="Codebench screenshot" width="480" height="249" /></a><br />
<span class="pull small">Benchmarking seven ways to validate HTML color values</span>
</p>
<p class="figure x-large">
<a class="left large" href="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot2.png"><img src="http://www.geertdedeckere.be/wp-content/uploads/2009/04/codebench_screenshot2-480x182.png" alt="Codebench screenshot" width="480" height="182" /></a><br />
<span class="pull small">Collapsable results per subject for each method</span>
</p>
<h2>Working with Codebench</h2>
<p><strong><a href="http://github.com/GeertDD/codebench/tree/master">Download Codebench</a></strong> at GitHub. Since Codebench is a module for the Kohana PHP framework, I suggest you install <a href="http://kohanaphp.com/">Kohana</a> first if you have not already done so. Add Codebench to the list of activated modules in <code>application/config.php</code>.</p>
<p>Creating your own benchmarks is just a matter of creating a library that extends the Codebench class. Put the code parts you want to compare into separate methods. Be sure to prefix those methods with “bench”, other methods will not be benchmarked. Glance at <code>Valid_Color.php</code> in the download for a real example.</p>
<p>Here is another short example with some extra explanations.</p>
<pre lang="php">
// libraries/Ltrim_Digits.php
class Ltrim_Digits extends Codebench {

    // Some optional explanatory comments about the benchmark file.
    // HTML allowed. URLs will be converted to links automatically.
    public $description = 'Trimming leading digits: regex vs ltrim.';

    // How many times to execute each method per subject.
    // Total loops = loops * number of methods * number of subjects
    public $loops = 100000;

    // The subjects to supply iteratively to your benchmark methods.
    public $subjects = array
    (
        '123digits',
        'no-digits',
    );

    public function bench_regex($subject)
    {
        return preg_replace('/^\d+/', '', $subject);
    }

    public function bench_ltrim($subject)
    {
        return ltrim($subject, '0..9');
    }
}
</pre>
<p>And the winner is… <a href="http://php.net/ltrim"><code>ltrim</code></a>. Happy benchmarking!</p>
]]></content:encoded>
			<wfw:commentRss>http://geertdedeckere.be/article/introducing-codebench/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
