CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Internationalization and Localization (Moderators: Michał Mach, mathieu) »
  • Unified Drupal & CivicRM Language Switcher
Pages: [1]

Author Topic: Unified Drupal & CivicRM Language Switcher  (Read 3111 times)

denjell

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7.0.1
  • MySQL version: 5.1.41
  • PHP version: 5.3.2-1ubuntu4.9 (10.4 64bit)
Unified Drupal & CivicRM Language Switcher
May 11, 2011, 04:56:16 am
As you may know the switching mechanism is not working for both systems. This thread will document my attempt to make a working block with php to solve this issue until it is resolved in civicrm core.

1) Description of the problem
Drupal switching does not switch the civicrm system, and similarly civicrm does not switch drupal. I believe this is the case because there is not a unified vision of language integration. I agree with the discussion that even on non-english sites the admin NEEDS to be using the English maintenance, however it seems to me that civicrm uses an "all-or-nothing" approach to language integration.

2) Details
When you enable a second language, a block is enabled that allows you to switch the civicrm language. This block uses javascript and appends the link with:

"[? or &]lcMessages=[selected language from list of enabled languages]";

Unfortunately, it does not check if lcMessages is already set, and will stupidly append so you get something like:

"?lcMessages=de_DE&lcMessages=en_US&lcMessages=de_DE" if you use the drop-box select multiple times, which is a common method when developing multilingual sites.

Furthermore, this "system" is never registered with drupal, so it is impossible for Drupal to force this, and the "Inherit CMS Language" in the Settings->Localization panel doesn't work at this time...

-----

this is the code in file LangSwitch.tpl:

{if isset($langSwitch) and $langSwitch|@count > 1}
  <form action="#">
    <select name="lcMessages" onchange="window.location='{$smarty.server.REQUEST_URI}{if strpos( $smarty.server.REQUEST_URI, '?' ) === false}?{else}&{/if}lcMessages='+this.value">
      {foreach from=$langSwitch item=language key=locale}
        <option value="{$locale}" {if $locale == $tsLocale}selected="selected"{/if}>{$language}</option>
      {/foreach}
    </select>
  </form>
{/if}


xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: Unified Drupal & CivicRM Language Switcher
May 11, 2011, 05:25:25 am
Hi,

you are right, can you provide a patch to check if lcMessages is already set and override it instead of always adding  ?

X+

P.S. I'm sure you are aware you can edit the url as a workaround to get rid of the extra lcMessages
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

denjell

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
  • CiviCRM version: 4.0.1
  • CMS version: Drupal 7.0.1
  • MySQL version: 5.1.41
  • PHP version: 5.3.2-1ubuntu4.9 (10.4 64bit)
Re: Unified Drupal & CivicRM Language Switcher
May 11, 2011, 10:09:58 am
this is a snippet for a block. it has two or three problems at the moment, and I see it as a temporary workaround, however it works well enough to help the staff not get confused when they are new to the site and need to switch the language quickly and globally.
unfortunately the link to civicrm (from the navigation link) does not respect the language as set, so I have removed that on my setting and added it here.



<?php
// Drupal 7.x & CivicRM 4.x Language Switcher
// This is hookless right now, but should look use translation_node_get_translations() to serve the translated node when asked for it...
// Used at http://kuenstlerhaus-weimar.de/en
error_reporting(0); //Uncomment this to hide any pesky notifications

global $user;

$civicrm = 0;
$civicrm = $_GET['lcMessages'];

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$targetUrl=$url;
$request=$_SERVER['REQUEST_URI'];
$drupalLANG = substr($_SERVER['REQUEST_URI'], 0,3);
$isCivicrm = substr($_SERVER['REQUEST_URI'], 3,9); // ="/civicrm/"
   switch ($drupalLANG) {
      case "/de": //is German -> Make link for English
         $drupalLang="de";
         $drupalLang2="en";
         $civicrmlang="de_DE"; //this is to pass so that we get logged into civicrm right!!!
         $locale1="Deutsch";
         $locale2="English";
         
         $request=substr_replace($request, "/en", 0,3); //Do Drupal Switch
         // here is where we should look up the dom after load
         //(for the translation page)

         if ($isCivicrm == "/civicrm/"){ // here we find out if we are in the civic domain...
            if ($civicrm) {
               $request=str_replace("lcMessages=de_DE", "lcMessages=en_US", $request);
               continue;
            }

            if(strstr($request, "?")) {
               $request=$request."&lcMessages=en_US";
            } else {
               $request=$request."?lcMessages=en_US";
            }
            
         }
      break;
      case "/en": //is English -> Make link for German
         $drupalLang="en";
         $drupalLang2="de";
         $civicrmlang="en_US";
         $locale1="English";
         $locale2="Deutsch";
         $request=substr_replace($request, "/de", 0,3); //Do Drupal Switch

         if ($isCivicrm == "/civicrm/"){ // here we find out if we are in the civic domain...
            if ($civicrm) {
               $request=str_replace("lcMessages=en_US", "lcMessages=de_DE", $request);
               continue;
            }

            if(strstr($request, "?")) {
               $request=$request."&lcMessages=de_DE";
            } else {
               $request=$request."?lcMessages=de_DE";
            }
            
         }
         
      break;   
      default: //is English?
         $drupalLang="en";
         $drupalLang2="de";
         $civicrmlang="en_US";
         $locale1="English";
         $locale2="Deutsch";
         $request=substr_replace($request, "/de", 0,0); //Do Drupal Switch
   }

//Link 1 -> Current Location with Flag (ACTIVE, not a link)
//Link 2 -> Future Location with Flag (PASSIVE, a link)
// get the flags here: http://drupal.org/files/issues/flags.tar_.gz
// untar here: /sites/all/libraries/
// I like /sites/all/libraries/flags/famfamfam & /sites/all/libraries/flags/famfamfam/plain
print '
<img src="http://'.$_SERVER["SERVER_NAME"].'/sites/all/libraries/flags/famfamfam/plain/'.$drupalLang.'.png">&nbsp;'.$locale1.'<br/>
<a id="translationPage" href="http://'.$_SERVER["SERVER_NAME"].$request.'" target="_self">
<img src="http://'.$_SERVER["SERVER_NAME"].'/sites/all/libraries/flags/famfamfam/'.$drupalLang2.'.png">&nbsp;'.$locale2.'
</a><br/>';
if ($user->uid) {
       if (!$civicrm == "/civicrm/") { //this will push the user to civicrm with the active language selected.
          print '<hr/><a href="http://'.$_SERVER["SERVER_NAME"].'/'.$drupalLang.'/civicrm/dashboard?lcMessages='.$civicrmlang.'" target="_self">CiviCRM</a>';
   }else{
      print '<hr/><a href="http://'.$_SERVER["SERVER_NAME"].'/'.$drupalLang.'" target="_self">Drupal</a>';
   }
}


?>


Hershel

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4640
  • Karma: 176
    • CiviHosting
  • CiviCRM version: Latest
  • CMS version: Mostly WordPress and Drupal
Re: Unified Drupal & CivicRM Language Switcher
May 20, 2012, 08:07:41 am
Quote from: denjell on May 11, 2011, 04:56:16 am
2) Details
When you enable a second language, a block is enabled that allows you to switch the civicrm language. This block uses javascript and appends the link with:

"[? or &]lcMessages=[selected language from list of enabled languages]";

Unfortunately, it does not check if lcMessages is already set, and will stupidly append so you get something like:

"?lcMessages=de_DE&lcMessages=en_US&lcMessages=de_DE" if you use the drop-box select multiple times, which is a common method when developing multilingual sites.

Furthermore, this "system" is never registered with drupal, so it is impossible for Drupal to force this, and the "Inherit CMS Language" in the Settings->Localization panel doesn't work at this time...

Seems both of these issues are still problems. Here's my shot at a more generic (and simpler) Universal Language Switcher Block:

Code: [Select]
<?php
// Setup language array. Done manually b/c Drupal and CiviCRM don't always
// match precisely. Default language has no key 
$languages = array(
  
'sq' => array('Shqip','sq_AL'),
  
'' => array('English','en_US'),
  
'mk' => array('Македонски','mk_MK'),
  
'sh' => array('Serbo-Croatian','bs-BA')
);
// Generate the true request URL, without the language prefix
$orig_request = $_SERVER['REQUEST_URI'];
if (
in_array(substr($orig_request,1,2),array_keys($languages))) {
  
$orig_request = substr_replace($orig_request,'',0,3);
}
// Generate links
foreach($languages as $k => $v) {
  
// Setup Drupal language prefix
  
if ('' != $k) {
    
$request = "/$k" . $orig_request;
  }
  else {
    
// Default language has no prefix
    
$request = $orig_request;
  }
  if (
'civicrm' == arg(0)) {
    
// Replace language setter in CiviCRM, if one exists. Presumes it's at the
    //  end of the URL
    
$request = preg_replace('/lcMessages=(.*)&?(.*)/','lcMessages='.$v[1],$request,-1,$count);
    
// If it wasn' there, then add it
    
if (0 == $count) {
      
$request = $request . '&lcMessages='.$v[1];
    }
  }
  
// print link
  
print '<a href="http://'.$_SERVER["SERVER_NAME"].$request.'">'.$v[0].'</a><br/>';
}
?>

CiviHosting and CiviOnline -- The CiviCRM hosting experts, since 2007

See here for the official: What to do if you think you've found a bug.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Internationalization and Localization (Moderators: Michał Mach, mathieu) »
  • Unified Drupal & CivicRM Language Switcher

This forum was archived on 2017-11-26.