Download the PHP package neam/yii-i18n-columns without Composer

On this page you can find all versions of the php package neam/yii-i18n-columns. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yii-i18n-columns

Yii Extension: I18nColumns

Transparent language/locale-dependent attributes and relations for ActiveRecords, without using lookup tables for translated field contents.

Ideal when translated attributes are supposed to be native in the active records' database tables, such as translated foreign keys, or multilingual look-up/search columns.

Features

Limitations

Use only with a smaller number of attributes/languages or else database restrictions on row size and/or column counts will be exceeded. For the bulk of translated attributes, use https://github.com/neam/yii-i18n-attribute-messages or mike's translatable behavior.

Requirements

Setup

Download and install

Ensure that you have the following in your composer.json:

"repositories":[
    {
        "type": "vcs",
        "url": "https://github.com/neam/yii-i18n-columns"
    },
    ...
],
"require":{
    "neam/yii-i18n-columns":"dev-master",
    ...
},

Then install through composer:

php composer.phar update neam/yii-i18n-columns

If you don't use composer, clone or download this project into /path/to/your/app/vendor/neam/yii-i18n-columns

Add Alias to both main.php and console.php

'aliases' => array(
    ...
    'vendor'  => dirname(__FILE__) . '/../../vendor',
    'i18n-columns' => 'vendor.neam.yii-i18n-columns',
    ...
),

Import the behavior in main.php

'import' => array(
    ...
    'i18n-columns.behaviors.I18nColumnsBehavior',
    ...
),

Reference the translate command in console.php

'commandMap' => array(
    ...
    'i18n-columns'    => array(
        'class' => 'i18n-columns.commands.I18nColumnsCommand',
    ),
    ...
),

Configure models to be multilingual

1. Add the behavior to the models that you want multilingual

public function behaviors()
{
    return array(
        'i18n-columns' => array(
             'class' => 'I18nColumnsBehavior',
             /* The multilingual attributes */
             'translationAttributes' => array(
                  'title',
                  'slug',
                  'image_id',
                  'etc',
             ),
            /* Specify multilingual belongsTo relations in the form 'RelatedModel' => array('relationName' => 'foreignKey') */
            'multilingualRelations' => array(
                'Image' => array('image' => 'image_id'),
            ),
        ),
    );
}

2. Create migration from command line:

./yiic i18n-columns process

Prior to this, you should already have configured a default language ($config['language']) and available languages ($config['components']['langHandler']['languages']) for your app.

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_defaultlanguagecode and add columns for the remaining languages.

Sample migration file:

<?php
class m130708_165204_i18n extends CDbMigration
{
    public function up()
    {
    $this->renameColumn('section', 'title', 'title_en');
    $this->renameColumn('section', 'slug', 'slug_en');
    $this->addColumn('section', 'title_sv', 'varchar(255) null');
    $this->addColumn('section', 'slug_sv', 'varchar(255) null');
    $this->addColumn('section', 'title_de', 'varchar(255) null');
    $this->addColumn('section', 'slug_de', 'varchar(255) null');
    }

    public function down()
    {
      $this->renameColumn('section', 'title_en', 'title');
      $this->renameColumn('section', 'slug_en', 'slug');
      $this->dropColumn('section', 'title_sv');
      $this->dropColumn('section', 'slug_sv');
      $this->dropColumn('section', 'title_de');
      $this->dropColumn('section', 'slug_de');
    }
}

4. Re-generate models

Use Gii as per the official documentation. After this, you have multilingual Active Records at your disposal :)

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.

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' 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/unit/I18nColumnsTest.php

Changelog

0.3.1 (development release)

0.3.0 (development release)

0.2.0 (latest stable)

0.1.0

0.0.0

Credits

FAQ

Why use suffixed columns instead of one or many lookup tables?

1. Compatibility with Gii and other Yii extensions

Your multilingual models will keep working as ordinary models, albeit with more fields than before. You can generate new CRUD and instantly have a translation interface for all your languages.

This means that you will quickly be able to add multilingual content earlier in the development cycle.

Having a simple multilingual datamodel most likely means better compatibility with other extensions offering magic tooling, such as saving many-many relations, providing search/filtering features, form-generators, editable grid views, etc. It is our experience that these extensions need more custom fitting the higher the amount of joins necessary to show relevant information.

2. You are no longer dependent on magic tooling (such as ActiveRecord)

There is no need to create advanced join-helpers to access the translated attributes, they are simply attributes in the table to begin with. Thus, creating SQL to interact with translations is very straightforward:

SELECT id, title_en AS title FROM book WHERE title_en = 'The Alchemist';

This may not be a notable difference when you prototype your application, but becomes more important when you move away from ActiveRecord and write queries using QueryBuilder, pure SQL, or in a barebone PHP/C layer/app side by side with your Yii application (for performance reasons).

3. Decreased complexity = Flexibility

Say you have a query that without translated fields requires 7 joins, a group by clause and 1-2 subqueries. Then add the necessity to add one more join for each translated field and still achieve a high-performant query. It certainly is possible, but with the cost of added complexity.

In essence, the concept of having suffixed columns instead of translation table(s) is similar to the concept of code generation. You add extra complexity to generate the code/datamodel and receive the benefit of a code base that is easier to maintain and customize.

4. Why not?

See "Limitations" above.

Also, if the system should be developer independent once shipped, adding languages on-the-fly will need to be automated with a script similar to:

#!/bin/bash
./yiic i18n-columns
./yiic migrate
./path/to/giic generate application.config.giic.crud

In general, it may not fit larger projects that are aimed at simple translated frontends, but will be more suitable when the main priority is to quickly build feature-complete backends for managing and translating multilingual content, possibly for which the data model changes often. Migrations and CRUD generation is a part of the daily workflow while developing such solutions, and simplicity and easy customization of the generated code often more important than developer independence.

Running tests

cd vendor/neam/yii-i18n-columns
php path/to/composer.phar install --dev
cd tests
../vendor/bin/phpunit --verbose --debug

This should result in an output similar to:

PHPUnit 3.7.22 by Sebastian Bergmann.

Configuration read from /path/to/app/vendor/neam/yii-i18n-columns/tests/phpunit.xml

Starting test 'I18nColumnsTest::ensureEmptyDb'.
.
Starting test 'I18nColumnsTest::getWithoutSuffix'.
.
Starting test 'I18nColumnsTest::setWithoutSuffix'.
.
Starting test 'I18nColumnsTest::saveSingleWithoutSuffix'.
.
Starting test 'I18nColumnsTest::fetchSingleWithoutSuffix'.
.
Starting test 'I18nColumnsTest::saveMultiple'.
.

Time: 0 seconds, Memory: 10.00Mb

OK (6 tests, 28 assertions)

All versions of yii-i18n-columns with dependencies

PHP Build Version
Package Version
Requires php Version >=5.0.0
yiisoft/yii Version >=1.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package neam/yii-i18n-columns contains the following files

Loading the files please wait ....