Jump to:

23459 Posts in 18906 Topics by 2877 members

General Questions

SilverStripe Forums » General Questions » Current Locale in Language Select

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1979 Views
  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Current Locale in Language Select Link to this post

    Hi have a site using Translatable and am detecting the language using browser settings ($_SERVER['HTTP_ACCEPT_LANGUAGE']) this is working great so far my problem is my language select still shows English and won't select the current locale:

    <div id="Language">
       <dl id="Translations" class="langselect">
          <dt>
             <a href="#" hreflang="$Locale.RFC1766" title="$Title">
                <span><img class="flag" src="site/images/locale/{$Locale.RFC1766}.png" />$Locale.Nice</span>
             </a>
          </dt>
          <dd>
             <ul>
             <% control Translations %>
                <% if Locale.Nice %>
                <li class="$Locale.RFC1766">
                   <a href="$Link" hreflang="$Locale.RFC1766" title="$Title">
                   <span><img class="flag" src="site/images/locale/{$Locale.RFC1766}.png" />$Locale.Nice</span>
                   </a>
                </li>
                <% end_if %>
             <% end_control %>
          </ul>
          </dd>
       </dl>
    </div>

    This has been working fine until I forcibly have set the locale using the users browser settings it seems to always default to English (the default locale)

    Can anyone provide some advice please?

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Current Locale in Language Select Link to this post

    Just realised that only things using i18n are working as well so menus etc.. are still in English as well this is really strange, would it be better for me just to redirect the current page to the current locale eg: <URL Fragment>/?locale=es_ES ?

    Page.php init()

    //First try to detect browser preferred language
          if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
           $locales = array();
          
           $list = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
           foreach($list as $part) {
              $priority = '';
              @list($_locale, $priority) = explode(';', $part);
              if ($priority) {
                 $priority = substr($priority, 2);
              } else {
                 $priority = 1.0;
              }
             
              if (preg_match('/^([a-zA-Z]+)$/', $_locale, $match)) {
                 if (isset(i18n::$likely_subtags[$match[1]])) {
                   $_locale = i18n::$likely_subtags[$match[1]];
                 } else {
                   $_locale = self::default_locale();
                 }
              } else if (preg_match('/^([a-zA-Z]+)-([a-zA-Z]+)$/', $_locale, $match)) {
                 $_locale = $match[1] . '_' . strtoupper($match[2]);
              } else {
                 $_locale = self::default_locale();
              }
             
              if (in_array($_locale, $locales) == false) {
                 $locales[$priority] = $_locale;
              }
           }
          
           //Sort browser locales by priority
           krsort($locales);
          
           //Try to find best matching browser locale
           $langsAvailable = i18n::$common_locales; // from set from allowed locales in _config.php
          
           foreach($locales as $_locale) {
              if (isset($langsAvailable)) {
                 if (isset($langsAvailable[$_locale])) {
                   $locale = $_locale;
                   break;
                 }
              } else {
                 if (isset(i18n::$all_locales[$_locale])) {
                   $locale = $_locale;
                   break;
                 }
              }
           }
          }
          
          if(!empty($locale)) {
             i18n::set_locale($locale);
          } else {
             // set locale
             if($this->dataRecord->hasExtension('Translatable')) {
                i18n::set_locale($this->dataRecord->Locale);
             }
          }

  • CodeGuerrilla
    Avatar
    Community Member
    105 Posts

    Re: Current Locale in Language Select Link to this post

    One step closer I added:

    Translatable::set_current_locale($locale);

    if(!empty($locale)) {
             i18n::set_locale($locale);
             Translatable::set_current_locale($locale);
          } else {
             // set locale
             if($this->dataRecord->hasExtension('Translatable')) {
                i18n::set_locale($this->dataRecord->Locale);
             }
          }

    Now the menu's etc.. work still some fields are still showing in English and the langauge dropdown does not select current Locale, it seems the only way to display correctly is with the &locale=<Locale> on the URL

  • Chris_Bryer
    Avatar
    Community Member
    35 Posts

    Re: Current Locale in Language Select Link to this post

    hey CodeGuerilla,
    nice approach to presenting preferred language, i havent thought about that approach.. clever
    although you are setting a current locale, you are still presenting one specific record based on the url.. you may want to try getting the translation of the page.. try something like this (not tested):

    $currentLocale = [your locale calculation based on user preference];
    $translation = $this->getTranslation($currentLocale);
    Director::redirect($translation->Link());

    set_current_locale isnt strong enough to return translated records, it may affect javascript i18n if you are setting <html lang="$Locale">, but it doesnt translate the current record..

    hope it gets you somewhere
    -Chris

    1979 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.