Skip to content

Commit 01d742d

Browse files
authored
Merge pull request #6 from ava007/wnk_translation_cakephp36
Wnk translation cakephp36
2 parents ce80274 + 2caf4dc commit 01d742d

File tree

15 files changed

+281
-74
lines changed

15 files changed

+281
-74
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# Translation Plugin for CakePHP 3
2+
# Translation Plugin for CakePHP 3.6
33

44
It provides translation services for pot files:
55

@@ -27,6 +27,16 @@ Configure::write('WnkTranslation', [
2727
'tablePrefix' => '', // optional prefix for database tables
2828
]);
2929
```
30+
### src/Application.php
31+
```
32+
class Application //extends BaseApplication {
33+
34+
public function bootstrap() {
35+
parent::bootstrap();
36+
$this->addPlugin('WnkTranslation');
37+
}
38+
}
39+
```
3040
### Database
3141

3242
run the appropriate sql-ddl-script:
@@ -36,9 +46,12 @@ run the appropriate sql-ddl-script:
3646

3747
After installation the plugins is called used the following url:
3848

39-
http://....domainname/wnk_translation/translations/index
49+
http://....domainname/wnk-translation/translations/cockpit
4050

4151

4252
## References
4353

54+
55+
4456
visit https://www.locavores.co/wnk-translation/translations/index to see this plugin in action
57+

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
"description": "Translation plugin for CakePHP",
44
"type": "cakephp-plugin",
55
"require": {
6-
"php": ">=5.4.16",
7-
"cakephp/cakephp": ">=3.0"
6+
"cakephp/cakephp": ">=3.6"
87
},
98
"require-dev": {
109
"phpunit/phpunit": "*"
1110
},
1211
"autoload": {
1312
"psr-4": {
14-
"WnkTranslation\\": "src"
13+
"WnkTranslation\\": "src/"
1514
}
1615
},
1716
"autoload-dev": {

config/routes.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
2+
use Cake\Routing\RouteBuilder;
23
use Cake\Routing\Router;
4+
use Cake\Routing\Route\DashedRoute;
35

4-
Router::plugin('WnkTranslation', function ($routes) {
5-
$routes->fallbacks('DashedRoute');
6-
});
6+
Router::plugin('WnkTranslation',
7+
['path' => '/wnk-translation'],
8+
function (RouteBuilder $routes) {
9+
$routes->fallbacks(DashedRoute::class);
10+
}
11+
);

ddl-postgresql.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ CREATE TABLE wnk_translation_proposed (
1919
msgstr text
2020
CONSTRAINT status CHECK ( status IN ( 'ProposedByUser', 'AcceptedByAdmin') )
2121
) with (oids = false);
22+
23+
create unique index wnk_translation_unique on wnk_translation (msgid,locale);

src/Controller/Component/UtltransComponent.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,14 @@ function export() {
134134

135135
// Step 4: export default.pot file to the relevant directory
136136

137-
$wnk_translation = Configure::read('WnkTranslation');
137+
$msg = 'export: wrote to ' . ROOT. DS. 'src'.DS.'Locale' ; // for msg to end user
138+
139+
$wnk_translation = Configure::read('WnkTranslation');
138140

139-
$filename= 'f' . gmdate('YmdHis');
141+
$filename= 'f' . gmdate('YmdHis');
140142

141-
// iterate through all configured languages:
142-
foreach ($wnk_translation['trans_lang'] as $k):
143+
// iterate through all configured languages:
144+
foreach ($wnk_translation['trans_lang'] as $k):
143145
if ($k == $wnk_translation['default_lang']) continue;
144146

145147
$path = ROOT.DS.'src'.DS.'Locale'.DS.$k;
@@ -177,8 +179,9 @@ function export() {
177179
if (file_exists($path.DS.'default.po'))
178180
unlink ($path.DS.'default.po');
179181
rename ($path.DS.$filename,$path.DS.'default.po');
182+
$msg .= ' (' . $k . ': ' . $z. ')';
180183
endforeach;
181-
return 'Export successful with ' . $z . ' Records';
184+
return 'Export successful ended. ' . $msg;
182185
}
183186

184187
/*

src/Controller/ProposedtranslationsController.php renamed to src/Controller/TranslationproposedController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
namespace WnkProposedtranslation\Controller;
2+
namespace WnkTranslation\Controller;
33

44
use WnkTranslation\Controller\AppController;
55
use Cake\Core\Configure;
@@ -11,7 +11,7 @@
1111
*
1212
* @property \WnkTranslation\Model\Table\TranslationsTable $Translations
1313
*/
14-
class ProposedtranslationsController extends AppController
14+
class TranslationproposedController extends AppController
1515
{
1616

1717
public function initialize()
@@ -27,16 +27,15 @@ public function initialize()
2727
*/
2828
public function add($id = null)
2929
{
30-
$translation = $this->Translations->get($id, [
31-
'contain' => []
32-
]);
30+
$this->loadModel('WnkTranslation.Translations');
31+
$translation = $this->Translations->get($id);
3332
$this->set('translation', $translation);
3433
$this->set('_serialize', ['translation']);
3534

3635
if ($this->request->is('post')) {
37-
$translation = $this->Proposedtranslations->newEntity();
38-
$translation = $this->Proposedtranslations->patchEntity($translation, $this->request->data);
39-
if ($this->Proposedtranslations->save($translation)) {
36+
$translation = $this->Translationproposed->newEntity();
37+
$translation = $this->Translationproposed->patchEntity($translation, $this->request->data);
38+
if ($this->Translationproposed->save($translation)) {
4039
$this->Flash->success(__('The translation has been saved.'));
4140
return $this->redirect(['action' => 'index']);
4241
} else {

src/Controller/TranslationsController.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,25 @@ public function index()
3030
{
3131

3232
$where = array();
33-
if (!empty($this->request->getQuery['locale']))
34-
$where['locale'] = $this->request->getQuery['locale'];
33+
34+
if (!empty($this->request->getQuery('locale')))
35+
$where['locale'] = $this->request->getQuery('locale');
3536
if (!empty($this->request->data['locale']))
3637
$where['locale'] = $this->request->data['locale'];
3738

38-
if (!empty($this->request->getQuery['msgstr']))
39-
$where['msgstr like'] = '%' . $this->request->getQuery['msgstr'] . '%';
39+
if (!empty($this->request->getQuery('msgstr')))
40+
$where['msgstr like'] = '%' . $this->request->getQuery('msgstr') . '%';
4041
if (!empty($this->request->data['msgstr']))
4142
$where['msgstr like'] = '%' . $this->request->data['msgstr'] . '%';
4243

43-
if (!empty($this->request->getQuery['status']))
44-
$where['status'] = $this->request->getQuery['status'];
44+
if (!empty($this->request->getQuery('status')))
45+
$where['status'] = $this->request->getQuery('status');
46+
4547
if (!empty($this->request->data['status']))
4648
$where['status'] = $this->request->data['status'];
4749

4850
$query = $this->Translations->find()->where($where);
49-
51+
5052
$this->set('translations', $this->paginate($query));
5153
$this->set('_serialize', ['translations']);
5254
$this->set('WnkTranslation', Configure::read('WnkTranslation'));
@@ -72,13 +74,13 @@ public function cockpit()
7274
$table = $wnkConf['tablePrefix'] . $table;
7375

7476
$q = "select locale,status,count(*) as cnt from " . $table;
75-
$q.= "where locale in (select distinct locale where status ='Original') ";
77+
$q.= " where locale in (select distinct locale where status ='Original') ";
7678
$q.= "group by locale,status order by 1,2 ";
7779
$tset = $conn->execute($q)->fetchAll('assoc');
7880
$this->set('original', $tset);
7981

8082
$q = "select locale,status,count(*) as cnt from " . $table;
81-
$q.= "where locale NOT in (select distinct locale where status ='Original') ";
83+
$q.= " where locale NOT in (select distinct locale where status ='Original') ";
8284
$q.= "group by locale,status order by 1,2 ";
8385
$tset = $conn->execute($q)->fetchAll('assoc');
8486
$this->set('tsets', $tset);
@@ -215,4 +217,14 @@ public function googletranslate() {
215217
return $this->redirect(['action' => 'index']);
216218
}
217219
}
220+
221+
public function deleteunused()
222+
{
223+
$this->autoRender = false;
224+
$rc = $this->Translations->deleteunused();
225+
$this->Flash->success(__('del ended.') . ' ' . $rc);
226+
return $this->redirect(['action' => 'cockpit']);
227+
}
228+
229+
218230
}

src/Model/Entity/Translation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ class Translation extends Entity
2727
* @var array
2828
*/
2929
protected $_accessible = [
30-
'*' => true,
31-
'id' => false,
30+
'msgid' => true,
31+
'locale' => true,
32+
'created' => true,
33+
'last_used' => true,
34+
'status' => true,
35+
'msgstr' => true
3236
];
3337
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace WnkTranslation\Model\Entity;
3+
4+
use Cake\ORM\Entity;
5+
6+
/**
7+
* WnkTranslationProposed Entity
8+
*
9+
* @property string $id
10+
* @property string $msgid
11+
* @property string $locale
12+
* @property string $status
13+
* @property \Cake\I18n\Time $created
14+
* @property string $createdbyip
15+
* @property string $msgstr
16+
*/
17+
class TranslationProposed extends Entity
18+
{
19+
20+
/**
21+
* Fields that can be mass assigned using newEntity() or patchEntity().
22+
*
23+
* Note that when '*' is set to true, this allows all unspecified fields to
24+
* be mass assigned. For security purposes, it is advised to set '*' to false
25+
* (or remove it), and explicitly make individual fields accessible as needed.
26+
*
27+
* @var array
28+
*/
29+
protected $_accessible = [
30+
'msgid' => true,
31+
'locale' => true,
32+
'status' => true,
33+
'created' => true,
34+
'createdbyip' => true,
35+
'msgstr' => true
36+
];
37+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
namespace WnkTranslation\Model\Table;
3+
4+
use Cake\ORM\Query;
5+
use Cake\ORM\RulesChecker;
6+
use Cake\ORM\Table;
7+
use Cake\Validation\Validator;
8+
9+
/**
10+
* WnkTranslationProposed Model
11+
*
12+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed get($primaryKey, $options = [])
13+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed newEntity($data = null, array $options = [])
14+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed[] newEntities(array $data, array $options = [])
15+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed|bool save(\Cake\Datasource\EntityInterface $entity, $options = [])
16+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed|bool saveOrFail(\Cake\Datasource\EntityInterface $entity, $options = [])
17+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
18+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed[] patchEntities($entities, array $data, array $options = [])
19+
* @method \WnkTranslation\Model\Entity\WnkTranslationProposed findOrCreate($search, callable $callback = null, $options = [])
20+
*
21+
* @mixin \Cake\ORM\Behavior\TimestampBehavior
22+
*/
23+
class TranslationProposedTable extends Table
24+
{
25+
26+
/**
27+
* Initialize method
28+
*
29+
* @param array $config The configuration for the Table.
30+
* @return void
31+
*/
32+
public function initialize(array $config)
33+
{
34+
parent::initialize($config);
35+
36+
$wnkConf = Configure::read('WnkTranslation');
37+
if (isset($wnkConf['tablePrefix']))
38+
$table = $wnkConf['tablePrefix'] . 'wnk_translation_proposed';
39+
else
40+
$table = 'wnk_translation';
41+
42+
$this->setTable($table);
43+
44+
$this->setDisplayField('id');
45+
$this->setPrimaryKey('id');
46+
47+
$this->addBehavior('Timestamp');
48+
}
49+
50+
/**
51+
* Default validation rules.
52+
*
53+
* @param \Cake\Validation\Validator $validator Validator instance.
54+
* @return \Cake\Validation\Validator
55+
*/
56+
public function validationDefault(Validator $validator)
57+
{
58+
$validator
59+
->scalar('id')
60+
->maxLength('id', 36)
61+
->allowEmpty('id', 'create');
62+
63+
$validator
64+
->scalar('msgid')
65+
->maxLength('msgid', 255)
66+
->requirePresence('msgid', 'create')
67+
->notEmpty('msgid');
68+
69+
$validator
70+
->scalar('locale')
71+
->maxLength('locale', 2)
72+
->allowEmpty('locale');
73+
74+
$validator
75+
->scalar('status')
76+
->maxLength('status', 24)
77+
->allowEmpty('status');
78+
79+
$validator
80+
->scalar('createdbyip')
81+
->maxLength('createdbyip', 41)
82+
->allowEmpty('createdbyip');
83+
84+
$validator
85+
->scalar('msgstr')
86+
->allowEmpty('msgstr');
87+
88+
return $validator;
89+
}
90+
}

0 commit comments

Comments
 (0)