Sunday, October 21, 2012

Re: Locale prefix

hi there,

I have mz main site where I can acces it as http://domain.com. for multi languages I made several subdomains, for example http://hr.domain.com, but subdomain is pointing into the same place as http://domain.com. Okay, now comes the cake part. Inside my settings file (app/config/settings.php) I have something like this:

//internationalization and localization
$config['locale_mappings'] = array(
         'at' => 'de_AT', 
         'de' => 'de_DE', 
         'ch' => 'de_CH',
         'hr' => 'hr_HR',
         ''   => 'eng',
         'www' => 'eng'
         );
Configure::write($config);

and in my app_controller.php:

function beforeFilter() {
    
    $this->recordActivity();

    $this->set('appConfigurations', Configure::read('App'));

    Configure::load('settings');
    $this->getLanguage();
}

function getLanguage() {
 
      // Now lest see what is happening with subdomain and domain
      $parsedUrl = parse_url($this->RequestHandler->getReferer(), PHP_URL_PATH);
           
      $host = explode('.', $parsedUrl);
      $this->set('parsedUrl', $parsedUrl);
      $this->set('host', $host[1]);

      if(count($host) > 2) {
        $subdomain = $host[0];
        $url_domain = $host[1].'.'.$host[2];
      } else {
        $subdomain = null;
        $url_domain = $host[0].'.'.$host[1];
      }
      
      //need this for facebook and twitter apps
      $this->set('subdomain', $subdomain);

      $lang_locale_mapping = Configure::read('locale_mappings', $subdomain);

      Configure::write('Config.language', $lang_locale_mapping[$subdomain]);

      // Set this to be visible accross the app
      $lang_locale_mapping = Configure::read('locale_mappings', $subdomain);
      if($this->layout == 'admin') {
        Configure::write('Config.language', $subdomain);
      } else {
        Configure::write('Config.language', $lang_locale_mapping[$subdomain]);
      }
      $this->set('lang', $lang_locale_mapping);
  }

and of course, the app/locale folder look like this:

app/locale/hr_HR/LC_MESSAGES/default.po

and that/s it! I hope you know how to extract the .po files with cake console (./i18n), after that this is piece of Cake :-)

Also, I presumed that you prepared all of your text inside app for translation!

Greets




On Mon, Oct 22, 2012 at 8:18 AM, Vanja Dizdarević <lex.non.scripta@gmail.com> wrote:
Oops, wrong example... Sooo:

domain.com/fr/admin/users

 = 

Config.language => fr
prefix => admin
controller => users
action => index


On Monday, October 22, 2012 8:17:04 AM UTC+2, Vanja Dizdarević wrote:
I am making my site multilingual, with English being the default language.

I wish to have domain.com/ display English, domain.com/{language_code (fr|de|sp)} display the complete app, with all its prefix routing in the selected language. So domain.com/fr/admin would translate to Config.language being fr and controller => admin, action=> index..

How do I achieve this? 

I have some ugly hacks in mind, but I would like to know if there is a more "conventional" way of doing this. How is this routing done on book.cakephp.org?

Cheers!

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 



--
Ivan Rimac
tel: +385 95 555 99 66

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: