Skip to content

Commit

Permalink
New feature: Possible for plugins to include their own locale files
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Jul 25, 2016
1 parent cc68dd6 commit 2b2ac33
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 8 deletions.
56 changes: 56 additions & 0 deletions application/core/LSCGettextMessageSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Extension of CGettextMessageSource to allow plugins to have
* their own localization files
*
* @since 2016-07-25
* @author Olle Haerstedt
*/
class LSCGettextMessageSource extends CGettextMessageSource
{
const CACHE_KEY_PREFIX='Yii.LSCGettextMessageSource.';

/**
* 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
*/
public function loadMessages($category, $language)
{
// Default catalog to langauge (e.g. de)
// TODO: Where is catalog set (except default value)?
$this->catalog = $language;

$messageFile=$this->basePath . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . $this->catalog;
if($this->useMoFile)
$messageFile.=self::MO_FILE_EXT;
else
$messageFile.=self::PO_FILE_EXT;

if ($this->cachingDuration > 0 && $this->cacheID!==false && ($cache=Yii::app()->getComponent($this->cacheID))!==null)
{
$key = self::CACHE_KEY_PREFIX . $messageFile . "." . $category;
if (($data=$cache->get($key)) !== false)
return unserialize($data);
}

if (is_file($messageFile))
{
if($this->useMoFile)
$file=new CGettextMoFile($this->useBigEndian);
else
$file=new CGettextPoFile();
$messages=$file->load($messageFile,$category);
if(isset($cache))
{
$dependency=new CFileCacheDependency($messageFile);
$cache->set($key,serialize($messages),$this->cachingDuration,$dependency);
}
return $messages;
}
else
return array();
}
}
47 changes: 39 additions & 8 deletions application/libraries/PluginManager/LimesurveyApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
use SurveyDynamic;

/**
* Class exposing a Limesurvey API to plugins.
* This class is instantiated by the plugin manager,
* plugins can obtain it by calling getAPI() on the plugin manager.
*/
* Class exposing a Limesurvey API to plugins.
* This class is instantiated by the plugin manager,
* plugins can obtain it by calling getAPI() on the plugin manager.
*/
class LimesurveyApi
{
/**
Expand All @@ -24,20 +24,24 @@ public function getConfigKey($key, $defaultValue = false)
{
return App()->getConfig($key, $defaultValue);
}

/**
* Generates the real table name from plugin and tablename.
* @param iPlugin $plugin
* @param string $tableName
* @return string
*/
protected function getTableName(iPlugin $plugin, $tableName)
{
return App()->getDb()->tablePrefix . strtolower($plugin->getName()) . "_$tableName";
}

/**
* Sets a flash message to be shown to the user.
* @param html $message
*/
* Sets a flash message to be shown to the user.
*
* @param string $message HTML
* @return void
*/
public function setFlash($message, $key ='api')
{
// @todo Remove direct session usage.
Expand Down Expand Up @@ -389,5 +393,32 @@ public function pluginIsActive($name)
throw new Exception("Can't find a plugin with name " . $name);
}
}

/**
* Translation for plugin
*
* @param string $sToTranslate The message that are being translated
* @param string $sEscapeMode
* @param string $sLanguage
* @return string
*/
public function gT($sToTranslate, $sEscapeMode = 'html', $sLanguage = NULL)
{
$translation = \quoteText(Yii::t('', $sToTranslate, array(), 'pluginMessages', $sLanguage), $sEscapeMode);

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Nov 27, 2016

Collaborator

$sLanguage = is_null($sLanguage) ? App()->language ; then language of actual admin user is set by default. No ?


// If we don't have a translation from the plugin, check core translations
if ($translation == $sToTranslate)
{
$translationFromCore = \quoteText(Yii::t('', $sToTranslate, array(), null, $sLanguage), $sEscapeMode);

if ($translationFromCore != $sToTranslate)
{
return $translationFromCore;
}
}

return $translation;

}

}
22 changes: 22 additions & 0 deletions application/libraries/PluginManager/PluginBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public function __construct(\PluginManager $manager, $id)
$this->pluginManager = $manager;
$this->id = $id;
$this->api = $manager->getAPI();

// Set plugin specific locale file to locale/<lang>/<lang>.mo
\Yii::app()->setComponent('pluginMessages', array(
'class' => 'LSCGettextMessageSource',
'cachingDuration' => 3600,
'forceTranslation' => true,
'useMoFile' => true,
'basePath' => $this->getDir() . DIRECTORY_SEPARATOR . 'locale'
));
}

/**
Expand Down Expand Up @@ -249,4 +258,17 @@ protected function unsubscribe($event)
return $this->pluginManager->unsubscribe($this, $event);
}

/**
* To find the plugin locale file, we need late runtime result of __DIR__.
* Solution copied from http://stackoverflow.com/questions/18100689/php-dir-evaluated-runtime-late-binding
*
* @return string
*/
protected function getDir()
{
$reflObj = new \ReflectionObject($this);
$fileName = $reflObj->getFileName();
return dirname($fileName);
}

}

3 comments on commit 2b2ac33

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, must test in real plugin , but no time actually . But seems OK (for starting ;) )

@Shnoulle
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have it in develop version ? Because develop alpha release are in some month now.

@olleharstedt
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I was thinking about merging it. Can do tomorrow.

Please sign in to comment.