Internationalization And Localization Of Translation Files

Hi,

I would like to have such a set up and i would like to find out if its possible with Yii:

  • have a default English translation file that contains 100 keys, i.e.



return array(

   'page.title' => 'About Us',

   'anotherPage.title' => 'Welcome to Another Page',

);



  • have a localized translation file (e.g. en_HK) but only contains those keys that are different from the default file, i.e.



return array(

   'anotherPage.title' => 'Another Page',

);



If I set the language to en_HK and use


Yii::t('app', 'page.title')

and its should show ‘About Us’ instead. Something like an array_merge that overrides the default translation.

I have read the internationalization http://www.yiiframework.com/doc/guide/1.1/en/topics.i18n but it seems that the default translation are stored at the views.

I would like to know if its possible using the existing Yii component or if i have to resort to custom codes to achieve it.

Thanks,

11

Because message files just return an array, you can actually load a common file there, merge it with overrides and return it. No magic needed here :slight_smile:

Thanks nineinchnick!

I actually figured it out. Here’s my codes for those who may need it:

In protected/config/main.php, set the sourceLanguage and default language. Also, tell messages component to use our derived class:




return array(

	'sourceLanguage'=>'00',

	'language'=>'en_hk',

	'components'=>array(

		'messages' => array(

        		'class'=>'MessageSource',

        	),

	),

);



I created a custom component, MessageSource to retrieve both the specified local translations and the default master translations. I then merge these 2 arrays together in a way that the local values will override the default values on those same keys. The component is extended from CPhpMessageSource. Thereafter i overload the loadMessages functionality. The getMessages is actually the same as the CPhpMessageSource’s loadMessages.

protected/components/MessageSource.php:




<?php 


class MessageSource extends CPhpMessageSource 

{

	/**

	 * Loads the message translation for the specified language and category.

	 * @param string $category the message category

	 * @param string $language the target language

	 * @return array the loaded messages

	 */

	protected function getMessages($category,$language)

	{

		$messageFile=$this->getMessageFile($category,$language);


		if($this->cachingDuration>0 && $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)

		{

			$key=self::CACHE_KEY_PREFIX . $messageFile;

			if(($data=$cache->get($key))!==false)

				return unserialize($data);

		}


		if(is_file($messageFile))

		{

			$messages=include($messageFile);

			if(!is_array($messages))

				$messages=array();

			if(isset($cache))

			{

				$dependency=new CFileCacheDependency($messageFile);

				$cache->set($key,serialize($messages),$this->cachingDuration,$dependency);

			}

			return $messages;

		}

		else

			return array();

	}

	

	/**

	 * Loads the message translation for the specified language and category and merged it with the default.

	 * @param string $category the message category

	 * @param string $language the target language

	 * @return array the merged messages array

	 */

	protected function loadMessages($category,$language) 

	{

		// Loads the message translation for the specified language and category.

		$localizedMessages = $this->getMessages($category,$language);

		

		// Loads the message translation for the default language and category.

		$defaultMessages = $this->getMessages($category, Yii::app()->sourceLanguage);

		

		return CMap::mergeArray($defaultMessages,$localizedMessages);

	}

}