-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New feature: Possible for plugins to include their own locale files
- Loading branch information
1 parent
cc68dd6
commit 2b2ac33
Showing
3 changed files
with
117 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 comments
on commit 2b2ac33
There was a problem hiding this comment.
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 ;) )
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
$sLanguage = is_null($sLanguage) ? App()->language ;
then language of actual admin user is set by default. No ?