This Extension is based in lajax/yii2-translate-manager that was ported for works with Bootstrap 5 by KrishDemonDog.
This extension provides a simple translating interface for the multilingual elements of your project. This extension offers:
- Scan - Automatically detect new elements of the language. Duplications are filtered out automatically during project scanning.
- Optimize - Unused language elements will be removed from the database.
- Import - Import your translations from
.json
or.xml
file. - Export - Export your translations to
.json
or.xml
file. - You can exclude files, folders or categories to prevent them from being translated.
Please read and follow the instructions in the Contributing guide.
Via Composer
composer require demondogsl/yii2-translate-manager "*"
Run the following command in Terminal for database migration:
php yii migrate --migrationPath vendor/demondogsl/yii2-translate-manager/migrations
Turn on the Translate Manager:
- In your project file, Yii2 Advanced
/backend/config/main.php
Yii2 Basic/config/web.php
add Translate Manager inmodules
.
IMPORTANT Optional values overwrite Default Values.
'modules' => [
'translateManager' => [
'class' => 'DemonDogSL\translateManager\Module', // required
'root' => '@example', // optional Directory of the project scan. Can be an Array with other instances. For example: ['@backend', '@common', '@frontend']. For Default is '@app'
'allowedIPs' => ['0.0.0.0'], // optional IP addresses from which the translation interface is accessible. For Default is ['127.0.0.1']
'roles' => ['@'], // optional For setting access levels to the translating interface. For Default is []
'tmpDir' => '@example', // optional Writable directory for the client-side temporary javascript language files. Can be '@backend/runtime'. For Default is '@runtime'
'ignoredCategories' => ['example'], // optional These categories won't be scanned. For Default is ['yii']
'onlyCategories' => ['example'], // optional Only these categories will be scanned. For Default is []
'ignoredItems' => ['example'], // optional These files will not be processed. For Default is ['.svn', '.git', '.gitignore', '.gitkeep', '.hgignore', '.hgkeep', '/messages', '/BaseYii.php', 'runtime', 'bower', 'nikic']
'tables' => [ // optional Database Tables that will be scanned. For Default not exists
[
'connection' => 'db',
'table' => '{{%example}}', // Table name
'columns' => ['example1', 'example2'], // Names of columns
'category' => 'database-table-name',
]
],
],
],
- In our migration we added table
language_force_translation
for translate your ENUM values of your tables, if you want use it only add next code.
'tables' => [
[
'connection' => 'db',
'table' => 'language_force_translation',
'columns' => ['value'],
'category' => 'database-table-name'
]
]
- In your project file, Yii2 Advanced
/backend/config/main.php
Yii2 Basic/config/web.php
add Translate Manager incomponents
.
'components' => [
'translateManager' => [
'class' => 'DemonDogSL\translateManager\Component'
]
]
- In your project file, Yii2 Advanced
/backend/config/main.php
Yii2 Basic/config/web.php
add Translate Manager inbootstrap
.
'bootstrap' => [
'translateManager' => [
'class' => 'DemonDogSL\translateManager\Component'
],
],
- In your project file, Yii2 Advanced
/backend/config/main.php
Yii2 Basic/config/web.php
add I18n incomponents
and yourlanguage
.
'language' => 'en-GB',
'components' => [
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\DbMessageSource',
'db' => 'db',
'sourceLanguage' => 'xx-XX', // Your language
'sourceMessageTable' => '{{%language_source}}',
'messageTable' => '{{%language_translate}}',
'cachingDuration' => 86400,
'enableCaching' => true,
],
],
],
],
To translate static messages in JavaScript files it is necessary to register the files. You have two ways to do it.
Call the following method in each action:
\DemonDogSL\translateManager\helpers\Language::registerAssets();
Or create a Controller that extends Yii Controller
IMPORTANT All your Controllers must extend this New Controller
namespace backend\controllers;
use DemonDogSL\translateManager\helpers\Language;
class Controller extends \yii\web\Controller {
public function init() {
Language::registerAssets();
parent::init();
}
}
This is Optional.
Display a button to switch text to translation mode.
// Default
echo \DemonDogSL\translateManager\widgets\ToggleTranslate::widget();
// If you want change position
echo \DemonDogSL\translateManager\widgets\ToggleTranslate::widget([
'position' => 'example', // optional Can be 'top-left', 'top-right', 'bottom-left' or 'bottom-right'. For Default is 'bottom-left'
]);
- JavaScript
ddt.t('Hello');
ddt.t('Hello {name}!', {name:'World'});
- PHP
Yii::t('example', 'Hello');
Yii::t('example', 'Hello {name}!', ['name' => 'World']);
- Translate Mode Button
IMPORTANT Translate Mode Button does not support the translation of HTML attributes
use DemonDogSL\translateManager\models\DDT;
DDT::t('example', 'Hello');
DDT::t('example', 'Hello {name}!', ['name' => 'World']);
/translateManager/language/list
/translateManager/language/scan
/translateManager/language/optimize
/translateManager/language/import
/translateManager/language/export
Please see Changelog for more information on what has changed recently.
Same licence as lajax/yii2-translate-manager
The MIT License (MIT). Please see License for more information.