Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
48 posts Page 5 of 5
@KK

What if I want to display different names for <li> elements in #lang_selector?

I have that code in lang_switcher.html

Code: Select all
<cms:php>
      global $accepted_langs, $selected_lang;
      foreach( $accepted_langs as $lang ){
         $selected_class = ( $lang==$selected_lang ) ? ' selected' : '';
         echo'<li class="'.$lang.$selected_class.'"><a href="<cms:show k_admin_link />switch.php?lang='.$lang.'&redirect='.urlencode($_SERVER["REQUEST_URI"]).'"><span>'.strtoupper($lang).'</span></a></li>';
      }
      </cms:php>


but it shows me en/de/pl like I typed in config.
But I want full name like English/Polish.

In other places im using <cms:if but in this foreach I don't have idea how to achive that.
How about this?
<cms:php>
global $accepted_langs, $selected_lang;
foreach( $accepted_langs as $lang ){
if ($lang == 'en'){$language = 'English'};
if ($lang == 'de'){$language = 'German'};
if ($lang == 'pl'){$language = 'Polish'};


$selected_class = ( $lang==$selected_lang ) ? ' selected' : '';
echo'<li class="'.$lang.$selected_class.'"><a href="<cms:show k_admin_link />switch.php?lang='.$lang.'&redirect='.urlencode($_SERVER["REQUEST_URI"]).'"><span>'.strtoupper($language).'</span></a></li>';
}
</cms:php>
I've got syntax error:
Code: Select all
Parse error :  syntax error, unexpected '}' in D:\project\couch\tags.php(2866) : eval()'d code  on line  4



tim wrote: How about this?
<cms:php>
global $accepted_langs, $selected_lang;
foreach( $accepted_langs as $lang ){
if ($lang == 'en'){$language = 'English'};
if ($lang == 'de'){$language = 'German'};
if ($lang == 'pl'){$language = 'Polish'};


$selected_class = ( $lang==$selected_lang ) ? ' selected' : '';
echo'<li class="'.$lang.$selected_class.'"><a href="<cms:show k_admin_link />switch.php?lang='.$lang.'&redirect='.urlencode($_SERVER["REQUEST_URI"]).'"><span>'.strtoupper($language).'</span></a></li>';
}
</cms:php>
My mistake. I put the ";" in the wrong place.

<cms:php>
global $accepted_langs, $selected_lang;
foreach( $accepted_langs as $lang ){
if ($lang == 'en'){$language = 'English';}
if ($lang == 'de'){$language = 'German';}
if ($lang == 'pl'){$language = 'Polish';}


$selected_class = ( $lang==$selected_lang ) ? ' selected' : '';
echo'<li class="'.$lang.$selected_class.'"><a href="<cms:show k_admin_link />switch.php?lang='.$lang.'&redirect='.urlencode($_SERVER["REQUEST_URI"]).'"><span>'.strtoupper($language).'</span></a></li>';
}
</cms:php>
Automatically identify browser language during the first visit.


Slightly modified version:
Code: Select all
 <cms:php>
   global $accepted_langs, $selected_lang; // Initialize the language code variable
   
   // Set your languages here
   $accepted_langs = array( 'en', 'fi' );
   
   // Check to see that the global language in browser isset()
   // If it is set, we cut the first two characters from that string
   
   if( isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && in_array(strtolower( substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) ), $accepted_langs) ){
      $selected_lang = strtolower( substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) );
   }
   else{
      $selected_lang = 'fi'; // Set your fallback language here
   }
   
   // set it as a Couch variable
   global $CTX;
   $CTX->set( 'my_lang', $selected_lang, 'global' );   
</cms:php>
Hi
Because CouchCMS offers new ways of implementing method no 3 I wanted to ask if somebody could update the files (getter, menu, switch) to reflect changes like couchcms sessions, automatic language detection etc. I've tried to change the old code for the past 3 hours with no luck...
Also i noted that Safari doesn't keep the selected language when navigate to the home page like index.php, instead it switches to the default language - is that behaviour known? It works fine in Firefox and other browsers.
Thanks soo much :)
Since my last suggestion on 2014 is not working for the current version of couchcms.
Here I have another suggestion; as an example, there are 2 pages "en/lang.php" and "fr/lang.php"

Code for "en/lang.php";
Code: Select all
<cms:template title="Language Test">
    <cms:editable name="group_title" label="Title" desc="Page Title" type="group" group="group_desc">
        <cms:editable name="title_en" label="En Title" type="text" />
        <cms:editable name="title_fr" label="Fr Title" type="text" />
    </cms:editable>
</cms:template>
<cms:show title_en />


While for "fr/lang.php";
Code: Select all
<cms:php>$mylang="zhhant";</cms:php>
<cms:template title="Language Test (Trad.Chinese)" hidden="1" />
<cms:get_custom_field 'title_<?php echo $mylang;?>' masterpage='en/lang.php' />


So that our client can manage all the fields in the English template. and hide the Fr template from the admin panel.
For sure, it is much greater if we can have native language solution in the coming future.


KK wrote: Hi @gazzooid and welcome :)

Your approach is perfectly rational but, unfortunately, will not work in the current version of Couch.

The reason being that, the way things stand at 1.4, the format of the URLs is pretty much hardcoded and when Couch senses a deviation from the well known format, it issues a HTTP Redirect to what it considers as the correct URL.

We are in the process of changing this, though. The coming versions should allow user-configurable URLs. Your method would be perfectly feasible then.

Thanks for your input :)
@gazzooid
Please check the following (particularly the last section dealing with prettyURLs) -
viewtopic.php?f=5&t=10979
48 posts Page 5 of 5

Who is online

In total there are 2 users online :: 0 registered, 0 hidden and 2 guests
(based on users active over the past 5 minutes)

Users browsing this forum: No registered users and 2 guests