Language / Locale Management

Language / Locale Management

This article presents the code necessary to have a list of links at the top of a view so that languages can be changed by the user. In addition, it provides the code to access locale information necessary to customize a site.

The context of these instructions will be the admin plugin, simply because that's the context in which the writer figured out how to make this work. This article will be modified for a non-admin context when he implements it. Caveat: This article was written after the fact and there are a lot of details. If you find an error, please either fix this article or leave a forum message in the Documentation category.

This article will not explain how this works, but only what to do to make it work. This information will be organized by the files that need to be created or changed. There will be links to the file listings.

Locale Files

Directory: config/locale/

You must define locale files for each combination of locale and language that you will need. In my application, I need support for Finland and Sweden. By way of illustration, I'm also creating locale information for the English language in the United States and in Canada. The Language/Locale Management that this article describes requires that some additional information be put into the files. Here is the original en.php with the needed changes.

$locale = array();
$locale['description'] = 'generic'; 
// 'description' used to refer to the language.  I decided to make it refer to the country because
// it's in the locale section.
 
// This is pre-existing information
$locale['charset'] = 'UTF-8';
$locale['date_time_format'] = 'Y-m-d H:i:s';
$locale['db_date_time_format'] = 'Y-m-d H:i:s';
$locale['date_format'] = 'Y-m-d';
$locale['long_date_format'] = 'Y-m-d';
$locale['time_format'] = 'H:i';
$locale['long_time_format'] = 'H:i:s';
$locale['first_day_of_week'] = 0; // 0 sunday, 1 monday
$locale['weekday_abbreviation'] = 1;
 
$locale['currency'] = array(
'precision'=>2,
'unit' => '$',
'unit_position' => 'left',
'separator'=> '.',
'delimiter' =>  ','
);
 
// This is additional locale management code
 
// You need to be able to tell what the current language/locale information is.
$locale['code'] = 'en'; // This is to be the same as the file name, but without the "php" extension.  
 
// This has no connection with the $dictionary array.  
//It is the default for use on forms to tell users what languages are spoken in a business.
$locale['languages'] = 'English'; 
 
//  Name and positions on add & edit forms 
//  These are to make forms with names and addresses appear as locals expect them regardless of the locale.
$locale['state designation'] = 'State'; // Could also be "Province" or "none"
$locale['state position'] = 'after city'; // Could also be "none"
$locale['zone designation'] = 'Zip code'; // Could also be "Postal Code" or "none"
$locale['zone position'] = 'after state'; // Could also be "before city" or "none"
 
$dictionary = array();
$dictionary['language'] = 'English';  // To be used to construct the language menu
// Following are the normal entries for the $dictionary.

Here are specific examples:

  • fi.php Finnish language in Finland.
  • sv.php Swedish language in Sweden.
  • sv_fi.php Swedish language in Finland.
  • en_us.php English language in the United States.
  • en_ca.php English language in Canada.

CSS Files

Directory: public/stylesheets/admin/

The admin plugin uses two CSS files, admin.css and menu.css. They have been replaced by new code because, among other things, we needed a horizontal menu for language selection.

config.php

Directory: config/

Make certain that these definitions are set for the languages / locales that you wish to use.

define('AK_AVAILABLE_LOCALES', 'en_us,en_ca,fi,sv_fi,es');
 
// Use this in order to allow only these locales on web requests
define('AK_ACTIVE_RECORD_DEFAULT_LOCALES', 'en_us,en_ca,fi,sv_fi,es');
define('AK_APP_LOCALES', 'en_us,en_ca,fi,sv_fi,es');
define('AK_PUBLIC_LOCALES', 'en_us,en_ca,fi,sv_fi,es');

admin_controller.php

Directory: app/controllers/

Keep in mind that the other controllers within the admin plugin extend this controller. That's why this code doesn't seem to be complete.

In the __construct() function, add this code at the end:

        $this->languages = array();
        $this->locale = array();
        $this->available_locales = explode(',',AK_AVAILABLE_LOCALES);
        $this->_changeLanguage();

You will also need to add the following function:

    function _changeLanguage()
    {
        foreach($this->available_locales as $available_locale) {
            $locale_file = AK_BASE_DIR.DS.'config'.DS.'locales'.DS.
                $available_locale.'.php';
            include($locale_file);
            $this->languages[] = $dictionary['language'];
            if (empty($this->params['language'])) {
                if($available_locale == AK::lang()) {
                    $this->locale = $locale;
                    $this->current_language = $dictionary['language'];
                }
            }else{
                if($this->params['language'] == $dictionary['language']) {
                    $this->current_language = $dictionary['language'];
                    $this->lang = $locale['code'];
                    $_SESSION['lang'] = $locale['code'];
                    AK::lang($locale['code']);
                    $this->locale = $locale;
                }
            }
        }
    }

admin controllers

Directory: app/controllers/admin/

These changes apply to all controllers created for the admin module. With a new admin plugin installation, you will have the following files: dashboard_controller.php, permissions_controller.php, roles_controller.php and users_controller.php.

In the index function, add this line before the redirectToAction or renderAction statements:

        $this->redirectToLocale($this->locale['code']);

Add the following function to each file:

 
    function change_language()
    {
        $this->_changeLanguage();
        $this->redirectTo(array('action'=>'index'));
    }

admin.tpl

Directory: app/views/layouts/

The first and third blocks of code are pre-existing. The second is new code.

      <div id="header">
          {?admin_settings-application_name}
            <div id="site_name"><h1>{admin_settings-application_name}</h1></div>
          {end}
 	  <div id="lang_menu">
	    <ul id="langlist">
            {loop languages}
                <?php if($current_language == $language) { ?>
                  <li id="active"><%= link_to language %></li>
                {else}
                  <li><%= link_to language, :action => 'change_language', :language => language %></li> 
                {end}
            {end}
            </ul>
	  </div>
          <div id="user_menu"><%= user_menu %></div>
          <div class="cls"></div>
      </div>

blank_slate.tpl

Directory: app/views/admin/dashboard/

This file doesn't really need to be changed, but to demonstrate that our code works, I added the following two lines after the “Thank you for signing up” line.

<h3>The language is _{language}.</h3>
<h3>The country is {locale-description}.</h3>
 
lang-locale-mgmt.txt · Last modified: 2009/01/12 17:10 by 82.103.203.179
 

The Akelos Framework was created by Bermi Ferrer and other contributors.
Potions of the code and documentation have been ported from Ruby on Rails.

The Akelos Framework is released under the LGPL license.

"Akelos", "Akelos Framework", and the Akelos logo are trademarks of Bermi Labs All rights reserved.

Wiki driven by DokuWiki