Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » How to register Locale Values in i18n::get_common_locales()

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

Page: 1 2
Go to End
Author Topic: 3706 Views
  • kudesign
    Avatar
    Community Member
    64 Posts

    How to register Locale Values in i18n::get_common_locales() Link to this post

    v2.3.2 still trying to figure out a way to show available translation on the template of my page.

    Been following this doc : http://doc.silverstripe.com/doku.php?id=multilingualcontent&s=get%20common%20locales

    Under the code for Template, the doc mentioned: "Keep in mind that this will only show you available translations for the current page. The $Locale.Nice casting will just work if your locale value is registered in i18n::get_common_locales(). "

    Now, I got stuck on how you can register the locale value in i18n::get_common_locales()

    I did manage to find the file named"DBLocale.php, and this is what it has in it

    <?php
    /**
    * Locale database field, mainly used in {@link Translatable} extension.
    *
    * @todo Allowing showing locale values in different languages through Nice()
    *
    * @package sapphire
    * @subpackage i18n
    */
    class DBLocale extends Varchar {
       
       function __construct($name, $size = 16) {
          parent::__construct($name, $size);
       }

       function Nice() {
          return $this->getShortName();
       }
       
       function RFC1766() {
          return i18n::convert_rfc1766($this->value);
       }
       
       function getShortName() {
          $common_names = i18n::get_common_locales();
          return (isset($common_names[$this->value])) ? $common_names[$this->value] : false;
       }
       
       function getLongName() {
          return i18n::get_locale_name($this->value);
       }
    }
    ?>

    Now, does anyone know how I can register the locales for this? Please help! Thank you!

  • Juanitou
    Avatar
    Community Member
    322 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    Hi!

    Take a look to one of my _config.php files as example:

    /* Multilingual content */
    /********************/

    // Enable Translatable
    Object::add_extension('SiteTree', 'Translatable');
    Translatable::set_default_locale('fr_FR'); // This defines Translatable::current_lang() in Page.php

    // i18n
    i18n::enable();
    // Define allowed locales overriding those present in i18n::$common_locales
    global $allowed_locales;
    $allowed_locales = array(
          'fr_FR' => array('French', 'fran&ccedil;ais'),
          'en_GB' => array('English', 'English'),
          'es_ES' => array('Spanish', 'espa&ntilde;ol')
       );
    i18n::$common_locales = $allowed_locales;

    // Define defaults
    i18n::set_locale('fr_FR');
    setlocale(LC_ALL, 'fr_FR', 'fr_FR.UTF8', 'french'); // Needed for dates

    Hope it helps,
    Juan

  • kudesign
    Avatar
    Community Member
    64 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    Awesome!! Your example worked for me!! Thank you very very much!! This example should go into the SS doc about multi-lingual contents.

    So to SS tech reading this topic, please add this example in for future reference!

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    Hi Juanito & Kudesign

    This did the job for me to, but yes, the example should be placed in the SS doc.

    Anyway, thanks.

    Joel

  • Ingo
    Avatar
    Forum Moderator
    801 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    Hi there, I've added a short recipe for this in the documentation: http://doc.silverstripe.com/doku.php?id=multilingualcontent#add_new_locales

  • yurigoul
    Avatar
    Community Member
    202 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    setlocale(LC_ALL, 'fr_FR', 'fr_FR.UTF8', 'french'); // Needed for dates

    This code sets the date format for all languages. This would be very problematic when working with - lets say - German (dd.mm.yyyy) and American English (mm/dd/yyyy). Add Dutch (dd-mm-yyyy) to the mix and you have yourself a nice party, and then we are not even talking about numbers...

    My question: LC_ALL and setlocale: I can not find any data on this in the docs, I want to find out how to set dates for different languages. Anyone knows more about this or has any pointers?

    TIA

  • Lukin
    Avatar
    Community Member
    41 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    hey!

    has anybody figured out how to handle the lc_all story?
    I need to switch form german to english date (same format but, with ger/en month names) when switching language on m site
    I put setlocale (LC_ALL, 'de_DE@euro', 'de_DE.UTF-8', 'de_DE', 'de', 'ge'); into my language-file
    but didn't work.

    It would be possible to put it into the _config file but there seems to be no informatino about the current locale, so you can't
    manage the setlocale values with an if-clause or somethin...

    I wonder why it's so hard to find information about that point. It has to be important for many users....

  • banal
    Avatar
    Community Member
    901 Posts

    Re: How to register Locale Values in i18n::get_common_locales() Link to this post

    I've had issues with date formatting and LC_ALL. Therefore I used LC_TIME directly and it worked fine. E.g.

    setlocale(LC_TIME, 'de_DE', 'de_DE.UTF8', 'German', 'de-DE');

    To switch the locale used for dates, depending on the current site-translation, I suggest you do the following in your Page_Controller init method:

    // set the current locale based on the current translation language
    i18n::set_locale(Translatable::get_current_locale());

    switch(Translatable::get_current_locale()){
       case 'de_DE':
          setlocale(LC_TIME, 'de_DE', 'de_DE.UTF8', 'German', 'de-DE');
          break;
       case 'en_US':
          setlocale(LC_TIME, 'en_US', 'en_US.UTF8', 'English', 'en-US');
          break;
    }

    3706 Views
Page: 1 2
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.