PHP code example of neam / yii-i18n-attribute-messages
1. Go to this page and download the library: Download neam/yii-i18n-attribute-messages library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
neam / yii-i18n-attribute-messages example snippets
class m131115_204413_i18n extends CDbMigration
{
public function up()
{
$this->renameColumn('book', 'title', '_title');
$this->renameColumn('book', 'slug', '_slug');
$this->renameColumn('chapter', 'title', '_title');
$this->renameColumn('chapter', 'slug', '_slug');
$this->dropForeignKey('fk_chapter_book', 'chapter');
$this->renameColumn('chapter', 'book_id', '_book_id');
$this->addForeignKey('fk_chapter_book', 'chapter', '_book_id', 'book', 'id', 'NO ACTION', 'NO ACTION');
}
public function down()
{
$this->renameColumn('book', '_title', 'title');
$this->renameColumn('book', '_slug', 'slug');
$this->renameColumn('chapter', '_title', 'title');
$this->renameColumn('chapter', '_slug', 'slug');
$this->dropForeignKey('fk_chapter_book', 'chapter');
$this->renameColumn('chapter', '_book_id', 'book_id');
$this->addForeignKey('fk_chapter_book', 'chapter', 'book_id', 'book', 'id', 'NO ACTION', 'NO ACTION');
}
}
#### 4. Add save-support
Save-support is only enabled if you use CDbMessageSource. Configure your app to use it and make sure the following tables (Note: with auto-increment for SourceMessage) exists:
CREATE TABLE `SourceMessage` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`category` VARCHAR(32) NULL DEFAULT NULL,
`message` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`))
COLLATE = utf8_bin;
CREATE TABLE `Message` (
`id` INT(11) NOT NULL DEFAULT '0',
`language` VARCHAR(16) NOT NULL DEFAULT '',
`translation` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`, `language`),
CONSTRAINT `FK_Message_SourceMessage`
FOREIGN KEY (`id`)
REFERENCES `SourceMessage` (`id`)
ON DELETE CASCADE)
COLLATE = utf8_bin;
Hint: You can still keep CPhpMessageSource as the default messages component for your app, and configure CDbMessageSource to be used only for attribute messages.
Your application config should have two message source components configured:
'components' => array(
...
// Static messages
'messages' => array(
'class' => 'CPhpMessageSource',
),
// Attribute messages
'attributeMessages' => array(
'class' => 'CDbMessageSource',
),
...
),
And when configuring the behavior, set an appropriate 'messageSourceComponent' configuration option (see example configuration above).
#### 5. Re-generate models
Use Gii as per the official documentation. To be able to save translations, you'll need to generate the models Message and SourceMessage as well.
Usage
-----
Example usage with a Book model that has a multilingual *title* attribute.
All translations will be available through attribute suffix, ie `$book->title_en` for the english translation, `$book->title_sv` for the swedish translation. `$book->title` will be an alias for the currently selected language's translation.
### Reading and saving translations
#### Fetching translations
$book = Book::model()->findByPk(1);
Yii::app()->language = 'en';
echo $book->title; // Outputs 'The Alchemist'
Yii::app()->language = 'sv';
echo $book->title; // Outputs 'Alkemisten'
echo $book->title_en; // Outputs 'The Alchemist'
#### Saving a single translation
Yii::app()->language = 'sv';
$book->title = 'Djävulen bär Prada';
$book->save(); // Saves 'Djävulen bär Prada' as if it was assigned to Book.title_sv
#### Saving multiple translations
$book->title_en = 'The Devil Wears Prada';
$book->title_sv = 'Djävulen bär Prada';
$book->save(); // Saves both translations
#### More examples
...can be found in tests/codeception/unit/BasicTest.php
### Creating a UI for translators
#### Configuration
The default behavior when a translation is missing is to return the source message.
When we construct a translation UI, we want the fields to be `null` until they have a translation.
'import' => array(
...
'i18n-attribute-messages.components.MissingTranslationHandler',
...
),
'components' => array(
...
'attributeMessages' => array(
'class' => 'CDbMessageSource',
'onMissingTranslation' => array('MissingTranslationHandler', 'returnNull'),
),
...
),
#### Creating an input to change the source language content of the field "title"
<div class="row">
echo $form->labelEx($model,'_title');
echo $form->textField($model,'_title');
echo $form->error($model,'_title');
echo $form->labelEx($model,'title_sv');
echo $form->textField($model,'title_sv');
echo $form->error($model,'title_sv');
echo Yii::t('app', 'Content to translate');
echo CHtml::encode($model->_title);
echo $form->labelEx($model,'title_sv');
echo $form->textField($model,'title_sv');
echo $form->error($model,'title_sv');
echo $form->labelEx($model,'title');
echo $form->textField($model,'title');
echo $form->error($model,'title');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.