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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

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


Go to End


10 Posts   6137 Views

Avatar
kudesign

Community Member, 64 Posts

6 August 2009 at 11:49pm

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

Community Member, 323 Posts

7 August 2009 at 3:43am

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

Community Member, 64 Posts

9 August 2009 at 10:42pm

Edited: 09/08/2009 10:42pm

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

Community Member, 134 Posts

21 August 2009 at 9:23am

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

Forum Moderator, 801 Posts

23 August 2009 at 12:42pm

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

Community Member, 203 Posts

15 December 2009 at 6:44am

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

Community Member, 56 Posts

21 October 2010 at 9:55pm

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
bummzack

Community Member, 904 Posts

22 October 2010 at 1:11am

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