Bilingual Publishing Without a Plugin
From Textbook
Note: the following examples can, in theory, be applied for trilingualism and, indeed, multilingualism. Actually, they would involve geometrically growing levels of operational complication. Rather, the proper way to tackle the task of building a site with more than two languages points to this feature request, with the Multilingual Publishing Pack as an elaborated and usable - although not flawless and 4.0.5 updated - answer. Stay tuned here for the latest discussions on it.
The following examples both point to what is called mirroring i.e. using pairs of articles pointing to each other. Please note that this solves only the core of the bilingual problem as there are many other important details (related to category and section titles, date and time formats and so on) not yet discussed here.
Both examples listed below are variations of one solution, involving the use of custom fields and conditional tags.
Solution no. 1
Minutely explained here, is about using the urls of the mirrored articles, a number of custom fields equal to the languages used (two in this case) and two txp tags to obtain the link for mirroring.
Solution no. 2
It is explained here and more explicitly below:
It uses one custom field and two forms. Basically one has two articles: first has the id=1 and is written in English and the other has the id=2 and is written in Romanian. The purpose is to post on the website a link to the other article.
(For more discipline, the English and Romanian content can be separated in different sections. Let's say we have the "home" section paired with the "english" section (and it could go on like this with other pairs.) Article id=1 is assigned to section "home" and article id=2 is assigned to section "english."
One way of creating a link in textpattern is to use the<txp:article_custom />tag together with a form (named for example "link") containing
<txp:permlink>link text</txp:permlink>. Thus to link to the article with the id=1 we use
<txp:article_custom id="1" form="link" />In our case to link from article 1 to article 2 we would have to use
<txp:article_custom id="2" form="link"/>and the reciprocal to link back. Also, the "link" form would have to be:
<txp:if_section name="english"> <txp:permlink>Read in Romanian</txp:permlink> <txp:else /> <txp:permlink>Read in English</txp:permlink> </txp:if_section>
Now, to generalize, we use a custom field, naming it "ro-en", and fill it with the id of the mirrored article. Thus article 1 has "2" in its "ro-en" custom field, and article 2 has "1" in its "ro-en" custom field.
The ideal tag would look like this:
<txp:article_custom id="<txp:custom_field name="ro-en">" form="link" />
But textpattern does not support tags in tags. To overcome this we adapt this slick php trick to look like this:
<txp:php>
$a = custom_field (array ('name' => 'ro-en'));
echo article_custom (array(
'id' => $a,
'form' => 'link'
));
</txp:php>
and we put it in a form, not before containing it in a conditional that will let the link show only if the "ro-en" has any content. We name the form "lang":
<txp:if_custom_field name="ro-en">
<txp:php>
$a = custom_field (array ('name' => 'ro-en'));
echo article_custom (array(
'id' => $a,
'form' => 'link'
));
</txp:php>
</txp:if_custom_field>
That's it. Now, to get the mirroring link, we put the following tag wherever we want to show the reader to the other language version of an article:
<txp:output_form form="lang" />
You can see this last solution at work here
Solution no. 3
Textpattern developer Robert Wetzlmayr came up with this following solution on his blog, which makes use of
<txp:variable />
and therefore requires textpattern 4.0.7 and upwards.
The idea is to put all localized renditions of a text into just one article and let Textpattern conditionally render either one or the other part of it, depending on the user-agent’s Accept-Language preference.
A form wisely named accept-language establishes a default language preference, tries to sniff out the visitor’s language preferences subsequently, and finally stores the resulting language code into a Textpattern variable:
<txp:php>
variable(array('name' =>'accept-language', 'value' => 'de'));
$al = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (preg_match('/(.*?)[,;-]/', $al, $a)) {
variable (
array (
'name' =>'accept-language',
'value' => $a[1]
)
);
}
</txp:php>
This form is placed somewhere above the content in question, either in the page’s template or at the very beginning of the article. Inside the article’s body, each localized fraction is surrounded by
<txp:if_variable name="accept-language" value="LANGCODE">
tags. LANGCODE is one of the ISO languages codes like de, en, fr.
<txp:output_form form="accept-language" /> <txp:if_variable name="accept-language" value="de"> German content goes here... <txp:else /> Non-german content goes here... </txp:if_variable>
That’s all. For a live example, try visiting this article with different language preferences. All settings but de-* would render an English version.




