Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Data Model Questions

How to register Locale Values in i18n::get_common_locales()


Go to End
Reply

10 Posts   3980 Views

Avatar
kudesign

6 August 2009 at 11:49pm Community Member, 64 Posts

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!

Avatar
Juanitou

7 August 2009 at 3:43am Community Member, 323 Posts

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

Avatar
kudesign

9 August 2009 at 10:42pm (Last edited: 9 August 2009 10:42pm), Community Member, 64 Posts

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!

Avatar
joelg

21 August 2009 at 9:23am Community Member, 129 Posts

Hi Juanito & Kudesign

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

Anyway, thanks.

Joel

Avatar
Ingo

23 August 2009 at 12:42pm Forum Moderator, 801 Posts

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

Avatar
yurigoul

15 December 2009 at 6:44am Community Member, 202 Posts

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

Avatar
Lukin

21 October 2010 at 9:55pm Community Member, 42 Posts

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....

Avatar
banal

22 October 2010 at 1:11am Community Member, 901 Posts

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;
}

Go to Top