w3lifer/yii2-i18n-js

Installs: 8 731

Dependents: 1

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

2.2.1 2018-06-21 20:23 UTC

This package is auto-updated.

Last update: 2024-04-27 00:09:58 UTC


README

Installation

composer require w3lifer/yii2-i18n-js
  1. Add this to your application configuration:
<?php

return [
    // ...
    'components' => [
        // ...
        'i18nJs' => [
            'class' => 'w3lifer\yii2\I18nJs',
        ],
        // ...
    ],
    // ...
];
  1. Initialize the component anywhere, for example in @app/views/layouts/main.php:
Yii::$app->i18nJs;

Note, you do not need to register the component in the places that will be processed with AJAX-requests (for example, in @app/config/web.php -> bootstrap, on afterRequest, etc), because it will be loaded twice, and it makes no sense.

Usage

window.addEventListener('DOMContentLoaded', function () {
  console.log(yii.t('app', 'Hello'));
  console.log(yii.t('app', 'Hello, World!'));
  console.log(yii.t('app', 'Hello, {username}!', {username: 'John'}));
  console.log(yii.t('app', 'Hello, {0}!', ['John']));
  console.log(yii.t('app', 'Hello, {0} {1}!', ['John', 'Doe']));
  console.log(yii.t('app', 'Hello, {0} {1}!', ['John', 'Doe'], 'en-US'));
});