Skip to content

Latest commit

 

History

History
101 lines (86 loc) · 2.78 KB

I18N.md

File metadata and controls

101 lines (86 loc) · 2.78 KB

I18n db message source

Internationalization tarantool data source allows use tarantool database for storing translate messages.

Configuration

Setup globally for all categories:

It will work as default message source

return [
    'components' => [
        'i18n' => [
            'translations' => [
                '*' => [
                    'class' => 'mhthnz\tarantool\i18n\MessageSource',
                    // 'db' => 'tarantool',
                    // 'enableCaching' => false,
                    // 'cache' => 'tarantoolCache',
                ],
            ],
        ],
    ],
]

Setup for app category:

Wildcard is allowed

return [
    'components' => [
        'i18n' => [
            'translations' => [
                'app*' => [
                    'class' => 'mhthnz\tarantool\i18n\MessageSource',
                    // 'db' => 'tarantool',
                    // 'enableCaching' => false,
                    // 'cache' => 'tarantoolCache',
                ],
            ],
        ],
    ],
]

Init i18n tables

hostname@user:~$ php yii tarantool-migrate --migrationNamespaces=\\mhthnz\\tarantool\\i18n\\migrations

You may want to change some migration settings such as:

  • Message table name
  • Source message table name
  • Table engine

Just create a new migration and inherit the migration class from \mhthnz\tarantool\i18n\migrations\m230401_092642_create_i18n_tables:

class m230330_105111_create_i18n_tables extends \mhthnz\tarantool\i18n\migrations\m230401_092642_create_i18n_tables
{
    public $engine = 'vinyl';
    public $sourceMessageTable = '{{%another_source_message_table}}';
    //.....
}

Usage

It works as other translation sources, check out docs link above

echo \Yii::t('app', 'translate msg');

// Using placeholders
$username = 'Alexander';
// display a translated message with username being "Alexander"
echo \Yii::t('app', 'Hello, {username}!', [
    'username' => $username,
]);

// Specify another category
echo \Yii::t('app/user/registration', 'Thanks for registration!');

// Formatting placeholders
$price = 100;
echo \Yii::t('app', 'Price: {0,number,currency}', $price);

echo \Yii::t('app', 'Today is {0,date}', time());

// may produce "42 is spelled as forty-two"
echo \Yii::t('app', '{n,number} is spelled as {n,spellout}', ['n' => 42]);

$n = 3;
echo Yii::t('app', 'You are the {n,selectordinal,one{#st} two{#nd} few{#rd} other{#th}} visitor', ['n' => $n]);
// For English it outputs:
// You are the 3rd visitor