themes: add a language sensitive button themes: add a language sensitive button
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

themes: add a language sensitive button

Started by ripat, July 09, 2007, 11:30:45 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ripat

Hi,

I need to add a button in the sys_menu_buttons list that is language dependent. As I think that the language file is loaded after the theme page, I'll have to do the switching in my customized theme.php

I plan to do a check on the Accept-Language or User-Agent content of the http header sent by the client's browser. This is the easy part. But what if there is a language selector on the main page? Where is that value stored?

By the way, I had a look in the select_lang.inc.php and I am a little bit puzzeld by the following code (starting at row 123):

if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
} else if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}

if (!empty($_SERVER['HTTP_USER_AGENT'])) {
    $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
} else if (!empty($_SERVER['HTTP_USER_AGENT'])) {
    $HTTP_USER_AGENT = $_SERVER['HTTP_USER_AGENT'];
}


It doesn't make much PHP sense to me. Isn't it two time the same empty condition test?

Joachim Müller

Either add the button's code to the language files (Option 1) or use the var $CONFIG['lang'] (Option2) which holds the language actually used by the user (no matter if it was set using the language selectors or any other method). If you want to check specifically wether a user has selected a particular language (using the language selectors), overriding the default or auto-detected language, use $USER['lang']. When using from within a function, make sure to globalize the vars/arrays.




Code sample for option 1: edit /lang/yourlanguagefile.php, find$lang_main_menu = array(and add afterwards (into a new line)  'my_custom_button' => '<img src="path/to/localized/image.jpg" width="15" height="15" border="0" alt="" title="" />',. Use$lang_main_menu['my_custom_button']to access the button in theme.php.
Drawback: you'll need to re-apply this to all language files when updating the coppermine package.




Code sample for option 2:if ($CONFIG['lang'] == 'german') {
  $output = '<img src="path/to/localized/image/in/german.jpg" width="15" height="15" border="0" alt="" title="Mein benutzerdefinierter Menüpunkt" />';
} elseif ($CONFIG['lang'] == 'spanish') {
  $output = '<img src="path/to/localized/image/in/spanish.jpg" width="15" height="15" border="0" alt="" title="Mi menu espanol" />';
} else {
  $output = '<img src="path/to/localized/image/in/english.jpg" width="15" height="15" border="0" alt="" title="My custom menu item in English (or whatever your default language is)" />'; // default
}


ripat

I will go for option 2 in order to strictly keep my customization in the theme.php/html pages. But, strangely enough $CONFIG['lang'] returns english and $USER['lang'] --> french.

I will then use $USER['lang']. Thank you.

What about that funny logical test in select_lang.inc.php? Also, that language detection page is using eregi() function that slowly becomes obsolete and inefficient compared to the PCRE preg_match. Where can I make my suggestions to improve the code?

Joachim Müller

Quote from: ripat on July 09, 2007, 02:19:20 PM
I will go for option 2 in order to strictly keep my customization in the theme.php/html pages. But, strangely enough $CONFIG['lang'] returns english and $USER['lang'] --> french.
Try clearing your user-selection by going to http://yoursite.tld/your_coppermine_folder/?lang=xxx (alternatively, delete the cookies on your client).

Quote from: ripat on July 09, 2007, 02:19:20 PMWhat about that funny logical test in select_lang.inc.php?
Haven't looked into this recently. What's so funny about it?

Quote from: ripat on July 09, 2007, 02:19:20 PMAlso, that language detection page is using eregi() function that slowly becomes obsolete and inefficient compared to the PCRE preg_match. Where can I make my suggestions to improve the code?
Start a new thread on the misc board with a subject line like "[Suggestion for code review]: select_lang.php" or similar. A moderator will move it if applicable (and apply it to the core if your contribution is valid). We'd sure like to see your code suggestions and contributions, please go ahead.

Joachim

ripat

Quote from: GauGau on July 09, 2007, 04:44:24 PM
Try clearing your user-selection by going to http://yoursite.tld/your_coppermine_folder/?lang=xxx (alternatively, delete the cookies on your client).

OK. I'll test this.

Quote from: GauGau on July 09, 2007, 04:44:24 PMHaven't looked into this recently. What's so funny about it?

if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
} else if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}


Two times the same condition !empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])
Could simply be changed in:


if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
    $HTTP_ACCEPT_LANGUAGE = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
}


Or didn't I get the point?

Quote from: GauGau on July 09, 2007, 04:44:24 PMStart a new thread on the misc board with a subject line like "[Suggestion for code review]: select_lang.php" or similar. A moderator will move it if applicable (and apply it to the core if your contribution is valid). We'd sure like to see your code suggestions and contributions, please go ahead.

OK, I'll give it a go soon.

Nibbler


ripat

Almost done with the language cutomization of my theme.php. I only had to modify the value of $USER['lang'] with the GET['lang'] value when user clicks on a language flag or make a language selection in the <select> list.

// change $USER['lang'] if user hit the flag or language selector list
if (isset($_GET['lang'])){
  $USER['lang'] = $_GET['lang'];
}

// localize myButton
if ($USER['lang'] == 'french') {
  $myButton = 'html french.....';
} elseif ($USER['lang'] == 'dutch') {
  $myButton = 'html dutch...';
} else {
// default
  $myButton = 'Back to ';
}


But now, how to localize the content of template tokens like {GAL_DESCRIPTION} ?

Nibbler

Look at where they are replaced for example pageheader() and add another switch there.

ripat

That was it. Thank you. It is not easy to enter in someone else template logic but I am done now.

BTW, the Coppermine doc is one of the best I have seen so far for a OS package. Well done.

JL