Help needed with revised dynamic-locale plugin

I am revising the dynamic-locale plugin to display buttons rather than a pull down list. In this way I can then blend the buttons in with CSS to look like a typical pipelist. Check it out bottom left here: http://estudioa-2.com/

As you can see it works but the problem I have is that I cannot find the correct way to label the button. I want the button value (label) to be the 'language array key'/'language name' eg. Español. This however generates the error which says that the locale cannot be found on the server.

Now I suspect that this is because the getOption function called in the plugin is only appropriate for a select list and not for buttons. Is that assumption correct? How can I get around that - by writing another function to convert the value to the 'language array item'/'ISO code' ie. es_ES.

I include below the plugin in which, for simplicity, I also put the new functions rather than also hacking the functions and functions-i18n files. To toggle the error alternate between lines 46 and 47 (//if ($localize) $display = $key;...) . Please feel free to suggest more efficient methods or redundant code.

A second method I really would prefer to use is an unordered html list (UL) styled with CSS to make a flat/pipelist. I believe that to POST the locale selection would need a javascript call on anchor links around the language list items (LI). Can anyone illustrate to to do this alternate method correctly to avoid hassle with the buttons?

Of course if anyone can outline which of these two methods is best and why then I would be most grateful!

Mike

`
<?php
/**
* dynamic-locale -- plugin to allow the site viewer to select a localization.
* This applies only to the theme pages--not Admin. Admin continues to use the
* language option for its language.
*
* Only the zenphoto and theme gettext() string are localized by this facility.
*
* If you want to support image descriptions, etc. in multiple languages you will
* have to enable the multi-lingual option found next to the language selector on
* the admin gallery configuration page. Then you will have to provide appropriate
* alternate translations for the fields you use. While there will be a place for
* strings for all zenphoto supported languages you need supply only those you choose.
* The others language strings will default to your local language.
*
* Uses cookies to store the individual selection. Sets the 'locale' option
* to the selected language (non-persistent.)
*
* @author Stephen Billard (sbillard)
* @version 1.0.0
* @package plugins
*/
$plugin_description = gettext("Enable <strong>dynamic-locale to allow viewers of your site to select the language translation of their choice.");
$plugin_author = "Stephen Billard (sbillard)";
$plugin_version = '1.0.0';
$plugin_URL = "http://www.zenphoto.org/documentation/plugins/_plugins---dynamic-locale.php.html";

function generateButtonsFromArray($currentValue, $list, $descending, $localize) {
if ($localize) {
$list = array_flip($list);
if ($descending) {
arsort($list);
} else {
natcasesort($list);
}
$list = array_flip($list);
} else {
if ($descending) {
rsort($list);
} else {
natcasesort($list);
}
}
foreach($list as $key=>$item) {
echo "\n".'';
}
}

$_zp_active_languages = NULL;

function generateLanguageButtons($HTTPAccept) {
global $_zp_active_languages;
if (!is_array($_zp_active_languages)) {
$_zp_active_languages = generateLanguageList();
}
$locales = $_zp_active_languages;
if ($HTTPAccept) { // for admin only
$locales[gettext("HTTP Accept Language")] = '';
}
generateButtonsFromArray(array(getOption('locale', $HTTPAccept)), $locales, false, true);
}

/**
* prints a form for selecting a locale
* The POST handling is by getUserLocale() called in functions.php
*
*/
function printLanguageSelector($class='') {
global $_zp_languages;
if (isset($_POST['dynamic-locale'])) {
$locale = sanitize($_POST['dynamic-locale'], 0);
if (getOption('locale') != $locale) {
echo '
';
echo '

'.sprintf(gettext('%s is not available.'),$_zp_languages[$locale]).
' '.sprintf(gettext('The locale %s is not supported on your server.').
''.gettext('See the troubleshooting guide on zenphoto.org for details.'), $locale);
echo '

';
echo '
';
}
}
if (!empty($class)) { $class = " class='$class'"; }
echo "\n
\n";
echo ''."\n";
echo '';
generateLanguageButtons(false);
echo "\n";
echo "
\n";
}

?>
`

Comments

  • The 'option' must always be the language designator--the xx_XX version of the language.

    There is an array `$_zp_languages` which can be indexed by the language designator to give you the display name of the language.
  • Just as in the original dynamic-locale I have tried using the $_zp_languages to generate the button label (uncomment line 41 / comment line 42). However as, unlike an option list, buttons have no option and will submit that label. Thus I am guessing that an additional process is required to switch the label back to the ISO code xx_XX. Can you confirm my supositions and offer a little guidance on the easiest way to do that swap?
  • Firstly I didn't say many thanks for all your work and getting back to me so quickly.

    Apart from my comment above can you also help me understand how this form is handled? Is the hash a null or a page reload? I am sorry for the newbie questions but I am going round in circles and need someone to help me clear my head!

    Many thanks,
    Mike
  • Yes, you must switch back to the ISO code. That is what Zenphoto needs internally. You can use an array_flip of the $_zp_languages array and index it by the display language.

    With the original plugin, when you change the selection of the drop-down the form is posted. The "action" takes you back to the same page and the post is processed by the zenphoto setupCurrentLocale() function. It is looking for a POST of 'dynamic-locale'.

    So, somehow you need to make your buttons do a POST submit with a field of that name containing the ISO value of the language.
  • That's what I thought - the pointers you give are just what I needed.
    Thanks!
Sign In or Register to comment.