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.

General Questions /

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

i18n 99% there


Go to End
Reply


10 Posts   1109 Views

Avatar
novaweb

Community Member, 115 Posts

20 March 2012 at 3:05pm

Greetings fellow SilverStrippers,

I have a project I am working which requires internationalization.

First time using i18n, following the doc.

If I set i18n::set_locale in mysite/_config.php to "de_DE" - then I can see my translation successfully.

However if I change this back to "en_US" then try visit localhost/mywebpage?locale=de_DE then I don't see the translation.

I thought appending the locale parameter to the URL was meant to be the "language switcher"

Can you see any wrongdoings in my _config.php file?

// Set the site locale
i18n::set_locale('en_US');
date_default_timezone_set('Pacific/Auckland');
i18n::default_locale('en_US');
i18n::set_date_format('dd/MM/YYYY');
i18n::set_time_format('HH:mm');
Translatable::set_default_locale('en_US');

Cheers!

Avatar
swaiba

Forum Moderator, 1805 Posts

21 March 2012 at 2:02am

I thought appending the locale parameter to the URL was meant to be the "language switcher"

Not so, I believe there is a "small hack" to do this on the forum or the dev list. The correct was is to use the CMS to create teh page transations (using the special tab) and then including the <% control Transalations %> to show them in the template.

Try reading this...

http://www.ssbits.com/tutorials/2011/using-translatable-to-create-a-simple-multilingual-site/

Avatar
novaweb

Community Member, 115 Posts

21 March 2012 at 8:06am

Edited: 21/03/2012 9:21am

The content that I am attempting to translate is in the templates, not in the database. I am pretty sure I have to use i18n and not Translatable.

For anyone who attempts this, here is my solution:

Make sure i18n::set_locale() is not set in your mysite/_config.php, then in Page:

public function init() {
    parent::init();
    self::languageSwitcher();
   }
   
   
   function languageSwitcher() {
      if(isset($_GET['locale'])){
         $lang = $_GET['locale'];
         $_SESSION['locale'] = $lang;
         return i18n::set_locale($_SESSION['locale']);      
      }
      if(isset($_SESSION['locale'])){
         return i18n::set_locale($_SESSION['locale']);
      }
   }

Thanks guys!

Avatar
novaweb

Community Member, 115 Posts

21 March 2012 at 8:13am

Edited: 21/03/2012 11:41am

And this in your template:

<div id="LanguageSelector">
   <ul>
      <li><a href='{$URLSegment}?locale=en_US'>English</a></li>
      <li><a href='{$URLSegment}?locale=de_DE'>German</a></li>
   </ul>
</div>

Avatar
Josua

Community Member, 86 Posts

21 March 2012 at 10:49pm

Edited: 21/03/2012 11:08pm

Hi!

Unfortunately, this does not work very well with the hardcoded pages of SilverStripe (Security/login, Security/lostpassword, Security/passwordsent, ....).

See http://open.silverstripe.org/ticket/7056

Regards,
Jose

Avatar
novaweb

Community Member, 115 Posts

22 March 2012 at 7:09am

Hola Jose,

In my project I have used custom extensions on the login form etc, provided you do this you can make the login pages i18n enabled :)

Cheers,
Josh

Avatar
Josua

Community Member, 86 Posts

22 March 2012 at 8:12am

Hi Josh!

Where are those extensions? :)
Do you have created?

However, the problem is not only in these pages, other pages, for example, in the forum, that they are also hardcoded. :(

Thanks Josh,

Regards,
Jose

Avatar
novaweb

Community Member, 115 Posts

22 March 2012 at 8:29am

Hi Jose,

You need to create a CustomLoginPage, which just extends the normal Page class, with a Sign In form. This is just a normal form with EmailField and PasswordField.

The form's action should be doLogin

My Custom Form is a bit too large and customized to share on here, but the tricky part of the doLogin logic is as follows:

$authenticate = MemberAuthenticator::authenticate(array("Email"=>$data['Email'], "Password"=>$data['password']));
   if($authenticate){
      $authenticate->login();
// then do a redirect here
}

Good luck and let me know if you get stuck.

Go to Top