@@ -42,14 +42,18 @@ public function main()
42
42
$ this ->out ('<info>I18n Shell</info> ' );
43
43
$ this ->hr ();
44
44
$ this ->out ('[E]xtract POT file from sources ' );
45
+ $ this ->out ('[I]inialize a language from POT file ' );
45
46
$ this ->out ('[H]elp ' );
46
47
$ this ->out ('[Q]uit ' );
47
48
48
- $ choice = strtolower ($ this ->in ('What would you like to do? ' , ['E ' , 'H ' , 'Q ' ]));
49
+ $ choice = strtolower ($ this ->in ('What would you like to do? ' , ['E ' , 'I ' , ' H ' , 'Q ' ]));
49
50
switch ($ choice ) {
50
51
case 'e ' :
51
52
$ this ->Extract ->main ();
52
53
break ;
54
+ case 'i ' :
55
+ $ this ->init ();
56
+ break ;
53
57
case 'h ' :
54
58
$ this ->out ($ this ->OptionParser ->help ());
55
59
break ;
@@ -63,6 +67,54 @@ public function main()
63
67
$ this ->main ();
64
68
}
65
69
70
+ /**
71
+ * Inits PO file from POT file.
72
+ *
73
+ * @return void
74
+ */
75
+ public function init ($ language = null ) {
76
+ if (!$ language ) {
77
+ $ language = strtolower ($ this ->in ('What language? Please use the two-letter ISO code, e.g. `en`. ' ));
78
+ }
79
+ if (strlen ($ language ) !== 2 ) {
80
+ return $ this ->error ('Must be a two-letter ISO code ' );
81
+ }
82
+
83
+ $ this ->_paths = [APP ];
84
+ if (!empty ($ this ->params ['plugin ' ])) {
85
+ $ plugin = Inflector::camelize ($ this ->params ['plugin ' ]);
86
+ $ this ->_paths = [Plugin::classPath ($ plugin )];
87
+ $ this ->params ['plugin ' ] = $ plugin ;
88
+ }
89
+
90
+ $ response = $ this ->in ('What folder? ' , null , rtrim ($ this ->_paths [0 ], DS ) . DS . 'Locale ' );
91
+ $ sourceFolder = rtrim ($ response , DS ) . DS ;
92
+ $ targetFolder = $ sourceFolder . $ language . DS ;
93
+ if (!is_dir ($ targetFolder )) {
94
+ mkdir ($ targetFolder , 0770 , true );
95
+ }
96
+
97
+ $ count = 0 ;
98
+ $ iterator = new \DirectoryIterator ($ sourceFolder );
99
+ foreach ($ iterator as $ fileinfo ) {
100
+ if (!$ fileinfo ->isFile ()) {
101
+ continue ;
102
+ }
103
+ $ filename = $ fileinfo ->getFilename ();
104
+ $ newFilename = $ fileinfo ->getBasename ('.pot ' );
105
+ $ newFilename = $ newFilename . '.po ' ;
106
+ if (empty ($ this ->params ['force ' ]) && is_file ($ targetFolder . $ newFilename )) {
107
+ $ this ->err ('File ' . $ newFilename . ' exists, skipping. Use --force or -f to force overwriting ' );
108
+ continue ;
109
+ }
110
+
111
+ copy ($ sourceFolder . $ filename , $ targetFolder . $ newFilename );
112
+ $ count ++;
113
+ }
114
+
115
+ $ this ->out ('Generated ' . $ count . ' PO files in ' . $ targetFolder );
116
+ }
117
+
66
118
/**
67
119
* Gets the option parser instance and configures it.
68
120
*
@@ -71,12 +123,34 @@ public function main()
71
123
public function getOptionParser ()
72
124
{
73
125
$ parser = parent ::getOptionParser ();
126
+ $ initParser = [
127
+ 'options ' => [
128
+ 'plugin ' => [
129
+ 'help ' => 'Plugin name. ' ,
130
+ 'short ' => 'p '
131
+ ],
132
+ 'force ' => [
133
+ 'help ' => 'Force overwriting. ' ,
134
+ 'short ' => 'f ' ,
135
+ 'boolean ' => true
136
+ ]
137
+ ],
138
+ 'arguments ' => [
139
+ 'language ' => [
140
+ 'help ' => 'Two-letter language code. '
141
+ ]
142
+ ]
143
+ ];
74
144
75
145
$ parser ->description (
76
146
'I18n Shell generates .pot files(s) with translations. '
77
147
)->addSubcommand ('extract ' , [
78
148
'help ' => 'Extract the po translations from your application ' ,
79
149
'parser ' => $ this ->Extract ->getOptionParser ()
150
+ ])
151
+ ->addSubcommand ('init ' , [
152
+ 'help ' => 'Init PO language file from POT file ' ,
153
+ 'parser ' => $ initParser
80
154
]);
81
155
82
156
return $ parser ;
0 commit comments