FORUMS CLOSED DUE TO SPAM. YOU STILL CAN ADD POST!

How I made multilanguage Frog sites with page parts

Feed 28 posts, 12 voices

Avatar
24 posts

I found using page parts are the most end user friendly way to have multi-language content and found a way to make them work.

First create a snippet called “check_locale”

<?php // Check and set locales

if(!isset($_SESSION['lang']))
{
	// get browsers preferred language
	if(!$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2))
	{
		$lang = 'en'; // default language
	}

	// assign language to our session
	$_SESSION['lang'] = $lang;
} 
elseif (isset($_GET['lang']))
{

	switch($_GET['lang']) {
		case 'en': // English
			$lang = 'en';
			break;
		case 'de': // German
			$lang = 'de';
			break;
		case 'sv': // Swedish
			$lang = 'sv';
			break;
		case 'da': // Danish
			$lang = 'da';
			break;
		case 'zh': // Chinese
			$lang = 'zh';
			break;
		default:
			$lang = 'en';
	}
	// pass a custom language to the session
	$_SESSION['lang'] = $lang;
}
?>

I included a number of languages as a default, but you can add whichever ones you want. Language codes are available here

Next include this snippet above your normal layout like so

<?php $this->includeSnippet('check_locale'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title><?php echo $this->title(); ?></title>

(...)

I then also edited my layout to display the specific language, should the session var and the page part exist. First, I commented out of the following:

<?php //if ($this->hasContent('extended')) echo $this->content('extended'); ?>

And added the following:

<?php if ($this->hasContent($_SESSION['lang'])) { echo $this->content($_SESSION['lang']); } else { echo $this->content(); } ?> 

This checks for the session variable and displays the page part should it exist, if it doesn’t it shows the default content (body).

I then created a hidden page called lang, with layout set to none with the following code:

<?php 

// Set locale
$this->includeSnippet('check_locale');

// Forward back to originating page
if($_SERVER['HTTP_REFERER'] != '') 
{ 
  header('Location: '.$_SERVER['HTTP_REFERER']); // Forward back
} else {
  header('Location: '.BASE_URL); // Forward home
}

?>

You can link to this page with something similar to this:

 <div><a href="<?php echo URL_PUBLIC; ?>lang/?lang=en">English</a> | <a href="<?php echo URL_PUBLIC; ?>lang/?lang=de">Deutsch</a> | <a href="<?php echo URL_PUBLIC; ?>lang/?lang=sv">Svensk</a></div>

It will automatically forward you back to the page in which you clicked it from, displaying your new language.

NOTES This does not solve the search engine issues, which I am totally open to suggestion for to allow Google to index the other language pages.

Please post fixes/bugs or suggestions here and I will update this post.

 
Avatar
1493 posts

Nice solution, defunct! This looks like it could be very useful.

I can spot two limitations (other than the Google thing), which do not detract from the usefulness of this solution “as-is”, but may be worth noting:

  • you will only ever get the English title, I suppose. But it would take something more radical to get to a multi-title solution.
  • having used the page-parts for languages, I don’t see a way of doing the read more thing, although maybe I haven’t had enough coffee yet today.

Meanwhile, thanks for the write-up! Looks good!

 
Avatar
257 posts

I think that the cookie option is a viable option in the short term, but long term maybe not

 
Avatar
24 posts

@David I’m glad you pointed that out…I never even considered it. Must be some work around for it, I’ll think about it. Now I know why a lot of people are using pages to do this.

 
Avatar
651 posts

@defunct – The snippet that checks for the browser’s preferred language is quite nice. It can be improved upon a bit and I would probably turn it into a Plugin (“Language Redirect” aka “lang_redir” anyone?). The plugin could then probably use the Observer system and the “page_requested” event to do some clever stuff like redirecting, etc…

Just a suggestion though. :-)

 
Avatar
1493 posts

A suggestion about NOT using the “extended” page-part for “Read more…” links is here.

I’m not sure how the thought below fits in with Martijn’s musings, but would this pictured suggestion be a Froggish way of accomplishing what this Wordpress plugin does?

 
Avatar
41 posts

A solution for the multilingual titles (just thinking out loud), could be using a similar approach of David’s suggestion of the read more without page parts.

You can create a page part called “titles”, enclose the titles with tags for each language (<de>Title</de>, etc or something similar), and in the Layout, modify:

$article->link($article->title)

With some function (sorry, I’m too bad on php) who reads the ‘titles’ page part. Mmmhh… I’ll take a look to that to see if I can write something that works.

 
Avatar
651 posts

Hmmmm… this is beginning to become increasingly more complex… For now, I will (personally) stick with using the copy-tree functionality to create a language specific subtree. Though I will be using defuncts function to auto-redirect the user to a new subtree based on the browser’s preferred language.

 
Avatar
1493 posts

@Pnikosis – I’m aiming for another beer… :P

IF you want to use defunct’s technique, and IF you want to have language-specific titles, you COULD do it this way:

- add another page-part called ‘titles’, and for each language page-part you have, add in the appropriate title this way:

UPDATE : the solution originally posted here wasn’t reliable. Go down a few posts to find Craig’s working code.

 
Avatar
41 posts

@David – You definitively won it, beware of your liver because at this pace I will have to invite you a full beer keg :P

 
Avatar
1493 posts

Don’t roll out the barrels too quickly, Pnikosis! On further testing, I don’t think that “titles” solution is working quite right. =/ Will report back….

 
Avatar
76 posts

Just a thought on David’s title posting…

Regular expressions might be quicker, and if in this sort of format too:

de: Auf Deutsch
fr: En Français
 
Avatar
1493 posts

@craig – are you around IRC just at the moment…?

 
Avatar
1493 posts

Right — solution from IRC. Craig cracked it! (That’ll be a pint for my friend, then! :P )

Use the page-part “titles”, with filter set to -none- and having this content:

de: Auf Deutsch
fr: En Français
it: Lingua Italiano
es: Si, Español

That is, with your language-specific title following their 2-character tag. Then, in your Layout where you expect the page title to come (the <h2>...</h2> bit in the default layout), put this code:

<?php if ($this->hasContent('titles')) : ?>
<h2>
<?php
$lang = $_SESSION['lang'];
$titles = $this->content('titles');
$res = preg_match('/'.$lang.':\s*(.*)/i', $titles, $results);
if($res == 1){
	$title = trim($results[1]);
	echo $title;
} else {
	echo $this->title();
} ?>
</h2>
<?php else : ?>
<h2><?php echo $this->title(); ?></h2>
<?php endif; ?>

And … it works! I’ll see about setting up a live example.* Craig says: “people can then turn it into a snippet if they like to keep it out of the layout” [from IRC]. And he’s quite right. I just dumped all that into a pageTitle snippet, called it in the Layout with

<?php $this->includeSnippet('pageTitle'); ?>

and all is as it should be! It certainly makes for a tidier layout.

Thanks, Craig! And thanks again, defunct, for setting this one rolling.

* Update: live example now online.

 
Avatar
76 posts

You’re welcome, happy to help out.

 
Avatar
41 posts

Fantastic. This will end in some kind of beerfest.
I think that this is the best solution if someone doesn’t want to duplicate their page trees. And again, this is a proof of the flexibility of Frog, you can do amazing stuff without the need of a ton of plugins.

 
Avatar
1493 posts

If anyone wants to see the defunct + Craig/David multilingual solution at work, have a look at this page (as long as it lasts: it’s a temporary test install), and use the language links in the footer to change the page.

There are, of course, some limitations to this technique, but I fully agree with Pnikosis!

I would be interested to know if anyone looks at the page whose browser locale is set to one of: de, fr, it, es — do let me know how it works for you in particular!

 
Avatar
24 posts

We need one final thing…displaying a translated menu.

I set my default language to German and it shows the German text by default.

 
Avatar
24 posts

@Martijn Oh yeah feel free to make/release a plugin for the language redirect :)

 
Avatar
1493 posts

We need one final thing…displaying a translated menu.

Done

:)

P.s. The menu code! It’s just a mash-up of Craig’s routine with the MadeByFrog style sidbar menu The class="current" bit even works.

<?php $lang = $_SESSION['lang']; ?>

<h3>Sidebar Menu</h3>

<ul id="nav_sub">
<?php foreach ($this->children() as $child): ?>
<?php if ($child->hasContent('titles')) {
  $titles = $child->content('titles');
  $res = preg_match('/'.$lang.':\s*(.*)/i', $titles, $results);
    if($res == 1) {
	$title = trim($results[1]);
	$childTitle = $title;
  } else { $childTitle = $child->title(); } 
} else { $childTitle = $child->title(); } ?>
    <li><?php echo $child->link($childTitle, (url_start_with($child->url) ? ' class="current"': null)); ?></li>
<?php endforeach; ?>
</ul>
 
Avatar
40 posts

I don’t know if I’m being rude, if you could make the above site avaible for download so people can have a look on how this exactly works. Me as a non back-end coder, finds this topic chaotic to read:D

thanks in advance! (and sorry if this is an insult to you…)

 
Avatar
1493 posts

Neither rude, nor insulting! It would be good to have this as an easier-to-follow tutorial. Consider it on the “to-do” list.

Where’s that wiki again…? :P

 
Avatar
17 posts

Wow since my last post with picture from WP plug-in, this is the best solution I ever seeing here!

All my studio sites where I now working are based on ModX and copy-tree functionality to create a language specific subtree. And i can say that that technique is terrible! Imagine, 300 documents in English, than you copy that tree, than you translate it, than you copy it again…

When the How-to will be available? ;)

 
Avatar
2 posts

Hey there!

I got an issue with the hidden “lang” page.
When i click on the links that set the lang parameter all I get is the code from “lang” in my browser.

I’ve taken all the code from first post and followed the instructions.

If I overstep the “lang” page and set the lang parameter myself it works fine.

Did I forget something?

Greetings,
Rumpirat! Arrgh!

 
Avatar
1 posts

I’m new with frog and download the version 0.9.5. I try to solve my issue with the multilangue support but i got stuck with the link to change view lang.

When i use the code you provide :
<a href="<?php echo URL_PUBLIC; ?>lang/?lang=en">English</a>

I got an error saying page not found
This is my local url :
http://www.monfrog.my/lang/?lang=fr

If i remove the lang/ from the url, the lang of my site change but i’m alway redirect to the home page and it’s not what i want.

Thanks for help