Giter Site home page Giter Site logo

cakephp-multitranslatebehavior's Introduction

Multi Translate Behavior

  • Author:
      Saleh Souzanchi 
      @func0der  https://github.com/func0der
    
  • Version: 1.0.0
  • License: MIT
  • CakePHP: 2.1

Features

  • support multi language in form ( Model.fieldName.locale )
  • save and edit record , is very easy
  • validate all language for a field

Changelog

  • 1.0.0 first release.

Install

Clone the MultiTranslateBehavior.php into your app/Model/Behavior directory:

Setup

1-in model :


class Post extends AppModel{
	public $actsAs = array(
        'MultiTranslate' => array(
            'title','body'
        )
    );
    public $validate = array(
        'title' => array(
            'rule' => 'notEmpty',
            'message' => ' your message '
        ),
        'body' => array(
            'rule' => 'notEmpty',
            'message' => 'message'
        ),    
    );
}

2- in controller :


classPostsControllerextendsAppController{

	public function admin_index(){
		$this->Post->setLocale(array('fa','en'));
		$Results=$this->Paginator->paginate('Post');
		$this->set('Results',$Results);
	}


	public function admin_add(){
		$this->Post->setLocale(array('fa','en'));
		$this->Post->multiTranslateOptions(array('validate'=>true));

		if($this->request->is('post')){
			$this->Post->create();
			if($this->Post->save($this->request->data)){
				$this->flash(__('save..'),array('action'=>'index'));
			}
		}
	}

	public function admin_edit($id=null){
		$this->Post->setLocale(array('fa','en'));
		$this->Post->id=$id;
		if(!$this->Post->exists()){
			thrownewNotFoundException(__('InvalidPost'));
		}
		$this->Post->multiTranslateOptions(array('validate'=>true,'find'=>true));
		if($this->request->is('post')||$this->request->is('put')){
			if($this->Post->save($this->request->data)){
				$this->flash(__('save...'),array('action'=>'index'));
			}
			else{
				$this->Session->setFlash(__('cannotsave'));
			}
		} else {
			$this->request->data=$this->Post->read(null,$id);
		}
	}

}

3- in view/forms :


    echo $this->Form->create('Post');
    echo $this->Form->input('Post.title.eng');
    echo $this->Form->input('Post.title.per');
    echo $this->Form->input('Post.title.pol');
    echo $this->Form->input('Post.body.eng');
    echo $this->Form->input('Post.body.per');
    echo $this->Form->input('Post.body.pol');
    echo $this->Form->end('save');

cakephp-multitranslatebehavior's People

Contributors

hugofcampos avatar zoghal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cakephp-multitranslatebehavior's Issues

Save error since cake 2.2.4

I've migrated my cakephp from 2.2.3 to 2.2.4 and i've met problems with the MultiTranslateBehavior :

Cake try to save the the field with translation array into the model table :

Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'

SQL Query: UPDATE `iwibox-prod`.`mod_article_items` SET `id` = 2415, `content` = Array, `has_tablet_content` = '0', `tablet_content` = Array, `has_mobile_content` = '0', `mobile_content` = Array WHERE `iwibox-prod`.`mod_article_items`.`id` = '2415'

Before, it tooks the first content value into this table and saved the others into the i18n table ... so it worked

I'll try to understand why it behaves like this now , but if someone have an idea i'll take it !

Saving form with 2.4.2 generates loop error

Fatal error: Maximum function nesting level of '100' reached, aborting! in E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php on line 101

Call Stack:
0.0003 361544 1. {main}() E:\htdocs\cakephp2\app\webroot\index.php:0
0.0158 3221736 2. Dispatcher->dispatch() E:\htdocs\cakephp2\app\webroot\index.php:110
0.0227 4338536 3. Dispatcher->_invoke() E:\htdocs\cakephp2\lib\Cake\Routing\Dispatcher.php:162
0.0254 4683528 4. Controller->invokeAction() E:\htdocs\cakephp2\lib\Cake\Routing\Dispatcher.php:187
0.0254 4684112 5. ReflectionMethod->invokeArgs() E:\htdocs\cakephp2\lib\Cake\Controller\Controller.php:490
0.0254 4684128 6. GroupsController->edit() E:\htdocs\cakephp2\lib\Cake\Controller\Controller.php:490
0.0438 8098488 7. Model->save() E:\htdocs\cakephp2\app\Controller\GroupsController.php:52
0.0443 8102496 8. Model->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:1738
0.0451 8254496 9. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0451 8254496 10. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0451 8254496 11. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0453 8267800 12. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0454 8268048 13. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0454 8268064 14. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0454 8270280 15. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0454 8270296 16. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0454 8270328 17. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0454 8274176 18. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0454 8274176 19. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0454 8274176 20. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0454 8274176 21. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0454 8275000 22. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0455 8275248 23. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0455 8275264 24. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0455 8277512 25. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0455 8277528 26. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0455 8277560 27. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0455 8281408 28. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0455 8281408 29. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0455 8281408 30. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0455 8281408 31. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0455 8282232 32. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0455 8282480 33. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0455 8282496 34. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0456 8284744 35. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0456 8284760 36. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0456 8284792 37. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0456 8288640 38. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0456 8288640 39. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0456 8288640 40. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0456 8288640 41. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0456 8289464 42. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0456 8289712 43. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0456 8289728 44. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0456 8292064 45. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0456 8292080 46. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0456 8292112 47. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0457 8295960 48. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0457 8295960 49. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0457 8295960 50. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0457 8295960 51. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0457 8296784 52. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0457 8297032 53. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0457 8297048 54. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0457 8299384 55. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0457 8299400 56. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0457 8299432 57. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0457 8303280 58. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0457 8303280 59. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0457 8303280 60. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0457 8303280 61. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0458 8304104 62. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0458 8304352 63. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0458 8304368 64. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0458 8306704 65. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0458 8306720 66. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0458 8306752 67. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0458 8310600 68. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0458 8310600 69. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0458 8310600 70. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0458 8310600 71. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0458 8311424 72. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0459 8311672 73. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0459 8311688 74. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0459 8314024 75. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0459 8314040 76. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0459 8314072 77. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0459 8317920 78. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0459 8317920 79. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0459 8317920 80. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0459 8317920 81. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0459 8318744 82. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0459 8318992 83. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0459 8319008 84. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0460 8321344 85. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0460 8321360 86. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0460 8321392 87. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0460 8325240 88. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0460 8325240 89. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297
0.0460 8325240 90. ModelValidator->errors() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:101
0.0460 8325240 91. ModelValidator->_triggerBeforeValidate() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:243
0.0460 8326064 92. CakeEventManager->dispatch() E:\htdocs\cakephp2\lib\Cake\Model\ModelValidator.php:459
0.0460 8326312 93. call_user_func() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0460 8326328 94. ObjectCollection->trigger() E:\htdocs\cakephp2\lib\Cake\Event\CakeEventManager.php:248
0.0460 8328664 95. call_user_func_array() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0460 8328680 96. MultiTranslateBehavior->beforeValidate() E:\htdocs\cakephp2\lib\Cake\Utility\ObjectCollection.php:132
0.0461 8328712 97. MultiTranslateBehavior->_multiValidate() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:146
0.0461 8332560 98. Model->validates() E:\htdocs\cakephp2\app\Model\Behavior\MultiTranslateBehavior.php:230
0.0461 8332560 99. ModelValidator->validates() E:\htdocs\cakephp2\lib\Cake\Model\Model.php:3297

PHP 5.3 isset behaviour

Hello, I've noticed that isset($string) (expected array) returns always true.

Line: 102

if (!isset($row[$model->alias][$aliasField][$_locale]) && !empty($row[$model->alias]['i18n_' . $field . '_' . $_locale])) {

In my case $row[$model->alias][$aliasField] returns string.

from php.net

Example #2 isset() on String Offsets

$expected_array_got_string = 'somestring';
var_dump(isset($expected_array_got_string['some_key']));
var_dump(isset($expected_array_got_string[0]));
var_dump(isset($expected_array_got_string['0']));
var_dump(isset($expected_array_got_string[0.5]));
var_dump(isset($expected_array_got_string['0.5']));
var_dump(isset($expected_array_got_string['0 Mostel']));

Output of the above example in PHP 5.3:
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

I suggest to add this line before the condition to ensure that isset is checking an array.

$row[$model->alias][$aliasField] = array();

Behavior mess up template locale

My site is localized in english and br-portuguese.
When MultiTranslateBehavior is set, the templte looses its default locale.

Ex:
before implementing MTB, my admin section was localized (menus, columns, etc). So, it was possible to see it in pt-br.
after, setting MTB to pt-br and en, I can only see admin section in english.

I solved this putting this code in afterFind callback in every Model I have:

$this->setLocale(Configure::read('Config.language'));

I could not find a way to change it in MTB. Help me and ai can pull request this change!

Proposal 2.5

Task Lists 2.5

  • upgrade to cakePHP 2.5
  • support multi validation for singel or multiple languages
  • add new helper for create advenced form - language/keyboard switching(with jquery plugin)
  • add tests

Edit Function not working.

Hello, this is a good workaround for 1 page for all translation. Follow exactly your code, the ADD function works flawlessly but the EDIT does not.

public function edit($id = null) {
$this->Content->setLocale(array('eng','chi'));
$this->Content->id = $id;
if (!$this->Content->exists()) {
throw new NotFoundException(__('Invalid content'));
}

    $this->Content->multiTranslateOptions(array('validate'=>true,'find'=>true));
    if ($this->request->is('post') || $this->request->is('put')) {
        if ($this->Content->save($this->request->data)) {
            $this->Session->setFlash(__('The content has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The content could not be saved. Please, try again.'));
        }
    } else {

        $this->request->data = $this->Content->read(null, $id);
    }
}

Here's the debug of $this->request->data

array(
'Content' => array(
'id' => '1',
'title' => 'Welcome bla',
'body' => 'Welcomebla.',
'meta_description' => 'test',
'meta_keywords' => 'test',
'created' => '2012-05-07 06:37:20',
'modified' => '2012-05-07 06:37:20',
'locale' => 'eng'
)
)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.