Download the PHP package neam/yii-i18n-attribute-messages without Composer
On this page you can find all versions of the php package neam/yii-i18n-attribute-messages. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download neam/yii-i18n-attribute-messages
More information about neam/yii-i18n-attribute-messages
Files in neam/yii-i18n-attribute-messages
Package yii-i18n-attribute-messages
Short Description Transparent attribute translation for ActiveRecords, leveraging Yii's built-in translation features for translated field contents.
License MIT
Homepage https://github.com/neam/yii-i18n-attribute-messages
Informations about the package yii-i18n-attribute-messages
Yii Extension: I18nAttributeMessages
Transparent attribute translation for ActiveRecords, leveraging Yii's built-in translation features to retrieve translated attribute contents.
All you'll need to do is to rename the fields from $book->title
to $book->_title
in your database. The included console command scans your database and configuration and creates a migration for all necessary renames.
The behavior then transparently turns $book->title
into Yii:t('attributes.Book.title', $book->_title)
and $book->title_de
into Yii:t('attributes.Book.title', $book->_title, array(), null, 'de')
, while providing transparent saving of translations simply by assigning and saving these attributes in the model (Note: CDbMessageSource only).
Features
- Eases the translation of user-generated content in a project
- Eases the creation of UI for translators to perform translation work
- Works with any Yii-compatible message source when retrieving translations
- Saving of translations when using CDbMessageSource
- Console command automatically creates migrations for the necessary database changes
- The source message is left in the model for Gii compatibility (generated models will have the correct validation rules and field order for the translated attributes)
- Rigorous unit tests
- Use with any number of attributes/languages without worrying about database restrictions on row size and/or column counts being exceeded
Limitations
Not ideal for translated attributes that are supposed to be native in the active records' database tables, such as translated foreign keys, or multilingual look-up/search columns. Use https://github.com/neam/yii-i18n-columns for those attributes instead.
Requirements
- Yii 1.1 or above
- Use of Yii console
Setup
Download and install
Ensure that you have the following in your composer.json:
"repositories":[
{
"type": "vcs",
"url": "https://github.com/neam/yii-i18n-attribute-messages"
},
...
],
"require":{
"neam/yii-i18n-attribute-messages":"dev-master",
...
},
Then install through composer:
php composer.phar update neam/yii-i18n-attribute-messages
If you don't use composer, clone or download this project into /path/to/your/app/vendor/neam/yii-i18n-attribute-messages
Add Alias to both main.php and console.php
'aliases' => array(
...
'vendor' => dirname(__FILE__) . '/../../vendor',
'i18n-attribute-messages' => 'vendor.neam.yii-i18n-attribute-messages',
...
),
Import the behavior in main.php
'import' => array(
...
'i18n-attribute-messages.behaviors.I18nAttributeMessagesBehavior',
...
),
Reference the console command in console.php
'commandMap' => array(
...
'i18n-attribute-messages' => array(
'class' => 'i18n-attribute-messages.commands.I18nAttributeMessagesCommand',
),
...
),
Configure models to be multilingual
1. Add the behavior to the models that you want multilingual
public function behaviors()
{
return array(
'i18n-attribute-messages' => array(
'class' => 'I18nAttributeMessagesBehavior',
/* The multilingual attributes */
'translationAttributes' => array(
'title',
'slug',
'book_id',
//'etc',
),
/* An array of allowed language/locale ids that are to be used as suffixes, such as title_en, title_de etc */
//'languageSuffixes' => array_keys(Yii::app()->params["languages"]),
/* Configure if you want to use another translation component for this behavior. Default is 'messages' */
//'messageSourceComponent' => 'attributeMessages',
),
);
}
2. Create migration from command line:
./yiic i18n-attribute-messages process
Run with --verbose
to see more detailed output.
3. Apply the generated migration:
./yiic migrate
This will rename the fields that are defined in translationAttributes to _fieldname, which will be the placed that the source content is stored (the content that is to be translated).
Sample migration file:
</div>
Note: This field is generated automatically by Gii.
Creating an input to set/update the swedish translation of the field "title"
<div class="row">
</div>
Hint: You might want to display the source language content next to the translation field, like so:
<div class="row">
:
</div>
<div class="row">
</div>
Also, don't forget to adjust the validation rules (safe, required, etc) for the virtual translation fields.
Creating an input to set/update the current app language's translation of the field "title"
<div class="row">
</div>
More examples
Simply look at any other examples of form building in Yii. Since the translated attributes are ordinary model attributes, you may use core or third-party extensions that save to and read from model attributes for constructing your translation UI.
Changelog
0.1.0
- Eases the translation of user-generated content in a project
- Eases the creation of UI for translators to perform translation work
- Works with any Yii-compatible message source when retrieving translations
- Saving of translations when using CDbMessageSource
- Console command automatically creates migrations for the necessary database changes
- The source message is left in the model for Gii compatibility (generated models will have the correct validation rules and field order for the translated attributes)
- Rigorous unit tests
0.0.0
- Forked https://github.com/neam/yii-i18n-columns v0.3.1
Testing the extension
One-time preparations
Switch to the extension's root directory
cd vendor/neam/yii-i18n-attribute-messages
Create a database called yiam_test in your local mysql server installation. Create a user called yiam_test with yiam_test as the password and make sure that this user has access to the local database.
After this, you can run the following routine to test the extension:
Test the command
1. Set-up the test database
Load tests/db/unmodified.sql into the database.
2. Run the console command
tests/app/protected/yiic i18n-attribute-messages process
3. Apply the migration
tests/app/protected/yiic migrate
Test the behavior
Run the unit tests
php codecept.phar run unit
You should get output similar to:
Codeception PHP Testing Framework v1.6.2
Powered by PHPUnit 3.7.19 by Sebastian Bergmann.
Suite unit started
Trying to ensure empty db (BasicTest::ensureEmptyDb) - Ok
Trying to ensure known source language (BasicTest::ensureKnownSourceLanguage) - Ok
Trying to see behavior (BasicTest::seeBehavior) - Ok
Trying to interpret language suffix (BasicTest::interpretLanguageSuffix) - Ok
Trying to get (BasicTest::get) - Ok
Trying to set without suffix (BasicTest::setWithoutSuffix) - Ok
Trying to set with suffix (BasicTest::setWithSuffix) - Ok
Trying to save single with source message (BasicTest::saveSingleWithSourceMessage) - Ok
Trying to save single without source message (BasicTest::saveSingleWithoutSourceMessage) - Ok
Trying to fetch single without suffix (BasicTest::fetchSingleWithoutSuffix) - Ok
Trying to reuse previous translation (BasicTest::reusePreviousTranslation) - Ok
Trying to update existing (BasicTest::updateExisting) - Ok
Trying to further fallback behavior tests (BasicTest::furtherFallbackBehaviorTests) - Ok
Trying to test test suite (EmptyTest::testTestSuite) - Ok
Time: 0 seconds, Memory: 14.25Mb
OK (14 tests, 124 assertions)