PHP code example of winter / wn-translate-plugin

1. Go to this page and download the library: Download winter/wn-translate-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

winter / wn-translate-plugin example snippets


class User extends Model
{
    public $implement = ['Winter.Translate.Behaviors.TranslatableModel'];

    public $translatable = ['name'];
}

$user = User::first();

// Outputs the name in the default language
echo $user->name;

$user->translateContext('fr');

// Outputs the name in French
echo $user->name;

$user = User::first();

// Sets the name in the default language
$user->name = 'English';

$user->translateContext('fr');

// Sets the name in French
$user->name = 'Anglais';

// Outputs the name in French
echo $user->lang('fr')->name;

// Gets a single translated attribute for a language
$user->getAttributeTranslated('name', 'fr');

// Sets a single translated attribute for a language
$user->setAttributeTranslated('name', 'Jean-Claude', 'fr');

public function boot() {
    Event::listen('backend.form.extendFieldsBefore', function($widget) {
        // Only apply listener to the Index controller, Page model, and when the formwidget isn't nested
        if (
            !($widget->getController() instanceof \Winter\Pages\Controllers\Index)
            || !($widget->model instanceof \Winter\Pages\Classes\Page)
            || $widget->isNested
        ) {
            return;
        }

        // Add fields
        $widget->tabs['fields']['viewBag[myField]'] = [
            'tab' => 'mytab',
            'label' => 'myLabel',
            'type' => 'text'
        ];

        // Translate fields
        $translatable = [
            'viewBag[myField]'
        ];

        // Merge the fields in the translatable array
        $widget->model->translatable = array_merge($widget->model->translatable, $translatable);

    });
}

$user = User::first();

$user->setTranslatableUseFallback(false)->lang('fr');

// Returns NULL if there is no French translation
$user->name;

public $translatable = [
    'name',
    ['slug', 'index' => true]
];

Post::transWhere('slug', 'hello-world')->first();

Post::transWhere('slug', 'hello-world', 'en')->first();

Event::listen('translate.localePicker.translateParams', function($page, $params, $oldLocale, $newLocale) {
    if ($page->baseFileName == 'your-page-filename') {
        return YourModel::translateParams($params, $oldLocale, $newLocale);
    }
});

public static function translateParams($params, $oldLocale, $newLocale) {
    $newParams = $params;
    foreach ($params as $paramName => $paramValue) {
        $record = self::transWhere($paramName, $paramValue, $oldLocale)->first();
        if ($record) {
            $newParams[$paramName] = $record->getAttributeTranslated($paramName, $newLocale);
        }
    }
    return $newParams;
}

Event::listen('translate.localePicker.translateQuery', function($page, $params, $oldLocale, $newLocale) {
    if ($page->baseFileName == 'your-page-filename') {
        return YourModel::translateParams($params, $oldLocale, $newLocale);
    }
});

Event::listen('winter.translate.themeScanner.afterScan', function (ThemeScanner $scanner) {
    // ...
});

Settings::instance()->getAttributeTranslated('your_attribute_name');

/**
 * Blog Post Model
 */
class Post extends Model
{

    // [...]

    /**
     * Softly implement the TranslatableModel behavior.
     */
    public $implement = ['@Winter.Translate.Behaviors.TranslatableModel'];

    /**
     * @var array Attributes that support translation, if available.
     */
    public $translatable = ['title'];

    // [...]

}

/**
 * Register new Twig variables
 * @return array
 */
public function registerMarkupTags()
{
    // Check the translate plugin is installed
    if (!class_exists('Winter\Translate\Behaviors\TranslatableModel'))
        return;

    return [
        'filters' => [
            '_' => ['Lang', 'get'],
            '__' => ['Lang', 'choice'],
        ]
    ];
}
bash
php artisan migrate
twig
{{ 'this is always english' | _({}, 'en') }}
bash
php artisan translate:scan
bash 
php artisan translate:scan --purge
twig
{{ form_open() }}
    <select id="languageSelect">
        <option value="none" hidden></option>
        {% for code, name in locales %}
            {% if code != activeLocale %}
                <option value="{{code}}" name="locale">{{code | upper }}</option>
            {% endif %}
        {% endfor %}
    </select>
{{ form_close() }}