Download the PHP package dejvidecz/yii2-translate-manager without Composer
On this page you can find all versions of the php package dejvidecz/yii2-translate-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package yii2-translate-manager
Yii2 - Translate Manager
Online Translations
Introduction
This module provides a simple translating interface for the multilingual elements of your project. It can auto-detect new language elements (project scan). Duplications are filtered out automatically during project scanning. Unused language elements can be removed from the database with a single click (database optimisation) and translations can be imported and exported. It is possible to translate client side messages too (those stored in JavaScript files) as the project scan collects language elements to be translated from JavaScript files as well.
It also allows you to translate text on the client side (on the live webpage) without having to log in to the translating interface. (frontendTranslation).
On the server side it can handle database or one-dimensional/multidimensional array elements and Yii::t functions. You can exclude files, folders or categories to prevent them from being translated.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Migration
Run the following command in Terminal for database migration:
Linux/Unix:
Windows:
Config
A simple exmple of turning on Yii database multilingual.
Turning on the TranslateManager Module:
Simple example:
A more complex example including database table with multilingual support is below:
Configuring the scan root
The root path can be an alias or a full path (e.g. @app
or /webroot/site/
).
The file scanner will scan the configured folders for translatable elements. The following two options
determine the scan root directory: root
, and scanRootParentDirectory
. These options are defaults to
values that works with the Yii 2 advanced project template. If you are using basic template, you have to modify
these settings.
The root
options tells which is the root folder for project scan. It can contain a single directory (string),
or multiple directories (in an array).
The scanRootParentDirectory
is used only if a single root directory is specified in a string.
IMPORTANT: Changing these options could cause loss of translated items, as optimize action removes the missing items. So be sure to double check your configuration!
a) Single root directory:
It is possible to define one root directory as string in the root
option. In this case the scanRootParentDirectory
will be used when determining the actual directory to scan.
If scanRootParentDirectory
is set to true
(which is the default value), the scan will run on the parent directory.
This is desired behavior on advanced template, because the @app
is the root for the current app, which is a subfolder
inside the project (so the entire root of the project is the parent directory of @app
).
For basic template the @app
is also the root for the entire project. Because of this with the default value
of scanRootParentDirectory
, the scan runs outside the project folder. This is not desired behavior, and
changing the value to false
solves this.
IMPORTANT: Changing the scanRootParentDirectory
from true
to false
could cause loss of translated items,
as the root will be a different directory.
For example:
root value |
scanRootParentDirectory value |
Scanned directory |
---|---|---|
/webroot/site/frontend |
true |
/webroot/site |
/webroot/site/frontend |
false |
/webroot/site/frontend |
b) Multiple root directories:
Multiple root directories can be defined in an array. In this case all items must point to the exact directory,
as scanRootParentDirectory
will be omitted.
For example:
Using of authManager
Examples:
PhpManager:
DbManager:
Front end translation:
Usage
Register client scripts
To translate static messages in JavaScript files it is necessary to register the files.
To register your scripts, call the following method in each action:
A simple example for calling the above method at each page load:
ToggleTranslate button
Simple example for displaying a button to switch to front end translation mode. (The button will only appear for users who have the necessary privileges for translating!)
A more complex example for displaying the button:
Placing multilingual elements in the source code.
JavaScript:
PHP methods:
PHP functions for front end translation:
IMPORTANT: The lajax\translatemanager\helpers\Language::t() (Lx::t()) function currently does not support the translation of HTMLattributes
PHP arrays:
PHP Database:
-
With new attributes:
-
With behavior (since 1.5.3):
Note: This will replace the model's original attribute values!
URLs
URLs for the translating tool:
Example implementation of the Yii2 menu into your own menu.
Console commands
Register the command
Use it with the Yii CLI
Using google translate api
Google translate api is a paid service. At the moment of writing the price is $20 USD per 1 million characters trsanslated.
In order to activate the feature you need to have Google account, generate google Api Key, and enable this feature by adding 'googleApiKey' to 'translatemanager' module configuration:
Once feature is enabled it will insert google translation of the source into the empty translation field (instead of original text) when you click on source field.
Known issues
-
Scanner is scanning parent root directory #12.
You can overwrite this behavior with the
scanRootParentDirectory
option. (See Config section for details.) - Frontend translation of strings in hidden tags corrupts HTML. #45
Coding style
The project uses the PSR-2 coding standard.
Coding style issues can be fixed using the following command:
You can check the code, without affecting it: