Welcome to Geeklog, Anonymous Saturday, November 30 2024 @ 11:45 am EST

Geeklog Forums

can anonymous users change the language?


cesar

Anonymous
Hi,
I need, in my site, allow to anonymous users change language.
Is it possible?
My GL version 1.8.1
Sorry by my english,
Thanks
 Quote

Status: offline

Roccivic

Forum User
Moderator
Registered: 05/19/10
Posts: 136
No, not out of the box. That said, here's a quick and dirty solution that will work with Javascript enabled.

Login as root

Download and install the autotags plugin: http://code.google.com/p/geeklog/downloads/detail?name=autotags_1.1.0_1.8.0.zip

Enable PHP code execution in autotags:
From "Admins only" menu select "Groups"
Find "Autotags Admin" and click "Edit"
Tick the box "autotags.PHP" and click "Save"

Create a new autotag:
From "Admins only" menu select "Autotags"
Click "Create new"
For "Tag" enter "langsel"
Tick the box near "Replace with PHP?"
Click "Save"

Open the file "system/lib-custom.php"
At the bottom of the file (but before the line "?>" ) add the below code and save the file.
Text Formatted Code
function phpautotags_langsel()
{
    global $_USER, $_CONF, $_SCRIPTS;

    if ($_USER['uid'] ) {
        $retval = "<a href='" . $_CONF['site_url'] . "/usersettings.php'>Change in profile</a>";
    } else {
        $_SCRIPTS->setJavascript('
            <script type="text/javascript">
                $(document).ready(function () {
                    $("#langsel").show().change(function () {
                        var selection = $("#langsel option:selected").val();
                        document.cookie = "' . $_CONF['cookie_language'] . '=" + escape(selection);
                        location.href = "";
                    });
                });
            </script>
        ');
        $language = MBYTE_languageList($_CONF['default_charset']);
        $retval  = '<noscript>This functionality requires JavaScript</noscript>' . LB;
        $retval .= '<form><div><select id="langsel" style="display: none">' . LB;
        foreach ($language as $langFile => $langName) {
            $retval .= '<option value="' . $langFile . '"';
            if ($langFile == COM_getLanguage()) {
                $retval .= ' selected="selected"';
            }
            $retval .= '>' . $langName . '</option>' . LB;
        }
        $retval .= '</select></div></form>';
    }
    return $retval;
}


Create a new block:
From "Admins only" menu select "blocks"
Click on create new
Enter "Language selection" as title and whatever you like for the name
For block content add:
Text Formatted Code
[langsel:]

Tick the box near "Autotags"
Save and logout

Done. You should now have a language selection block for anonymous users.
 Quote

Status: offline

::Ben

Forum User
Full Member
Registered: 01/14/05
Posts: 1569
Location:la rochelle, France
Hi,

This is also working as a block without the autotag step. I only changed $_USER['uid'] > 1

Text Formatted Code
function phpblock_langsel()
{
    global $_USER, $_CONF, $_SCRIPTS;

    if ($_USER['uid'] > 1 ) {
        $retval = "<a href='" . $_CONF['site_url'] . "/usersettings.php'>Change in profile</a>";
    } else {
        $_SCRIPTS->setJavascript('
            <script type="text/javascript">
                $(document).ready(function () {
                    $("#langsel").show().change(function () {
                        var selection = $("#langsel option:selected").val();
                        document.cookie = "' . $_CONF['cookie_language'] . '=" + escape(selection);
                        location.href = "";
                    });
                });
            </script>
        ');
        $language = MBYTE_languageList($_CONF['default_charset']);
        $retval  = '<noscript>This functionality requires JavaScript</noscript>' . LB;
        $retval .= '<form><div><select id="langsel" style="display: none">' . LB;
        foreach ($language as $langFile => $langName) {
            $retval .= '<option value="' . $langFile . '"';
            if ($langFile == COM_getLanguage()) {
                $retval .= ' selected="selected"';
            }
            $retval .= '>' . $langName . '</option>' . LB;
        }
        $retval .= '</select></div></form>';
    }
    return $retval;
}

Ben
I'm available to customise your themes or plugins for your Geeklog CMS
 Quote

Status: offline

Roccivic

Forum User
Moderator
Registered: 05/19/10
Posts: 136
Quote by: ::Ben

I only changed $_USER['uid'] > 1



Good catch Wink
 Quote

ironmax

Anonymous
Nice job guys!!!! I'll make this as part of the demo site so that new users will find it easier to navigate.

Michael
 Quote

Status: offline

::Ben

Forum User
Full Member
Registered: 01/14/05
Posts: 1569
Location:la rochelle, France
Yes it is a really good feature and it could become a default block on new Geeklog install Cool

Ben
I'm available to customise your themes or plugins for your Geeklog CMS
 Quote

cesar

Anonymous
thanks for all but i don't use java in my server.
may be a code for php?
i tride this but don't work.

Text Formatted Code
function phpblock_langsel()
{
global $_USER, $_CONF;
$id_language=$_POST['id_language'];
if ($_POST['id_language'])
  {
  $_CONF['cookie_language']=$id_language;
  }
if ($_USER['uid'] > 1 )
  {
  $retval = "<a href='" . $_CONF['site_url'] . "/usersettings.php'>Change in profile</a>";
  } else {
  $language = array_flip(MBYTE_languageList($_CONF['default_charset']));
  $retval .= "<form method='post' action='index.php'><select name='id_language'>";
  foreach ($language as $langName)
    {
    $retval .= '<option>'.$langName.'</option>';
    }
    $retval .= "</select><input type='submit' value='Choose'></form>";
  }
  return $retval;
  }
 


thanks
 Quote

Status: offline

Roccivic

Forum User
Moderator
Registered: 05/19/10
Posts: 136
The solution is in PHP, in fact no one said anything about Java. I mentioned JavaScript, which is a client side scripting language that most modern browsers have built-in, so you don't have to worry about that.

Anyway, read this: http://www.geeklog.net/article.php/20011231130438109
And then use the code from Ben's post.
 Quote

cesar

Anonymous
Ok, so it is an error in my server i think.
i think i know how to create a new php block because i've got a site where it uses more of 100 phpblocks but i'll say you what i did.

1-. Copy and paste the Ben's code in my system/lib-custom.php at the end but before ?>
2-. Create a new block: Title "Language selection" (i think it is no important but i do it), Type phpblock, in options of php block i write "phpblock_langsel" without (). Permisions all checked.
3-. Save

There are some part of code that not work and i don't know why.

1-. I exit of system and visit it like anonymous. (Before i clean cockies in firefox).
2-. Only show the tittle of block (Language selection)

If i delete, in Ben's code, the part
Text Formatted Code
style="display: none"
, my site show the box with languages. Only two (Spanish and English). That's wright because i only have these languages in my languages directory.

But i switch languages in box and it do nothing.

My site, under construction, www.lawebdelcamping.com
thanks for all.
 Quote

Status: offline

Roccivic

Forum User
Moderator
Registered: 05/19/10
Posts: 136
OK, i can see the problem on your site, you are missing jQuery.

Please put back:
Text Formatted Code
style="display: none"


And include jQuery:
Text Formatted Code
function phpblock_langsel()
{
    global $_USER, $_CONF, $_SCRIPTS;

    if ($_USER['uid'] > 1 ) {
        $retval = "<a href='" . $_CONF['site_url'] . "/usersettings.php'>Change in profile</a>";
    } else {
        $_SCRIPTS->setJavascriptLibrary('jquery'); // Add this line
        $_SCRIPTS->setJavascript('
            <script type="text/javascript">
                $(document).ready(function () {
                    $("#langsel").show().change(function () {
                        var selection = $("#langsel option:selected").val();
                        document.cookie = "' . $_CONF['cookie_language'] . '=" + escape(selection);
                        location.href = "";
                    });
                });
            </script>
        ');
        // etc...
 
 Quote

Status: offline

::Ben

Forum User
Full Member
Registered: 01/14/05
Posts: 1569
Location:la rochelle, France
Try this code below as jQuery library is not set on new install. Do not remove the display: none, It must be working (see demo here).

Text Formatted Code

function phpblock_langsel()
{
    global $_USER, $_CONF, $_SCRIPTS;

    $_SCRIPTS->setJavaScriptLibrary('jquery');

    if ($_USER['uid'] > 1 ) {
        $retval = "<a href='" . $_CONF['site_url'] . "/usersettings.php'>Change in profile</a>";
    } else {
        $_SCRIPTS->setJavascript('
            <script type="text/javascript">
                $(document).ready(function () {
                    $("#langsel").show().change(function () {
                        var selection = $("#langsel option:selected").val();
                        document.cookie = "' . $_CONF['cookie_language'] . '=" + escape(selection);
                        location.href = "";
                    });
                });
            </script>
        ');
        $language = MBYTE_languageList($_CONF['default_charset']);
        $retval  = '<noscript>This functionality requires JavaScript</noscript>' . LB;
        $retval .= '<form><div><select id="langsel" style="display: none">' . LB;
        foreach ($language as $langFile => $langName) {
            $retval .= '<option value="' . $langFile . '"';
            if ($langFile == COM_getLanguage()) {
                $retval .= ' selected="selected"';
            }
            $retval .= '>' . $langName . '</option>' . LB;
        }
        $retval .= '</select></div></form>';
    }
    return $retval;
}
 

Ben
I'm available to customise your themes or plugins for your Geeklog CMS
 Quote

cesar

Anonymous
Perfect.
thanks for all.
 Quote

Status: offline

Roccivic

Forum User
Moderator
Registered: 05/19/10
Posts: 136
I've created an improved solution for this issue.
The new solution also works without JavaScript and for logged-in users.
Please see here: http://www.geeklog.net/forum/viewtopic.php?showtopic=93490
 Quote

Status: offline

mocoli

Forum User
Junior
Registered: 02/10/12
Posts: 19
Location:USA
Quote by: Roccivic

I've created an improved solution for this issue.
The new solution also works without JavaScript and for logged-in users.
Please see here: http://www.geeklog.net/forum/viewtopic.php?showtopic=93490


It works fine for me, Thanks a lot this coding.
 Quote

All times are EST. The time is now 11:45 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content