-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
1,255 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
filter: | ||
excluded_paths: | ||
- tests/* | ||
checks: | ||
php: | ||
code_rating: true | ||
duplication: false | ||
unused_methods: true | ||
unused_parameters: true | ||
argument_type_checks: true | ||
verify_property_names: true | ||
method_calls_on_non_object: false | ||
fix_doc_comments: true | ||
instanceof_class_exists: true | ||
catch_class_exists: true | ||
assignment_of_null_return: false | ||
use_statement_alias_conflict: false | ||
simplify_boolean_return: true | ||
return_doc_comments: true | ||
return_doc_comment_if_not_inferrable: true | ||
remove_extra_empty_lines: true | ||
remove_php_closing_tag: true | ||
parameters_in_camelcaps: true | ||
parameter_doc_comments: true | ||
param_doc_comment_if_not_inferrable: true | ||
properties_in_camelcaps: true | ||
no_long_variable_names: | ||
maximum: '27' | ||
no_goto: true | ||
more_specific_types_in_doc_comments: true | ||
fix_use_statements: | ||
remove_unused: true | ||
preserve_multiple: false | ||
preserve_blanklines: true | ||
order_alphabetically: false | ||
fix_line_ending: true | ||
fix_php_opening_tag: true | ||
|
||
coding_style: | ||
php: | ||
spaces: | ||
around_operators: | ||
concatenation: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
language: php | ||
|
||
php: | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
- 7.1 | ||
- 7.2 | ||
|
||
# environment variable used in test suite | ||
env: TEST_ENVIRONMENT=travis | ||
|
||
# faster builds on new travis setup not using sudo | ||
sudo: false | ||
|
||
# cache vendor dirs | ||
cache: | ||
directories: | ||
- vendor | ||
- $HOME/.composer/cache | ||
|
||
# install dependencies | ||
install: | ||
- travis_retry composer self-update && composer --version | ||
- travis_retry composer update --prefer-dist --no-interaction | ||
|
||
# run tests | ||
script: | ||
- composer validate --no-check-lock | ||
- composer test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<p align="center"> | ||
<a href="https://github.com/yiimaker" target="_blank"> | ||
<img src="https://avatars1.githubusercontent.com/u/24204902" height="100px"> | ||
</a> | ||
<h1 align="center">Translatable behavior</h1> | ||
</p> | ||
|
||
[![Build Status](https://travis-ci.org/yiimaker/yii2-translatable.svg?branch=master)](https://travis-ci.org/yiimaker/yii2-translatable) | ||
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/yiimaker/yii2-translatable/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/yiimaker/yii2-translatable/?branch=master) | ||
[![Total Downloads](https://poser.pugx.org/yiimaker/yii2-translatable/downloads)](https://packagist.org/packages/yiimaker/yii2-translatable) | ||
[![Latest Stable Version](https://poser.pugx.org/yiimaker/yii2-translatable/v/stable)](https://packagist.org/packages/yiimaker/yii2-translatable) | ||
[![Latest Unstable Version](https://poser.pugx.org/yiimaker/yii2-translatable/v/unstable)](https://packagist.org/packages/yiimaker/yii2-translatable) | ||
|
||
Translatable behavior aggregates logic of linking translations to the primary model. | ||
|
||
Installation | ||
------------ | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
$ composer require yiimaker/yii2-translatable | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"yiimaker/yii2-translatable": "~1.0" | ||
``` | ||
|
||
to the `require` section of your `composer.json`. | ||
|
||
Usage | ||
----- | ||
|
||
1. Add behavior to the your primary model | ||
|
||
```php | ||
public function behaviors() | ||
{ | ||
return [ | ||
// ... | ||
'translatable' => [ | ||
'class' => TranslatableBehavior::className(), | ||
// 'translationRelationName' => 'translations', | ||
// 'translationLanguageAttrName' => 'language', | ||
// 'attributeNamePattern' => '%name% [%language%]', | ||
'translationAttributeList' => [ | ||
'title', | ||
'description', | ||
], | ||
], | ||
]; | ||
} | ||
``` | ||
|
||
2. And use `getTranslation()` or `translateTo()` methods | ||
|
||
```php | ||
// product is an active record model with translatable behavior | ||
$product = new Product(); | ||
|
||
// sets translation for default application language | ||
$product->title = 'PhpStrom 2018.1'; | ||
$product->description = 'Лицензия PhpStrom IDE версия 2018.1'; | ||
|
||
// gets translation for English language | ||
$translation = $product->getTranslation('en'); | ||
$translation->title = 'PhpStrom 2018.1'; | ||
$translation->description = 'License of the PhpStrom IDE version 2018.1'; | ||
|
||
// sets description for French language | ||
$product->translateTo('fr')->description = 'La licence de PhpStorm IDE la version 2018.1'; | ||
|
||
$product->insert(); | ||
``` | ||
|
||
`translateTo()` it's just an alias for `getTranslation()` method. | ||
|
||
After saving the model you can fetch this model from the database and translatable behavior will fetch all translations automatically. | ||
|
||
```php | ||
$product = Product::find() | ||
->where(['id' => 1]) | ||
->with('translations') | ||
->one() | ||
; | ||
|
||
// gets translation for English language | ||
$product->translateTo('en')->description; // License of the PhpStrom IDE version 2018.1 | ||
// gets translation for French language | ||
$product->translateTo('fr')->description; // La licence de PhpStorm IDE la version 2018.1 | ||
|
||
// check whether Ukrainian translation not exists | ||
if (!$product->hasTranslation('uk')) { | ||
$product->translateTo('uk')->description = 'Ліцензія PhpStrom IDE версія 2018.1'; | ||
} | ||
|
||
// update Enlish translation | ||
$product->translateTo('en')->title = 'PhpStorm IDE'; | ||
|
||
$product->update(); | ||
``` | ||
|
||
Tests | ||
----- | ||
|
||
You can run tests with composer command | ||
|
||
``` | ||
$ composer test | ||
``` | ||
|
||
or using following command | ||
|
||
``` | ||
$ codecept build && codecept run | ||
``` | ||
|
||
Contributing | ||
------------ | ||
|
||
For information about contributing please read [CONTRIBUTING.md](CONTRIBUTING.md). | ||
|
||
License | ||
------- | ||
|
||
[![License](https://poser.pugx.org/yiimaker/yii2-translatable/license)](https://packagist.org/packages/yiimaker/yii2-translatable) | ||
|
||
This project is released under the terms of the BSD-3-Clause [license](LICENSE). | ||
|
||
Copyright (c) 2017-2018, Yii Maker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
actor: Tester | ||
paths: | ||
tests: tests | ||
log: tests/_output | ||
data: tests/_data | ||
support: tests/_support | ||
settings: | ||
bootstrap: _bootstrap.php | ||
colors: true | ||
memory_limit: 1024M | ||
extensions: | ||
enabled: | ||
- Codeception\Extension\RunFailed | ||
modules: | ||
config: | ||
Yii2: | ||
cleanup: false | ||
configFile: 'tests/_config/app.php' | ||
Db: | ||
dsn: 'sqlite:tests/_output/test.db' | ||
user: '' | ||
password: '' | ||
dump: 'tests/_data/dump.sql' | ||
cleanup: true, | ||
populate: true, | ||
reconnect: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.