Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#13 2009-07-26 14:07:11

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

Re: Bilingual website

mungy wrote:

Yes please. That does what I want, apart from tidy urls.

Ok. Note this is on a TXP 4.0.6 version (no use of <txp:variable /> tag).

1. Create a form named “browser_language_detection” and put this in it:

<txp:php>
session_start();
$langs = explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$_SESSION['language'] = $langs[0];
if($langs[0] == 'fr' || $langs[0] == 'fr-fr') {
	$_SESSION['language'] = 'fr';
} else {
	$_SESSION['language'] ='en';
}
$lang = $_SESSION['language'];
</txp:php>

Fine. You’ve got a session with browser language preferences in it (if the browser language isn’t one of the two choices above, $lang is set to ‘en” by default)

2. In the very begining of your “default” page template, add this:

<txp:output_form form="browser_language_detection" />

3. Now your can call the session to render what you want. Here is an example:

<txp:php>
if ($_SESSION['language'] == 'fr') {
echo output_form(array('form'=>'website_title_tag_fr'));
} else {
echo output_form(array('form'=>'website_title_tag_en'));
}</txp:php>

The corresponding “website_title_tag_en” form contains:

<txp:if_section name="blog"><txp:if_article_list><txp:page_title /><txp:else /><txp:title /> - </txp:if_article_list></txp:if_section><txp:if_section name="en"><txp:site_name /> - Jurançon nr Pau in the heart of South-West France<txp:else /><txp:if_category><txp:if_individual_article><txp:category title="1" /> : <txp:title /> - <txp:site_name /><txp:else /><txp:category title="1" /> - <txp:site_name /></txp:if_individual_article><txp:else /><txp:section title="1" /> - <txp:site_name /></txp:if_category></txp:if_section>
<txp:if_status status="404">Page not found</txp:if_status>

4. In this website, I’ve got 2 special sections. The “fr” one is for the French main page (which use the “default” template, and the “en” one is for the English main page (which use the “en” template. So you can associate the proper article into the corresponding section (by language).

5. My “language_menu” form contain this:

<ul id="flags-lang" class="no-print"><li><span><img src="<txp:site_url />img/icons/fr.png" width="16" height="11" alt="Version Française" /> <a href="<txp:site_url />fr" title="Version Française">Français</a></span></li><li><img id="flat-gb" src="<txp:site_url />img/icons/gb.png" width="16" height="11" alt="English Version" /> <a href="<txp:site_url />en" title="English Version">English</a></li></ul>

6. And to redirect visitors to the corresponding browser’s language, put this into your “default” template:

<txp:if_section name=""><txp:php>if($_SESSION['language'] == 'fr') { header ('Location: '.hu.'fr'); } else { header ('Location: '.hu.'en'); }</txp:php>

Well. Yuo’ve got a bi-lingual website now ;)

I think I’ve not omit something (I’d made this website 2 years ago, so I could forget something).

My advice. Now with the magic <txp:variable /> tag (this is a kind of feature I’d asked to Zem long time ago), you can manipulate a session more easily, based upon my “pourly” example.

Hope that’s can help you.

Best regards,


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#14 2009-07-26 14:32:18

net-carver
Archived Plugin Author
Registered: 2006-03-08
Posts: 1,648

Re: Bilingual website

Patrick

Nice solution. There have been a few complaints in the past about the MLP pack using the request’s HTTP_ACCEPT_LANGUAGE header to determine the browse language. Some people prefer it to be set by the incoming URL only.


Steve

Offline

#15 2009-07-26 16:13:59

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

Re: Bilingual website

@Steve

Sure. I think your MLP plugin is the only solution if you want to use a multi language website (more than 2). MLP offers lot of possibilities (I try it with pleasure). For the header language request, as you said, it depends on the choice of each.


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#16 2009-07-26 21:57:15

mungy
Member
Registered: 2006-03-12
Posts: 22

Re: Bilingual website

thank you Pat64. I’ve been too busy with housework to try anything today. hopefully I’ll get it up and running tomorrow with the aid of some coffee.

thanks everyone who has helped me so far. if you are ever in north wales, i owe you all a beer :D

Offline

#17 2009-07-27 11:07:49

mungy
Member
Registered: 2006-03-12
Posts: 22

Re: Bilingual website

I’m having problems with Patrick’s solution. How can I load the selected language using the <txp:variable> tag?

also.

<txp:if_section name=""><txp:php>if($_SESSION['language'] == 'fr') { header ('Location: '.hu.'fr'); } else { header ('Location: '.hu.'en'); }</txp:php>

is that complete or is there something missing.

to be honest, I’m confused now. :D

Offline

#18 2009-07-28 05:40:44

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,599
GitHub Twitter

Re: Bilingual website

@mungy

Look at txptips.com and specialy the latest Robert’s (Core Developer) tutorial . It’s great.

Now, you can create a form named “browser_language_detection” (type “misc”) and put this in it:

<txp:php>
variable(array('name' =>'accept-language', 'value' => 'en'));
$al = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
if (preg_match('/(.*?)[,;-]/', $al, $a)) {
    variable (
        array (
              'name' =>'accept-language', 
              'value' => $a[1]
        )
    );
}
</txp:php>

You’ve got now a <txp:variable named accept-language its value has the client’s browser language. It’s pretty usefull.

Call your browser_language_detection form at the very begining of your main page template (“default”):

<txp:output_form form="browser_language_detection" /><!DOCTYPE (...)

At the place of your choice add some kind of select lang list for your visitors:

<ul><li><a href="/en" title="English version">English</a></li><li><a href="/" title="Version française">Français</a></li></ul>

Create a section named “en” for your English home page. Add all needed <txp:article tags to grab your english articles only.

In your “default” template, add this line just before your final </head> html tag:

<txp:if_section name=""><txp:if_variable name="accept_language" value="en"><txp:php>
header ('Location: '.hu.'en');
</txp:php></txp:if_variable></txp:if_section>

In the code above, you test the variable value. If it contains “en” you redirect your visitors to the “en” section. Otherwise, you stay in the “default” one.

It’s the idea. Adapt it for you purpose. Remember you need to create separate sections for the specific parts of your site for each languages (only 2. See my previous post). Use the “default” template ONLY for the home page otherwise you could create an infinite redirection failure .

It is a solution. Perhaps someone could help you with another one. Ask to this forum. There are plainty of clever guys with lot of ideas here.

Best regards,

P.S. My latest advice. Did you think about your website organisation? I think it’s the FIRST thing to do. All your future decisions will depend of this very first step.

Last edited by Pat64 (2009-07-28 06:07:15)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#19 2009-07-28 16:22:47

mungy
Member
Registered: 2006-03-12
Posts: 22

Re: Bilingual website

thank you, once again patrick for spending time to help me understand.

Offline

#20 2009-07-30 19:35:25

mungy
Member
Registered: 2006-03-12
Posts: 22

Re: Bilingual website

Thanks guys for all your help.

In the end I have opted for a “very poor man’s” solution.

I am doing this using :-

<txp:section name="en">English</txp:section> - <txp:section name="cy">Cymraeg</txp:section>

to allow the user to select the language. For the time being I’m not worried about getting the browsers language, that was confusing me :D

I’m using 2 pages one containing the welsh and the other containing the english.

Once again, thank you for the help.

Pints all round!

Offline

#21 2010-01-23 14:42:10

ilyuha
Member
Registered: 2008-02-14
Posts: 10

Re: Bilingual website

Website in two languages. Determine the value of $HTTP_ACCEPT_LANGUAGE, and, depending on its value, given the content of one of the languages. How to assign a button on javascript, which will change the value of $HTTP_ACCEPT_LANGUAGE in browser? How to switch languages? With AJAX, cookies? Thanks! (sorry my English :)

form accept-language:

<txp:php> 
variable(array('name' =>'accept-language', 'value' => 'ru')); 
$al = @$_SERVER['HTTP_ACCEPT_LANGUAGE']; 
if (preg_match('/(.*?)[,;-]/', $al, $a)) { 
    variable ( 
        array ( 
              'name' =>'accept-language',  
              'value' => $a[1] 
        ) 
    ); 
} 
</txp:php>
page default:
<txp:output_form form="accept-language" /> 
<txp:if_variable name="accept-language" value="ru"> 
    _russian text_
<txp:else /> 
    _non russian text_
</txp:if_variable>

Offline

Board footer

Powered by FluxBB