Code Monkey home page Code Monkey logo

yii2-translatable's Introduction

Stand With Ukraine

Translatable behavior

Build Status Scrutinizer Code Quality Total Downloads Latest Stable Version StandWithUkraine

Translatable behavior aggregates logic of linking translations to the primary model.

Installation

The preferred way to install this extension is through composer.

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
public function behaviors()
{
    return [
        // ...
        'translatable' => [
            'class' => TranslatableBehavior::className(),
            // 'translationRelationName' => 'translations',
            // 'translationLanguageAttrName' => 'language',
            // 'attributeNamePattern' => '%name% [%language%]',
            'translationAttributeList' => [
                'title',
                'description',
            ],
        ],
    ];
}
  1. And use getTranslation() or translateTo() methods
// 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.

$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.

Sponsoring

Buy Me A Coffee

License

License

This project is released under the terms of the BSD-3-Clause license.

Copyright (c) 2017-2022, Yii Maker

yii2-translatable's People

Contributors

dependabot-preview[bot] avatar greeflas avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

Forkers

zerojuls

yii2-translatable's Issues

Бесконечная рекурсия

Если свойство в моделе, к которой прикреплено это поведение, не найдено, то возникает бесконечная рекурсия из-за обращения к методу canGetProperty, который в итоге вызывает сам себя.

https://github.com/yiimaker/yii2-translatable/blob/master/src/TranslatableBehavior.php#L192
https://github.com/yiimaker/yii2-translatable/blob/master/src/TranslatableBehavior.php#L201

Как было в оригинальном расширении:

https://github.com/creocoder/yii2-translateable/blob/master/src/TranslateableBehavior.php#L148
https://github.com/creocoder/yii2-translateable/blob/master/src/TranslateableBehavior.php#L156

В таком случае все прекрасно работает

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.