Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
d48ef419
Commit
d48ef419
authored
Jun 07, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor refactoring of DbMessageSource.
parent
0bdf369b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
14 deletions
+21
-14
DbMessageSource.php
framework/yii/i18n/DbMessageSource.php
+21
-14
No files found.
framework/yii/i18n/DbMessageSource.php
View file @
d48ef419
...
@@ -41,8 +41,6 @@ use yii\db\Query;
...
@@ -41,8 +41,6 @@ use yii\db\Query;
* the translated messages. The name of these two tables can be customized by setting [[sourceMessageTable]]
* the translated messages. The name of these two tables can be customized by setting [[sourceMessageTable]]
* and [[messageTable]], respectively.
* and [[messageTable]], respectively.
*
*
* When [[cachingDuration]] is set as a positive number, message translations will be cached.
*
* @author resurtm <resurtm@gmail.com>
* @author resurtm <resurtm@gmail.com>
* @since 2.0
* @since 2.0
*/
*/
...
@@ -68,18 +66,23 @@ class DbMessageSource extends MessageSource
...
@@ -68,18 +66,23 @@ class DbMessageSource extends MessageSource
*/
*/
public
$cache
=
'cache'
;
public
$cache
=
'cache'
;
/**
/**
* @var string the name of the source message table.
Defaults to `{{%source_message}}`.
* @var string the name of the source message table.
*/
*/
public
$sourceMessageTable
=
'
{{%source_message}}
'
;
public
$sourceMessageTable
=
'
tbl_source_message
'
;
/**
/**
* @var string the name of the translated message table.
Defaults to `{{%message}}`.
* @var string the name of the translated message table.
*/
*/
public
$messageTable
=
'
{{%message}}
'
;
public
$messageTable
=
'
tbl_message
'
;
/**
/**
* @var integer the time in seconds that the messages can remain valid in cache.
* @var integer the time in seconds that the messages can remain valid in cache.
* Defaults to 0, meaning the caching is disabled.
* Use 0 to indicate that the cached data will never expire.
* @see enableCaching
*/
*/
public
$cachingDuration
=
0
;
public
$cachingDuration
=
0
;
/**
* @var boolean whether to enable caching translated messages
*/
public
$enableCaching
=
false
;
/**
/**
* Initializes the DbMessageSource component.
* Initializes the DbMessageSource component.
...
@@ -96,7 +99,7 @@ class DbMessageSource extends MessageSource
...
@@ -96,7 +99,7 @@ class DbMessageSource extends MessageSource
if
(
!
$this
->
db
instanceof
Connection
)
{
if
(
!
$this
->
db
instanceof
Connection
)
{
throw
new
InvalidConfigException
(
"DbMessageSource::db must be either a DB connection instance or the application component ID of a DB connection."
);
throw
new
InvalidConfigException
(
"DbMessageSource::db must be either a DB connection instance or the application component ID of a DB connection."
);
}
}
if
(
$this
->
cachingDuration
>
0
)
{
if
(
$this
->
enableCaching
)
{
if
(
is_string
(
$this
->
cache
))
{
if
(
is_string
(
$this
->
cache
))
{
$this
->
cache
=
Yii
::
$app
->
getComponent
(
$this
->
cache
);
$this
->
cache
=
Yii
::
$app
->
getComponent
(
$this
->
cache
);
}
}
...
@@ -117,16 +120,20 @@ class DbMessageSource extends MessageSource
...
@@ -117,16 +120,20 @@ class DbMessageSource extends MessageSource
*/
*/
protected
function
loadMessages
(
$category
,
$language
)
protected
function
loadMessages
(
$category
,
$language
)
{
{
if
(
$this
->
cachingDuration
===
0
)
{
if
(
$this
->
enableCaching
)
{
return
$this
->
retrieveMessages
(
$category
,
$language
);
$key
=
array
(
}
else
{
__CLASS__
,
$key
=
static
::
CACHE_KEY_PREFIX
.
".messages.
{
$category
}
.
{
$language
}
"
;
$category
,
$language
,
);
$messages
=
$this
->
cache
->
get
(
$key
);
$messages
=
$this
->
cache
->
get
(
$key
);
if
(
$messages
===
false
)
{
if
(
$messages
===
false
)
{
$messages
=
$this
->
retrieveMessages
(
$category
,
$language
);
$messages
=
$this
->
loadMessagesFromDb
(
$category
,
$language
);
$this
->
cache
->
set
(
$key
,
$messages
,
$this
->
cachingDuration
);
$this
->
cache
->
set
(
$key
,
$messages
,
$this
->
cachingDuration
);
}
}
return
$messages
;
return
$messages
;
}
else
{
return
$this
->
loadMessagesFromDb
(
$category
,
$language
);
}
}
}
}
...
@@ -137,7 +144,7 @@ class DbMessageSource extends MessageSource
...
@@ -137,7 +144,7 @@ class DbMessageSource extends MessageSource
* @param string $language the target language.
* @param string $language the target language.
* @return array the messages loaded from database.
* @return array the messages loaded from database.
*/
*/
protected
function
retrieveMessages
(
$category
,
$language
)
protected
function
loadMessagesFromDb
(
$category
,
$language
)
{
{
$query
=
new
Query
();
$query
=
new
Query
();
$messages
=
$query
->
select
(
array
(
't1.message message'
,
't2.translation translation'
))
$messages
=
$query
->
select
(
array
(
't1.message message'
,
't2.translation translation'
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment