Localizing user fields (ie. gallery content rather than GUI)

dr_gaston

Joined: 2003-08-18
Posts: 4
Posted: Tue, 2003-09-09 03:56

This small patch to allow the users to enter multi-language text for all fields. This is done by using a special syntax:
default_text|lang_code1:localized_text1|lang_code2:localized_text2
Such as:
My vacation by the sea|fr_CA:My vacation by the sea|de_DE:Mein urlaub am rand des meeres

This string as an album title will display the English title unless the user has selected French, in which case the French title is displayed.

Simple and efficient, you can see an example at http://art.baudard.net.

Please apply the attached patch by going to the directory right above your gallery installation and typing the command:
patch -p0 < paseMLField.txt

Basically, what this patch does is add an extra function in 'util.php' to parse the user-entered fields before display:

/*
   This function extracts the translation of a user-entered field value
   Users must enter thetext in the following format:
      default_string|lang_code:translated string
   Example:
      My vacation by the sea|fr_CA:My vacation by the sea|de_DE:Mein urlaub am rand des meeres
   Will display the English text unless the current language is either fr_CA or de_DE
*/
function parseMLField($field) {
	global $gallery;
	
	// If no separator found, return untouched input
	$endpos = strpos($field,"|");
	if ($endpos == FALSE) return $field;

	// If no current language tag, return default string
	$begpos = strpos($field, "|".$gallery->language.":");
	if ($begpos == FALSE) return substr($field, 0, $endpos);
	
	// We've found a transtlation ...
	// skip lanbguage tag in itself
	$begpos = $begpos + 7;
	// go to end of translated string (ie. next tag or end of field)
	$endpos = strpos($field, "|", $begpos);
	if ($endpos == FALSE) $endpos = strlen($field);
	return substr($field, $begpos, $endpos-$begpos);
}

It then calls on this function before displaying any user-entered fields (gallery title, album title, descriptions, custom fields, ...) in the scripts 'albums.php', 'view_albums.php', 'view_photos.php', 'slideshow.php' and a few others in the 'html_wrap' directory:

-  <title><?php echo $gallery->app->galleryTitle ?></title>
+  <title><?php echo parseMLField($gallery->app->galleryTitle) ?></title>

-        <?php echo $gallery->app->galleryTitle ?>
+        <?php echo parseMLField($gallery->app->galleryTitle) ?>

-	    $caption = $gallery->album->getCaption($index);
+	    $caption = parseMLField($gallery->album->getCaption($index));

-	$buf = $album->fields[$field];
+	$buf = parseMLField($album->fields[$field]);

-        print "<center>".$gallery->album->fields["summary"]."</center>";
+        print "<center>". parseMLField($gallery->album->fields["summary"]) ."</center>";

Etc.

Enjoy ..... http://art.baudard.net for a demo

AttachmentSize
parseMLField.txt10.83 KB
 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Tue, 2003-09-09 11:21

Hello dr_gaston,

thanks for your effort.
Your solution looks nice, but _I_ think we cant use this for Gallery main code.

But the idea behind it is still very interesting and we wont forget it.
This is on my todo list for a later 1.4.1-cvs or maybe 1.4.2

wbR,

Jens

 
maledictus

Joined: 2005-03-08
Posts: 23
Posted: Tue, 2005-04-12 14:19

Hello there.

This patch is working for Gallery 1.4.4?

Thanks!

 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Tue, 2005-04-12 14:42

Hello,

this post was made 1,5 years ago :)
its definitely NOT working with 1.4.4.

Also i must correct my statement.
Its not longer on my ToDo for 1.x
This feature will never go in 1.x series.

Jens

 
maledictus

Joined: 2005-03-08
Posts: 23
Posted: Tue, 2005-04-12 14:52

Yeah I saw the date :lol: Just asking to be sure :wink:... Thanks!

 
maledictus

Joined: 2005-03-08
Posts: 23
Posted: Wed, 2005-04-13 16:23

Ok, I have a solution to gallery 1.4.4-pl6, using the idea of dr_gaston, I came with this solution... I am testing it, but with the titles of the albums works...

The idea:
Modify the editField function in util.php by adding some "parsing" functionality to it. What I do is to parse the field description. For example, a title of an album look like this:

"Default Language|en_US:Healthy Food|es_ES:Comida Sana"

then in the editField function I did this (it might not be the best of codes but hey! it works)

$lang = $gallery->language; //Get the gallery language
$lang .= ":";	      //Add colon to match the format I choose	
$arr_trans = explode('|',$album->fields[$field]);//Make an array
$x = count($arr_trans);//count translations
$flag = 0;    //a tiny little flag here
for($cont=0; $cont<$x; $cont++)//a loop to search and match the language the user choose
{
     if(strstr($arr_trans[$cont],$lang))  //Searchs for the language string
    {
       $title_lang = str_replace($lang, "",$arr_trans[$cont]); //Get the translation.
       $cont = $x; //stop the loop, there should be only one translation per language.
       $buf .= $title_lang; //add it to the $buf variable so it can do other stuff.
       $flag = 1; //change the flag.
    }
}								
if($flag == 0) //In case the translation wasn't found, display the default translation
	$buf .= $arr_trans[0];

I am testing it to find if this works on other fields that are modified by this function....

Here is the complete function.

function editField($album, $field, $link=null) {
	global $gallery;
	$buf = "";
	if ($link) {
		$buf .= "<a href=\"$link\">";
	}
	
		$lang = $gallery->language;
		$lang .= ":";															
		$arr_trans = explode('|',$album->fields[$field]);
		$x = count($arr_trans);
		$flag = 0;
		for($cont=0; $cont<$x; $cont++)
		{
			if(strstr($arr_trans[$cont],$lang))
			{
				$title_lang = str_replace($lang, "",$arr_trans[$cont]);
				$cont = $x;									
				$buf .= $title_lang;
				$flag = 1;				
			}								
		}
		if($flag == 0)
			$buf .= $arr_trans[0];
		
	if ($link) {
		$buf .= "</a>";
	}
	if ($gallery->user->canChangeTextOfAlbum($album)) {
		if (!strcmp($buf, "")) {
			$buf = "<i>&". _("Empty") . "&</i>";
		}
		$url = "edit_field.php?set_albumName={$album->fields['name']}&field=$field"; // should replace with & for validatation
		$buf .= " <span class=editlink>";
		$buf .= popup_link( "[". sprintf(_("edit %s"), _($field)) . "]", $url) ;
		$buf .= "</span>";
	}
	return $buf;
}

Of course any suggestions, improvements are always welcome! :D

 
Cryssli

Joined: 2005-05-04
Posts: 8
Posted: Fri, 2005-05-06 09:54

Any chance to get this working with 1.5?
And how could I apply it without being able to use ´patch´.

 
thibaud

Joined: 2005-07-26
Posts: 5
Posted: Tue, 2005-07-26 15:10

Will multilingual caption be a G2 feature ?

 
Tim_j
Tim_j's picture

Joined: 2002-08-15
Posts: 6818
Posted: Tue, 2005-07-26 22:16

thibaud: yes, according to bharat it already does.

jens