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
edf98ced
Commit
edf98ced
authored
Jan 14, 2014
by
Alexander Makarov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #1973: Re-implemented solution for `yii message/extract` is now able to generate `.po` files
parent
f6e6ea99
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
26 additions
and
24 deletions
+26
-24
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
MessageController.php
framework/console/controllers/MessageController.php
+20
-23
config.php
framework/messages/config.php
+2
-0
messageConfig.php
framework/views/messageConfig.php
+3
-1
No files found.
framework/CHANGELOG.md
View file @
edf98ced
...
@@ -64,6 +64,7 @@ Yii Framework 2 Change Log
...
@@ -64,6 +64,7 @@ Yii Framework 2 Change Log
-
Enh #1852: ActiveRecord::tableName() now returns table name using DbConnection::tablePrefix (creocoder)
-
Enh #1852: ActiveRecord::tableName() now returns table name using DbConnection::tablePrefix (creocoder)
-
Enh #1894: The path aliases
`@webroot`
and
`@web`
are now available right after the application is initialized (qiangxue)
-
Enh #1894: The path aliases
`@webroot`
and
`@web`
are now available right after the application is initialized (qiangxue)
-
Enh #1921: Grid view ActionColumn now allow to name buttons like
`{controller/action}`
(creocoder)
-
Enh #1921: Grid view ActionColumn now allow to name buttons like
`{controller/action}`
(creocoder)
-
Enh #1973:
`yii message/extract`
is now able to generate
`.po`
files (SergeiKutanov, samdark)
-
Enh: Added
`favicon.ico`
and
`robots.txt`
to default application templates (samdark)
-
Enh: Added
`favicon.ico`
and
`robots.txt`
to default application templates (samdark)
-
Enh: Added
`Widget::autoIdPrefix`
to support prefixing automatically generated widget IDs (qiangxue)
-
Enh: Added
`Widget::autoIdPrefix`
to support prefixing automatically generated widget IDs (qiangxue)
-
Enh: Support for file aliases in console command 'message' (omnilight)
-
Enh: Support for file aliases in console command 'message' (omnilight)
...
...
framework/console/controllers/MessageController.php
View file @
edf98ced
...
@@ -15,8 +15,9 @@ use yii\helpers\FileHelper;
...
@@ -15,8 +15,9 @@ use yii\helpers\FileHelper;
/**
/**
* This command extracts messages to be translated from source files.
* This command extracts messages to be translated from source files.
* The extracted messages are saved as PHP message source files
* The extracted messages are saved either as PHP message source files
* under the specified directory.
* or ".po" files under the specified directory. Format depends on `format`
* setting in config file.
*
*
* Usage:
* Usage:
* 1. Create a configuration file using the 'message/config' command:
* 1. Create a configuration file using the 'message/config' command:
...
@@ -69,7 +70,7 @@ class MessageController extends Controller
...
@@ -69,7 +70,7 @@ class MessageController extends Controller
* this file and then customize it for your needs.
* this file and then customize it for your needs.
* @throws Exception on failure.
* @throws Exception on failure.
*/
*/
public
function
actionExtract
(
$configFile
,
$out
=
null
)
public
function
actionExtract
(
$configFile
)
{
{
$configFile
=
Yii
::
getAlias
(
$configFile
);
$configFile
=
Yii
::
getAlias
(
$configFile
);
if
(
!
is_file
(
$configFile
))
{
if
(
!
is_file
(
$configFile
))
{
...
@@ -81,6 +82,7 @@ class MessageController extends Controller
...
@@ -81,6 +82,7 @@ class MessageController extends Controller
'overwrite'
=>
false
,
'overwrite'
=>
false
,
'removeUnused'
=>
false
,
'removeUnused'
=>
false
,
'sort'
=>
false
,
'sort'
=>
false
,
'format'
=>
'php'
,
],
require
(
$configFile
));
],
require
(
$configFile
));
if
(
!
isset
(
$config
[
'sourcePath'
],
$config
[
'messagePath'
],
$config
[
'languages'
]))
{
if
(
!
isset
(
$config
[
'sourcePath'
],
$config
[
'messagePath'
],
$config
[
'languages'
]))
{
...
@@ -95,6 +97,9 @@ class MessageController extends Controller
...
@@ -95,6 +97,9 @@ class MessageController extends Controller
if
(
empty
(
$config
[
'languages'
]))
{
if
(
empty
(
$config
[
'languages'
]))
{
throw
new
Exception
(
"Languages cannot be empty."
);
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".'
);
}
$files
=
FileHelper
::
findFiles
(
realpath
(
$config
[
'sourcePath'
]),
$config
);
$files
=
FileHelper
::
findFiles
(
realpath
(
$config
[
'sourcePath'
]),
$config
);
...
@@ -109,21 +114,13 @@ class MessageController extends Controller
...
@@ -109,21 +114,13 @@ class MessageController extends Controller
@
mkdir
(
$dir
);
@
mkdir
(
$dir
);
}
}
foreach
(
$messages
as
$category
=>
$msgs
)
{
foreach
(
$messages
as
$category
=>
$msgs
)
{
if
(
$out
==
'po'
){
$file
=
str_replace
(
"
\\
"
,
'/'
,
"
$dir
/
$category
."
.
$config
[
'format'
]);
$file
=
str_replace
(
"
\\
"
,
'/'
,
"
$dir
/
$category
.po"
);
}
else
if
(
$out
==
null
){
$file
=
str_replace
(
"
\\
"
,
'/'
,
"
$dir
/
$category
.php"
);
}
$path
=
dirname
(
$file
);
$path
=
dirname
(
$file
);
if
(
!
is_dir
(
$path
))
{
if
(
!
is_dir
(
$path
))
{
mkdir
(
$path
,
0755
,
true
);
mkdir
(
$path
,
0755
,
true
);
}
}
$msgs
=
array_values
(
array_unique
(
$msgs
));
$msgs
=
array_values
(
array_unique
(
$msgs
));
if
(
$out
==
'po'
){
$this
->
generateMessageFile
(
$msgs
,
$file
,
$config
[
'overwrite'
],
$config
[
'removeUnused'
],
$config
[
'sort'
],
$config
[
'format'
]);
$this
->
generateMessageFile
(
$msgs
,
$file
,
$config
[
'overwrite'
],
$config
[
'removeUnused'
],
$config
[
'sort'
],
$out
);
}
else
if
(
$out
==
null
){
$this
->
generateMessageFile
(
$msgs
,
$file
,
$config
[
'overwrite'
],
$config
[
'removeUnused'
],
$config
[
'sort'
]);
}
}
}
}
}
}
}
...
@@ -168,18 +165,19 @@ class MessageController extends Controller
...
@@ -168,18 +165,19 @@ class MessageController extends Controller
* @param boolean $overwrite if existing file should be overwritten without backup
* @param boolean $overwrite if existing file should be overwritten without backup
* @param boolean $removeUnused if obsolete translations should be removed
* @param boolean $removeUnused if obsolete translations should be removed
* @param boolean $sort if translations should be sorted
* @param boolean $sort if translations should be sorted
* @param string $format output format
*/
*/
protected
function
generateMessageFile
(
$messages
,
$fileName
,
$overwrite
,
$removeUnused
,
$sort
,
$
type
=
null
)
protected
function
generateMessageFile
(
$messages
,
$fileName
,
$overwrite
,
$removeUnused
,
$sort
,
$
format
)
{
{
echo
"Saving messages to
$fileName
..."
;
echo
"Saving messages to
$fileName
..."
;
if
(
is_file
(
$fileName
))
{
if
(
is_file
(
$fileName
))
{
if
(
$type
==
'po'
){
if
(
$format
=
==
'po'
){
$translated
=
file_get_contents
(
$fileName
);
$translated
=
file_get_contents
(
$fileName
);
preg_match_all
(
'/(?<=msgid ").*(?="\nmsgstr)/'
,
$translated
,
$keys
);
preg_match_all
(
'/(?<=msgid ").*(?="\nmsgstr)/'
,
$translated
,
$keys
);
preg_match_all
(
'/(?<=msgstr ").*(?="\n\n)/'
,
$translated
,
$values
);
preg_match_all
(
'/(?<=msgstr ").*(?="\n\n)/'
,
$translated
,
$values
);
$translated
=
array_combine
(
$keys
[
0
],
$values
[
0
]);
$translated
=
array_combine
(
$keys
[
0
],
$values
[
0
]);
}
else
if
(
$type
==
null
)
{
}
else
{
$translated
=
require
(
$fileName
);
$translated
=
require
(
$fileName
);
}
}
sort
(
$messages
);
sort
(
$messages
);
...
@@ -220,9 +218,8 @@ class MessageController extends Controller
...
@@ -220,9 +218,8 @@ class MessageController extends Controller
if
(
false
===
$overwrite
)
{
if
(
false
===
$overwrite
)
{
$fileName
.=
'.merged'
;
$fileName
.=
'.merged'
;
}
}
if
(
$type
==
'po'
){
if
(
$format
=
==
'po'
){
$out_str
=
''
;
$out_str
=
''
;
ksort
(
$merged
);
foreach
(
$merged
as
$k
=>
$v
){
foreach
(
$merged
as
$k
=>
$v
){
$out_str
.=
"msgid
\"
$k
\"\n
"
;
$out_str
.=
"msgid
\"
$k
\"\n
"
;
$out_str
.=
"msgstr
\"
$v
\"\n
"
;
$out_str
.=
"msgstr
\"
$v
\"\n
"
;
...
@@ -232,15 +229,15 @@ class MessageController extends Controller
...
@@ -232,15 +229,15 @@ class MessageController extends Controller
}
}
echo
"translation merged.
\n
"
;
echo
"translation merged.
\n
"
;
}
else
{
}
else
{
if
(
$type
==
'po'
)
{
if
(
$format
===
'po'
)
{
$merged
=
''
;
$merged
=
''
;
sort
(
$messages
);
sort
(
$messages
);
foreach
(
$messages
as
$message
)
{
foreach
(
$messages
as
$message
)
{
$merged
.=
"msgid
\"
$message
\"\n
"
;
$merged
.=
"msgid
\"
$message
\"\n
"
;
$merged
.=
"msgstr
\"\"\n
"
;
$merged
.=
"msgstr
\"\"\n
"
;
$merged
.=
"
\n
"
;
$merged
.=
"
\n
"
;
}
}
}
else
if
(
$type
==
null
)
{
}
else
{
$merged
=
[];
$merged
=
[];
foreach
(
$messages
as
$message
)
{
foreach
(
$messages
as
$message
)
{
$merged
[
$message
]
=
''
;
$merged
[
$message
]
=
''
;
...
@@ -249,9 +246,9 @@ class MessageController extends Controller
...
@@ -249,9 +246,9 @@ class MessageController extends Controller
}
}
echo
"saved.
\n
"
;
echo
"saved.
\n
"
;
}
}
if
(
$type
==
'po'
)
{
if
(
$format
===
'po'
)
{
$content
=
$merged
;
$content
=
$merged
;
}
else
if
(
$type
==
null
)
{
}
else
{
$array
=
str_replace
(
"
\r
"
,
''
,
var_export
(
$merged
,
true
));
$array
=
str_replace
(
"
\r
"
,
''
,
var_export
(
$merged
,
true
));
$content
=
<<<EOD
$content
=
<<<EOD
<?php
<?php
...
...
framework/messages/config.php
View file @
edf98ced
...
@@ -42,4 +42,6 @@ return [
...
@@ -42,4 +42,6 @@ return [
'.hgkeep'
,
'.hgkeep'
,
'/messages'
,
'/messages'
,
],
],
// Generated file format. Can be either "php" or "po".
'format'
=>
'php'
,
];
];
framework/views/messageConfig.php
View file @
edf98ced
...
@@ -6,7 +6,7 @@ return [
...
@@ -6,7 +6,7 @@ return [
// string, required, root directory containing message translations.
// string, required, root directory containing message translations.
'messagePath'
=>
__DIR__
.
DIRECTORY_SEPARATOR
.
'messages'
,
'messagePath'
=>
__DIR__
.
DIRECTORY_SEPARATOR
.
'messages'
,
// array, required, list of language codes that the extracted messages
// array, required, list of language codes that the extracted messages
// should be translated to. For example, ['zh
_cn
', 'de'].
// should be translated to. For example, ['zh
-CN
', 'de'].
'languages'
=>
[
'de'
],
'languages'
=>
[
'de'
],
// string, the name of the function for translating messages.
// string, the name of the function for translating messages.
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
// Defaults to 'Yii::t'. This is used as a mark to find the messages to be
...
@@ -42,4 +42,6 @@ return [
...
@@ -42,4 +42,6 @@ return [
'.hgkeep'
,
'.hgkeep'
,
'/messages'
,
'/messages'
,
],
],
// Generated file format. Can be either "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