[solved] translation of single views / actions

Hello! :)

I have a question regarding translation of single views.

Lets say you create a single action + view in some subdirectory.

Just for example I take the login action:

app\mycode\actions\LoginAction.php

[spoiler]




namespace app\mycode\actions;


use Yii;

use yii\base\Action;


use app\mycode\models\LoginForm;


class LoginAction extends Action

{


    public function run()

    {

        if (!\Yii::$app->user->isGuest) {

            return $this->controller->goHome();

        }


        $model = new LoginForm();

        

        if($model->load(Yii::$app->request->post()) && $model->login()) {

            return $this->controller->goBack();

        } 

        else {

            return $this->controller->render('@app/mycode/views/login', [

                'model' => $model,

            ]);

        }

	}

}



[/spoiler]

This action has this related view:

app/mycode/views/login.php

[spoiler]




<?php

use yii\helpers\Html;

use yii\bootstrap\ActiveForm;


$this->title = 'MyCode Login';

$this->params['breadcrumbs'][] = $this->title;

?>


<div class="site-login">

    <h1><?= Html::encode($this->title) ?></h1>


    <p>Please fill out the following fields to login:</p>


    <?php $form = ActiveForm::begin([

        'id' => 'login-form',

        'options' => ['class' => 'form-horizontal'],

        'fieldConfig' => [

            'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",

            'labelOptions' => ['class' => 'col-lg-1 control-label'],

        ],

    ]); ?>


    <?= $form->field($model, 'username') ?>


    <?= $form->field($model, 'password')->passwordInput() ?>


    <?= $form->field($model, 'rememberMe', [

        'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",

    ])->checkbox() ?>


    <div class="form-group">

        <div class="col-lg-offset-1 col-lg-11">

            <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>

        </div>

    </div>


    <?php ActiveForm::end(); ?>


    <div class="col-lg-offset-1" style="color:#999;">

        You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>

        To modify the username/password, please check out the code <code>app\models\User::$users</code>.

    </div>

</div>



[/spoiler]

Now I want to translate this view file.

But if possible I want to store the translation under:

app/mycode/messages/

The guide suggested:

So I created: app/mycode/views/de/login.php

And it worked - but what if I have to do for example changes on the form inside the view?

Then I have to rewrite all the view files in the sub-directories.

Seems not like the best way to me.

So my questions are:

[list=1]

[*]Is there a nice way how I can use Yii:t inside this view and store the messages under /app/mycode/messages?

[*]Or can I somehow change the "basePath" for translations on the fly?

[*]What is the best solution / correct way to do this?

[/list]

I would appriciate some help here.

Thanks and best regards

IMHO translating the each single sentence instead of the whole view is more flexible and you can configure your translations to be in the folder of your choice using fileMap.

Ahhh!

Sure after reading the whole translation-guide again I understood.

Thanks for the hint! :)

Solution for above example I wrote:

app/config/web.php




'i18n' => [

    'translations' => [

        // ... your other translations ... 

        'mycode*' => [

            'class' => 'yii\i18n\PhpMessageSource',

            'basePath' => '@app/mycode/messages',

            'fileMap' => [

                'mycode.translation' => 'translation.php' 

            ]

        ]

    ],

], 



Then you can translate your files under app/mycode/* with:




Yii::t('mycode.translation', 'translation string');