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
ec1c85bb
Commit
ec1c85bb
authored
Jan 21, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2008 from kate-kate/db-message-command
added possibility to save messages to be translated
parents
9eb164f4
6ea98bf3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
7 deletions
+106
-7
MessageController.php
framework/console/controllers/MessageController.php
+104
-5
DbMessageSource.php
framework/i18n/DbMessageSource.php
+1
-1
config.php
framework/messages/config.php
+1
-1
No files found.
framework/console/controllers/MessageController.php
View file @
ec1c85bb
...
...
@@ -16,8 +16,8 @@ use yii\helpers\FileHelper;
/**
* This command extracts messages to be translated from source files.
* The extracted messages are saved either as PHP message source files
* or ".po" files under the specified directory
. Format depends on `format`
* setting in config file.
* or ".po" files under the specified directory
or to a database. Format
*
depends on `format`
setting in config file.
*
* Usage:
* 1. Create a configuration file using the 'message/config' command:
...
...
@@ -91,14 +91,16 @@ class MessageController extends Controller
if
(
!
is_dir
(
$config
[
'sourcePath'
]))
{
throw
new
Exception
(
"The source path
{
$config
[
'sourcePath'
]
}
is not a valid directory."
);
}
if
(
in_array
(
$config
[
'format'
],
[
'php'
,
'po'
]))
{
if
(
!
is_dir
(
$config
[
'messagePath'
]))
{
throw
new
Exception
(
"The message path
{
$config
[
'messagePath'
]
}
is not a valid directory."
);
}
}
if
(
empty
(
$config
[
'languages'
]))
{
throw
new
Exception
(
"Languages cannot be empty."
);
}
if
(
empty
(
$config
[
'format'
])
||
!
in_array
(
$config
[
'format'
],
[
'php'
,
'po'
]))
{
throw
new
Exception
(
'Format should be either "php"
or "po
".'
);
if
(
empty
(
$config
[
'format'
])
||
!
in_array
(
$config
[
'format'
],
[
'php'
,
'po'
,
'db'
]))
{
throw
new
Exception
(
'Format should be either "php"
, "po" or "db
".'
);
}
$files
=
FileHelper
::
findFiles
(
realpath
(
$config
[
'sourcePath'
]),
$config
);
...
...
@@ -107,7 +109,7 @@ class MessageController extends Controller
foreach
(
$files
as
$file
)
{
$messages
=
array_merge_recursive
(
$messages
,
$this
->
extractMessages
(
$file
,
$config
[
'translator'
]));
}
if
(
in_array
(
$config
[
'format'
],
[
'php'
,
'po'
]))
{
foreach
(
$config
[
'languages'
]
as
$language
)
{
$dir
=
$config
[
'messagePath'
]
.
DIRECTORY_SEPARATOR
.
$language
;
if
(
!
is_dir
(
$dir
))
{
...
...
@@ -124,6 +126,103 @@ class MessageController extends Controller
}
}
}
if
(
$config
[
'format'
]
===
'db'
)
{
$dbConnection
=
\Yii
::
$app
->
getComponent
(
isset
(
$config
[
'connectionID'
])
?
$config
[
'connectionID'
]
:
'db'
);
if
(
!
$dbConnection
instanceof
\yii\db\Connection
)
{
$this
->
usageError
(
'The "connectionID" must refer to a valid database application component.'
);
}
$sourceMessageTable
=
!
isset
(
$config
[
'sourceMessageTable'
])
?
'SourceMessage'
:
$config
[
'sourceMessageTable'
];
$res
=
[];
foreach
(
$config
[
'languages'
]
as
$language
)
{
foreach
(
$messages
as
$category
=>
$msgs
)
{
$res
[
$category
]
=
$msgs
;
}
}
$this
->
saveMessagesToDb
(
$res
,
$dbConnection
,
$sourceMessageTable
,
$config
[
'removeUnused'
]
);
}
}
/**
* @param $messages
* @param \yii\db\Connection $dbConnection
* @param $sourceMessageTable
* @param $removeUnused
*/
protected
function
saveMessagesToDb
(
$messages
,
$dbConnection
,
$sourceMessageTable
,
$removeUnused
)
{
$q
=
new
\yii\db\Query
;
$current
=
[];
foreach
(
$q
->
select
([
'id'
,
'category'
,
'message'
])
->
from
(
$sourceMessageTable
)
->
all
()
as
$row
)
{
$current
[
$row
[
'category'
]][
$row
[
'id'
]]
=
$row
[
'message'
];
}
$new
=
[];
$obsoleted
=
[];
foreach
(
$messages
as
$category
=>
$msgs
)
{
$msgs
=
array_unique
(
$msgs
);
if
(
isset
(
$current
[
$category
]))
{
$new
[
$category
]
=
array_diff
(
$msgs
,
$current
[
$category
]);
$obsoleted
=
array_diff
(
$current
[
$category
],
$msgs
);
}
else
{
$new
[
$category
]
=
$msgs
;
}
}
foreach
(
array_diff
(
array_keys
(
$current
),
array_keys
(
$messages
))
as
$category
)
{
$obsoleted
+=
$current
[
$category
];
}
if
(
!
$removeUnused
)
{
foreach
(
$obsoleted
as
$pk
=>
$m
)
{
if
(
mb_substr
(
$m
,
0
,
2
)
===
'@@'
&&
mb_substr
(
$m
,
-
2
)
===
'@@'
)
{
unset
(
$obsoleted
[
$pk
]);
}
}
}
$obsoleted
=
array_keys
(
$obsoleted
);
echo
"Inserting new messages..."
;
$savedFlag
=
false
;
foreach
(
$new
as
$category
=>
$msgs
)
{
foreach
(
$msgs
as
$m
)
{
$savedFlag
=
true
;
$dbConnection
->
createCommand
()
->
insert
(
$sourceMessageTable
,
[
'category'
=>
$category
,
'message'
=>
$m
])
->
execute
();
}
}
echo
$savedFlag
?
"saved.
\n
"
:
"nothing new...skipped.
\n
"
;
echo
$removeUnused
?
"Deleting obsoleted messages..."
:
"Updating obsoleted messages..."
;
if
(
empty
(
$obsoleted
))
{
echo
"nothing obsoleted...skipped.
\n
"
;
}
else
{
if
(
$removeUnused
)
{
$dbConnection
->
createCommand
()
->
delete
(
$sourceMessageTable
,
[
'in'
,
'id'
,
$obsoleted
])
->
execute
();
echo
"deleted.
\n
"
;
}
else
{
$dbConnection
->
createCommand
()
->
update
(
$sourceMessageTable
,
[
'message'
=>
new
\yii\db\Expression
(
"CONCAT('@@',message,'@@')"
)],
[
'in'
,
'id'
,
$obsoleted
]
)
->
execute
();
echo
"updated.
\n
"
;
}
}
}
/**
* Extracts messages from a file
...
...
framework/i18n/DbMessageSource.php
View file @
ec1c85bb
...
...
@@ -22,7 +22,7 @@ use yii\db\Query;
*
* ~~~
* CREATE TABLE tbl_source_message (
* id INTEGER PRIMARY KEY,
* id INTEGER PRIMARY KEY
AUTO_INCREMENT
,
* category VARCHAR(32),
* message TEXT
* );
...
...
framework/messages/config.php
View file @
ec1c85bb
...
...
@@ -42,6 +42,6 @@ return [
'.hgkeep'
,
'/messages'
,
],
// Generated file format. Can be either "php" or "po".
// Generated file format. Can be either "
db", "
php" or "po".
'format'
=>
'php'
,
];
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