Themed translations (i18n)

When using themes you want to changes some of the translations specifically to the theme while keeping the standard translations for most of the text. Duplicating the main translation file is a headache for maintainance. This how to provides a solution.

Simply use:

<?php
/**
 * PHP Message source allowing messages to be specified in the theme.
 */
class ThemedPhpMessageSource extends CPhpMessageSource {
    private $_files;

    /**
     * (non-PHPdoc)
     * @see CPhpMessageSource::getMessageFile()
     */
    protected function getMessageFile($category, $language) {
        if(!isset($this->_files[$category][$language]))
        {
            if(($pos=strpos($category,'.'))!==false)
            {
                $moduleClass=substr($category,0,$pos);
                $moduleCategory=substr($category,$pos+1);
                if($moduleClass==='theme') {
                    $filename=Yii::app()->theme->getBasePath().DIRECTORY_SEPARATOR
                        .'messages'.DIRECTORY_SEPARATOR
                        .$language.DIRECTORY_SEPARATOR
                        .$moduleCategory.'.php';
                    if(file_exists($filename)) {
                        $this->_files[$category][$language]=$filename;
                    } else {
                        return;
                    }
                }
            } else {
                $filename=Yii::app()->theme->getBasePath().DIRECTORY_SEPARATOR
                      .'messages'.DIRECTORY_SEPARATOR
                      .$language.DIRECTORY_SEPARATOR
                      .$category.'.php';
                $parent=parent::getMessageFile($category,$language);
                if(file_exists($filename)) {
                    if("$parent"!="")
                        $this->_files[$category][$language]=array($parent,$filename);
                } else {
                    return $parent;
                }
            }
        }
        if(isset($this->_files[$category][$language])) {
            return $this->_files[$category][$language];
        } else {
            return parent::getMessageFile($category,$language);
        }
    }
}

and reference it in the configuration:

return array(
 //...
 'components'=>array(
                'messages'=>array(
                        'class' => 'ThemedPhpMessageSource',
                ),
  )
);

Then in your 'themes//messages' directory, add your themed messages as you do for the application and for widgets.

I have (for french):

  • themes/mytheme/messages/fr/app.php
  • themes/mytheme/messages/fr/admin.php
1 0
1 follower
Viewed: 16 968 times
Version: 1.1
Category: How-tos
Written by: le_top
Last updated by: le_top
Created on: Nov 22, 2014
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles