Two Plugin Updates: Basic Bilingual 0.32 and Language Linker 0.2 [en]

[fr] Je me suis levée à l'aube pour aller faire la nouille sur RSR1 et prendre un p'tit déj improvisé chez une ancienne copine d'uni. Ensuite, j'ai passé la journée les mains dans le PHP, ce qui veut dire que je n'ai beaucoup blogué, mais que j'ai mis à jour deux plugins: Basic Bilingual, qui permet de tenir sans peine un blog "bilingue" comme celui-ci (c'est ce qui me permet de rédiger et d'afficher ce petit extrait en français) et Bunny's Language Linker, très utile pour afficher des liens entre pages correspondantes des différentes traductions d'un site.

After waking up at an ungodly hour this New Year’s Day (for a live radio appearance and impromptu breakfast at a uni friend’s home nearby) I spent the rest of my day elbows deep in PHP code. As a result, I haven’t written the half-dozen of posts that have been sitting in my drafts list over Christmas, but I have updated two plugins — an old one, and a new-born.

Basic Bilingual 0.32

Download | zip | .phps

This release fixes the disappearing excerpts problem (was fixed in 0.31 actually, but I never announced it) and replaces the ugly “language box” floating somewhere near the top of the post admin page by a pretty DBX (let me know what it stands for) box in the sidebar:

Basic Bilingual got a dbx box for the new year!

Bunny’s Language Linker 0.2

Download | zip | .phps

(I always want to call it “Language Links”, which was the initial name I chose — still not sure I was right to change.) Anyway, this version is pretty exciting, as it does something I’ve been thinking of for a while: it puts the link to the other localized versions of the page you’re viewing in the menu bar if you’re using a Sandbox-based theme:

Language Linker link in the menu bar!

Otherwise, it puts it at the end of the page in its own div (you can style it the way you wish). I’m not saying this is the best, final solution, but I think it’s headed in the right direction.

Bunny's Language Linker: New WordPress Plugin [en]

[fr] Un nouveau plugin WordPress que je viens d'écrire. Celui-ci vous permet de gérer les liens entre pages équivalentes de deux versions linguistiques d'un site. Par exemple, si vous avez http://stephanie-booth.com/en et http://stephanie-booth.com/fr (deux installations WordPress séparées!), le plugin vous aidera à faire en sorte qu'il y ait des liens entre http://stephanie-booth.com/en/about et http://stephanie-booth.com/fr/a-propos.

Ladies and Gentlemen, I’m proud to announce the WordPress plugin Bunny’s Language Linker (zip, phps).

I’ve been wanting to write this plugin for ages, and I’ve finally done it this evening. This is a plugin for people who have a WordPress site with content duplicated in more than one language, like I’m going to have with stephanie-booth.com. For example, you have an “about” page in English, and another “about” page in German. This plugin helps you create and manage links between such “sister” pages. (“Pages”, not “posts”. It doesn’t work with posts at all.)

The plugin adds an extra field to the page editing form, inviting you to input the page slug of the sister page:

Bunny's Language Linker - Admin view

The screenshot is a bit small, but there on the right, there is a little box with “a-propos” — the slug of the French sister page. It works with more than one other language, too. You just need to edit the settings in the plugin file to specify which languages you’re playing with (instructions are in the plugin file). If I had sites in 3 other languages, say French, Spanish, and German, my settings line in the plugin file would look like this:

$bll_other_languages=array('fr', 'es', 'de');

And the little box would provide three different fields for the page slugs of the different localized sites. (OK, I’m making this sound complicated, sorry.)

The plugin then automatically adds links to the sister pages you’ve indicated. Here’s what it could look like:

Bunny's Language Linker - Page view

There’s a readme file with the plugin which will give you some more details. I’ll soon have a client site in production using that plugin, so if these explanations weren’t very clear, hopefully the demonstration will help.

Browser Language Detection and Redirection [en]

[fr] Une explication de la méthode que j'ai suivie pour que http://stephanie-booth.com redirige le visiteur soit vers la version anglaise du site, soit la française, en fonction des préférences linguistiques définies dans son navigateur.

Update, 29.12.2007: scroll to the bottom of this post for a more straightforward solution, using Multiviews.

I’ve been working on stephanie-booth.com today. One of my objectives is the add an English version to the previously French-only site.

I’m doing this by using two separate installations of WordPress. The content in both languages should be roughly equivalent, and I’ll write a WordPress plugin which allows to “automate” the process of linking back and forth from equivalent content in different languages.

What I did today is solve a problem I’ve been wanting to attack for some time now: use people’s browser settings to direct them to the “correct” language for the site. Here is what I learnt in the process, and how I did it. It’s certainly not the most elegant way to do things, so let me know if you have a better solution by using the comments below.

First, what I needed to know was that the browser language preferences are sent in the HTTP_ACCEPT_LANGUAGE header (HTTP header). First, I thought of capturing the information through PHP, but I discovered that Apache (logical, if you think of it) could handle it directly.

This tutorial was useful in getting me started, though I think it references an older version of Apache. Out of the horses mouth, Apache content negotiation had the final information I needed.

I’ll first explain the brief attempt I did with Multiviews (because it can come in handy) before going through the setup I currently have.

Multiviews

In this example, you request a file, e.g. test.html which doesn’t physically exist, and Apache uses either test.html.en or test.html.fr depending on your language preferences. You’ll still see test.html in your browser bar, though.

To do this, add the line:

Options +Multiviews

to your .htaccess file. Create the files test.html.en and test.html.fr with sample text (“English” and “French” will do if you’re just trying it out).

Then, request the file test.html in your browser. You should see the test content of the file corresponding to your language settings appear. Change your browser language prefs and reload to see what happens.

This is pretty neat, but it forces you to open a file — and I wanted / to redirect either to /en/ or to /fr/.

It’s explained pretty well in this tutorial I already linked to, and this page has some useful information too.

Type maps

I used a type map and some PHP redirection magic to achieve my aim. A type map is not limited to languages, but this is what we’re going to use it for here. It’s a text file which you can name menu.var for example. In that case, you need to add the following line to your .htaccess so that the file is dealth with as a type map:

AddHandler type-map .var

Here is the content of my type-map, which I named menu.var:

URI: en.php
Content-Type: text/html
Content-Language: en, en-us, en-gb

URI: fr.php
Content-Type: text/html
Content-Language: fr, fr-ch, fr-qc

Based on my tests, I concluded that the value for URI in the type map cannot be a directory, so I used a little workaround. This means that if you load menu.var in the browser, Apache will serve either en.php or fr.php depending on the content-language the browser accepts, and these two PHP files redirect to the correct URL of the localized sites. Here is what en.php looks like:

And fr.php, logically:

Just in case somebody came by with a browser providing neither English nor French in the HTTP_ACCEPT_LANGUAGE header, I added this line to my .htaccess to catch any 406 errors (“not acceptable”):

ErrorDocument 406 /en.php

So, if something goes wrong, we’re redirected to the English version of the site.

The last thing that needs to be done is to have menu.var (the type map) load automatically when we go to stephanie-booth.com. I first tried by adding a DirectoryIndex directive to .htaccess, but that messed up the use of index.php as the normal index file. Here’s the line for safe-keeping, if you ever need it in other circumstances, or if you want to try:

DirectoryIndex menu.var

Anyway, I used another PHP workaround. I created an index.php file with the following content:

And there we are!

Accepted language priority and regional flavours

In my browser settings, I’ve used en-GB and fr-CH to indicate that I prefer British English and Swiss French. Unfortunately, the header matching is strict. So if the order of your languages is “en-GB, fr-CH, fr, en” you will be shown the French page (en-GB and fr-CH are ignored, and fr comes before en). It’s all explained in the Apache documentation:

The server will also attempt to match language-subsets when no other match can be found. For example, if a client requests documents with the language en-GB for British English, the server is not normally allowed by the HTTP/1.1 standard to match that against a document that is marked as simply en. (Note that it is almost surely a configuration error to include en-GB and not en in the Accept-Language header, since it is very unlikely that a reader understands British English, but doesn’t understand English in general. Unfortunately, many current clients have default configurations that resemble this.) However, if no other language match is possible and the server is about to return a “No Acceptable Variants” error or fallback to the LanguagePriority, the server will ignore the subset specification and match en-GB against en documents. Implicitly, Apache will add the parent language to the client’s acceptable language list with a very low quality value. But note that if the client requests “en-GB; q=0.9, fr; q=0.8”, and the server has documents designated “en” and “fr”, then the “fr” document will be returned. This is necessary to maintain compliance with the HTTP/1.1 specification and to work effectively with properly configured clients.

Apache, Content Negotiation

This means that I added regional language codes to the type map (“fr, fr-ch, fr-qc”) and also that I changed the order of my language preferences in Firefox, making sure that all variations of one language were grouped together, in the order in which I prefer them:

Language Prefs in Firefox

Catching old (now invalid) URLs

There are lots of incoming links to pages of the French site, where it used to live — at the web root. For example, the contact page address used to be http://stephanie-booth.com/contact, but it is now http://stephanie-booth.com/fr/contact. I could write a whole list of permanent redirects in my .htaccess file, but this is simpler. I just copied and modified the rewrite rules that WordPress provides, to make sure that the correct blog installation does something useful with those old URLs (bold is my modification):

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . **/fr**/index.php [L]


# END WordPress

In this way, as you can check, http://stephanie-booth.com/contact is not broken.

Next steps

My next mission is to write a small plugin which I will install on both WordPress sites (I’ve got to write it for a client too, so double benefit). This plugin will do the following:

  • add a field to the write/edit post field in which to type the post slug of the correponding page/post in the other language *(e.g. “particuliers” in French will be “individuals” in English)
  • add a link to each post pointing to the equivalent page in the other language

It’s pretty basic, but it beats manual links, and remains very simple. (I like simple.)

As I said, if you have a better (simpler!) way of doing all this, please send it my way.

A simpler solution [Added 29.12.2007]

For each language, create a file named index.php.lg where “lg” is the language code. For French, you would create index.php.fr with the following content:

Repeat for each language available.

Do not put an index.php file in your root directory, just the index.php.lg files.

Add the two following lines to your .htaccess:

Options +Multiviews
ErrorDocument 406 /fr/

…assuming French is the default language you want your site to show up in if your visitor’s browser doesn’t accept any of the languages you provide your site in.

You’re done!

WordPress Deaf to Pings [en]

[fr] Mon installation WordPress semble refuser les pings depuis deux semaines environ. Aucune idée ce qui peut causer ça.

While I’m at it in the “technical annoyances instead of getting work done” department, with the misbehaving plugin and the Sandbox trouble, my WordPress installation has obviously become deaf to pings/trackbacks over the last two weeks.

I can send trackbacks fine, but not receive them. Even from my own blog. I don’t know where to start searching for the problem.

Oh, and I’ve lost the French excerpt to my post Advisors, Boards, Companies, Partners, Oh My! so if you happen to have a cached copy, would you check it out for me, please?

Damn. This morning is not turning out the way I hoped.

Update, 17:30: the pings from my most recent post just came through! I’m only running Spam Karma 2 now, deactivated both Akismet and Bad Behavior. Hope to identify the culprit soon.

Update, 17:53: now, when I save a post, it sends one ping. If there is more than one pingable URL in the post, I need to save it multiple times. Got bug?

Nasty Problem With Basic Bilingual Plugin [en]

[fr] Un problème avec le plugin Basic Bilingual qui fait disparaître les extraits dans "l'autre langue". Je bosse sur une solution (voir commentaires).

Heck. I just spent the last 15 minutes digging through the Google cache to retrieve “other language excerpts” which had been wiped from a good dozen of my recent posts. Not all of them, mind you — almost all of them. I haven’t yet managed to reproduce the problem, but clearly, the meta fields get reset in some circumstances.

I suspect it might be something that has to do with editing posts. Maybe related to the old disappearing tags problem?

In any case, I’m afraid Basic Bilingual must be misbehaving. Be particularly cautious when editing posts. Let me know if you have the same problem or a path to a fix — I’m working on it now.

Update: if somebody has the French excerpt to my post Advisors, Boards, Companies, Partners, Oh My! in their newsreader or browser cache, could you please send it to me or copy-paste it here as a comment? Thanks a lot.

Update, 17:30: I think I solved the problem (see comments) and corrected the files available on my server, bumping up the version to 0.31. Please download the latest version if you’re using this plugin.

WordPress Sandbox Theme Problems [en]

[fr] Deux problèmes avec Sandbox: les menus déroulants qui se déroulent décalés sur la droite dans IE, et l'absence de feuille de style pour l'impression. Toute aide bienvenue.

As you might have seen, Sandbox is now my theme of choice for WordPress. Diurnal, here on CTTS, is built upon Sandbox, and I’m using it with a client to build a new design from scratch. It’s a nice base to work from, in a CSS Zen Garden way.

However, there are problems. Here are two I’m stuck with on my client site. I posted them to the Sandbox forums, but I thought I’d mention them here in case one of you smart readers had an answer.

  1. No print stylesheet?: does anybody have a print stylesheet handy for use with Sandbox? If I can avoid writing one from scratch…
  2. Broken drop-down menus in IE: I’m far from a drop-down menu specialist, so I’m not sure where to start to fix the IE wonkiness I’ve noticed. The menus in IE do not drop right below the parent menu as shown here, but overlap on the neighbouring menu item on the right.

Thanks for any help or pointers you can bring me.

Ridding WordPress Plugins of Template Tags [en]

[fr] Cet article décrit une méthode permettant de se défaire des "template tags" qu'utilisent certains plugins. Plutôt que de copier le fameux template tag à 3 endroits différents dans son thème (comme c'était le cas auparavant pour Basic Bilingual), il est possible de modifier à peu de frais un plugin pour qu'il injecte automatiquement son contenu dans le blog.

If you’re like me and use a bunch of plugins to liven up your WordPress blog, you’ve probably noticed that adding template tags in your favourite (or favourite-of-the-week) theme files can quickly become a royal pain in the neck.

One of the things I wanted to do with Basic Bilingual, and which I did with the last release, was make the plugin inject the text for the “other excerpt” (the French text you can see at the top of this post) automatically into the templates and feeds.

Once I’d figured out how to do that, I realised I could modify other plugins, too. And I’m going to tell you how you can do it, too. This method should work for any plugin which generates a template tag, as long as you want the content generated by the plugin to go immediately after or before the post content. (I’m still working on advanced rules for cases where you want to make modifications elsewhere in the template.)

Christine‘s Inline Tag Thing put me on the track (thanks!), as it does just that. Here’s the trick:

  1. create a function which concatenates (= “adds”) the plugin output to the content
  2. add an action hook to the plugin to apply that function to “the_content”.

Fear not, I’ll explain all. You don’t need to really know much PHP to do this, as long as you’re comfortable wading through a bit of code and copy-pasting stuff and making a few small modifications.

Let’s take an example: the Similar Posts plugin, which I’m now using to point out posts related to the current one (normally, in a box at the top of the post if you’re on the website, and as a list at the end of the post if you’re reading the feed).

Similar Posts provides a template tag, <?php similar_posts(); ?>, which you can paste in your template where you like the related posts to appear. Personally, I want them to appear on the main blog page, on the individual archive pages, and on the monthly, category, and taggy archive pages. This means I need to paste the tag into at least 3-4 different template files. And if I decide that I want the tag at the top of the post rather than the bottom, or I decide to remove it, there I go again. Not optimal.

So, here’s how I made it “better” (for me):

A. First, I looked at the plugin code to figure out where the template tag function was; in this case, quite easy, it’s the function called similar_posts(), logically. Here are the last two lines of this function, which do the actual data output:

print $result;
if (defined('POC_CACHE')) print $cache_time;

I changed them to:

return $result;
if (defined('POC_CACHE')) return $cache_time;

So that they didn’t print directly (ie, output text), but just return the value of the data we want to insert.

Then, I created the following function:

function sp_embed_similar_posts($content) {
    $content = similar_posts() . $content;
    return $content;
}

This function takes one parameter ($content), and sticks the output of similar_posts() in front of it. Now you see why we needed to change the other function so that it didn’t print. We’re just modifying the value of the $content string. This new function returns the modified value for the content (in our case, imagine it as being the content of your post with the code for the similar posts listing stuck in front.

Now all that is left to do is to tell WordPress to actually use that function at some point (plugins are usually a collection of functions, followed by a series of “hooks” which actually execute the functions at certain chosen moments). Here’s the action we’ll use:

add_action('the_content', 'sp_embed_similar_posts');

Try it!

This will also add the list of similar posts to the feed.

So, in summary, here is what we have done:

  • modified the template tag function so that it returns a value instead of printing it (in some plugins, you’ll find a function that already does that!)
  • created a function to stick that value on the beginning or end of a parameter, $content
  • add an action hook on the_content to execute the function.

Happy hacking!

Tag-Related Wishlist [en]

[fr] Mes idées/prédictions/désirs pour l'évolution des tags et des technologies associées.

I told you my mind never stops spinning, didn’t I? Here are a few follow-up thoughts on my previous post on tags. View this as my brain dump of what I’d like tags to do in WordPress and around.

Bear in mind that I haven’t tried 2.3 yet, don’t know exactly what it does and doesn’t do, and haven’t done much homework. So (hopefully!) some of the stuff I’m speaking about here already exists. If it’s the case, please leave a note with a pointer in the comments.

Some of the stuff here might also be stupid. If it is, please tell me.

I don’t think all this should necessarily be in the WordPress core. Plugin makers, feel free to delve in here for inspiration. If I like your plugin, I’ll plug it.

Links Between Tagspaces

So, based on what I’ve understood, WP2.3 will provide a local tagspace. This means that if I tag a post “cat”, the link on that tag will take me to something like myblog.com/tags/cat. That’s cool.

But I want more.

I want the myblog.com/tags/cat page to contain configurable pointers to other tagspaces. For example, my Flickr photos tagged “cat”. My del.icio.us links. My videos. The Technorati tagspace.

See what I mean?

Somewhere, WordPress would ask me “What other tagspaces would you like links to?” and I’d enter “http://flickr.com/photos/bunny/tags/”, “http://del.icio.us/steph/”, “http://www.viddler.com/explore/steph/tags/”, “http://technorati.com/tag/” in some pretty form (we know how to do those now, don’t we?)

Alternate Tagspaces

Some people may not want to use the local tagspace. Hell, most people who tag their posts right now point to the Technorati tagspace. An option to do so could be nice.

Tag Combinations

I’d like my local WordPress tagspace to allow tag combinations. This is the stuff I wrote about nearly 3 years ago. Del.icio.us does it: here are my links tagged “books” AND “read”.

We need more of this, particularly if we start thinking multilingual. I want to be able to point to a page containing posts I tagged “adolescents” OR “teenagers” OR “ados” or “teens”. I use all those, but I’m sure (given the nature of tagging) some posts have slipped through the cracks and have only one or two of these tags.

Less multilingual, maybe I just want to have “cats” or “cat” (sometimes I use plural, sometimes singular, and the distinction isn’t important to me in this context).

Related Tags

Del.icio.us does this. My local tagspace pages should have this feature too.

And how about an option to be able to see (in a click) posts tagged “cat” AND all the posts tagged with one of the related tags? (This could become a bit unwieldy though.)

Tag Management

The “obvious” stuff. Rename tag “stephaniebooth” to “Stephanie Booth” everywhere it is. (Flickr does this well.) Merge tags. Add a bunch of tags to all the selected posts (result of a search or by-category selection). Remove tag X from all posts which are tagged Y.

This is the kind of stuff I wanted to make possible for categories when I wrote Batch Categories, a lifetime ago. I haven’t touched this “hack” for years now, and I’ve heard conflicting information about its compatibility with recent WordPress versions. I think somebody somewhere updated it for WP2.x — if you search you might find it.

Public Tagging

Now, this would be a source of tag spam, unless it’s for example limited to registered users of the blog, or people identified by OpenID or on a “trust list” (e.g. people who have commented on the blog before). I’ve encouraged people to open up tagging to the community on Flickr, and the feedback from those who have done it has been great. I’d like a way to do this for my blog posts, too.

I’m sure structured portable social networks have a part to play here.

More Importing/Conversion

Ages ago, I added keywords to my blog posts. (I now know it’s not very useful — maybe even, not at all.) Around the same time, I used Topic Exchange Channels for some of my posts, making the ITE channel visible on the post by adding a link to it (gosh, come to think of it — I hadn’t heard of tags yet, but what I was doing was some form of proto-tagging… quite impressed with myself!)

Anyway, leaving the self-congratulatory stuff aside, my wp_postmeta table contains old information about posts which has long since disappeared from this blog, but which is still there, ready to be recycled. I could turn those old keywords and ITE channels into tags with an importer.

So, how about a very “customizable” importer? I would give the meta field name I want to convert to tags, and indicate if the tag data is comma-separated, space-separated, or simply placed in multiple fields.

(For my old keywords, there is one meta field called “keywords” which contains a comma-separated list of words, whereas for the ITE channels there is one entry per channel called “ite_topic” (IIRC) with a unique word as a value — but there can be more than one channel per post.)

So, “manual importer”, anybody?

That’s All, Folks!

There, I think I’ve told you what was on my mind. Feedback welcome. And plugins. Code. Solutions.