Download the PHP package hsoft/yii2-translate-manager without Composer
On this page you can find all versions of the php package hsoft/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
Translation management extension for Yii 2
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.
Contributing
Please read and follow the instructions in the Contributing guide.
Installation
Via Composer
Migration
Run the following command in Terminal for database migration:
Or use the namespaced migration (requires at least Yii 2.0.10):
Then run:
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 hsoft\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):
This behavior does the following:
- Replaces the specified attributes with translations after the model is loaded.
- Saves the attribute values as:
- Source messages, if the current language is the source language.
- Translations, if the current language is different from the source language. This way the value stored in database is not overwritten with the translation.
Note: If the model should be saved as translation, but the source message does not exist yet in the database then the message is saved as the source message whether the current language is the source language or not. To avoid this scan the database for existing messages when using the behavior first, and only save new records when the current language is the source language.
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
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:
Change log
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.
Screenshots
List of languages
Scanning project
Optimise database
Translate on the admin interface
Front end in translating mode
Translate on the front end
Links
All versions of yii2-translate-manager with dependencies
yiisoft/yii2-jui Version ~2.0.0
yiisoft/yii2-bootstrap5 Version *