Skip to content

Commit 4786795

Browse files
committed
Merge pull request #73 from moltam/feature/optional-parent-root-scan
Added option to specify whether scan the parent directory of the root, or the root directory itself.
2 parents dd7b21f + bd3d139 commit 4786795

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

Module.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,21 @@ class Module extends \yii\base\Module {
145145
* @var string the root directory of the scanning.
146146
*/
147147
public $root = '@app';
148+
149+
/**
150+
* @var bool Whether scan the defined `root` parent directory, or the folder itself.
151+
*
152+
* <b>IMPORTANT</b>: Changing this from `true` to `false` could cause loss of translated items, as
153+
* optimize action removes the missing items.
154+
*
155+
* If the configured root is `@app`:
156+
* - `true` means for advanced apps, that the scan runs on the parent directory, which is the root for the entire project.
157+
* This is the desired behavior.
158+
* - `true` means for basic apps, that the scan runs also on the parent directory, which is outside of the project folder
159+
* (as `@app` is equals to the project root). This is not desired behavior, it is preferred to change this option
160+
* to `false`.
161+
*/
162+
public $scanRootParentDirectory = true;
148163

149164
/**
150165
* @var string writeable directory used for keeping the generated javascript files.

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ A more complex example including database table with multilingual support is bel
9292
'translatemanager' => [
9393
'class' => 'lajax\translatemanager\Module',
9494
'root' => '@app', // The root directory of the project scan.
95+
'scanRootParentDirectory' => true, // Whether scan the defined `root` parent directory, or the folder itself.
96+
// IMPORTANT: for detailed instructions read the chapter about root configuration.
9597
'layout' => 'language', // Name of the used layout. If using own layout use 'null'.
9698
'allowedIPs' => ['127.0.0.1'], // IP addresses from which the translation interface is accessible.
9799
'roles' => ['@'], // For setting access levels to the translating interface.
@@ -118,7 +120,25 @@ A more complex example including database table with multilingual support is bel
118120
],
119121
```
120122

121-
IMPORTANT: If you want to modify the value of roles (in other words to start using user roles) you need to enable authManager in the common config.
123+
#### Configuring the scan root
124+
125+
The file scanner will scan the configured folders for translatable elements. The following two options
126+
determine the scan root directory: `root`, and `scanRootParentDirectory`. These options are defaults to
127+
values that works with the Yii 2 advanced project template. If you are using basic template, you have to modify
128+
these settings.
129+
130+
The `root` options tells which is the root folder for project scan. However if `scanRootParentDirectory` is set to `true`
131+
(which is the default value), the scan will run on the parent directory. This is desired behavior on advanced template,
132+
because the `@app` is the root for the current app, which is a subfolder inside the project (so the entire root of the
133+
project is the parent directory of `@app`).
134+
135+
For basic template the `@app` is also the root for the entire project. Because of this with the default value
136+
of `scanRootParentDirectory`, the scan runs outside the project folder. This is not desired behavior, and
137+
changing the value to `false` solves this.
138+
139+
**IMPORTANT: Changing the `scanRootParentDirectory` from `true` to `false` could cause loss of translated items,
140+
as optimize action removes the missing items.** Changing the root folder can cause also loss, so be sure to
141+
double check your configuration!
122142

123143
Using of [authManager](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html).
124144

services/scanners/ScannerFile.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,15 @@ public function __construct(Scanner $scanner) {
9494
/**
9595
* @inheritdoc Initialise the $files static array.
9696
*/
97-
public function init() {
98-
97+
public function init()
98+
{
9999
if (empty(self::$files[static::EXTENSION]) && in_array(static::EXTENSION, $this->module->patterns)) {
100-
self::$files[static::EXTENSION] = FileHelper::findFiles(realpath($this->_getRoot()), [
101-
'except' => $this->module->ignoredItems,
102-
'only' => [static::EXTENSION],
100+
$root = realpath($this->_getRoot());
101+
Yii::trace("Scanning " . static::EXTENSION . " files for language elements in: $root", 'translatemanager');
102+
103+
self::$files[static::EXTENSION] = FileHelper::findFiles($root, [
104+
'except' => $this->module->ignoredItems,
105+
'only' => [static::EXTENSION],
103106
]);
104107
}
105108

@@ -215,8 +218,14 @@ abstract protected function getLanguageItem($buffer);
215218
* Returns the root directory of the project scan.
216219
* @return string
217220
*/
218-
private function _getRoot() {
219-
return dirname(Yii::getAlias($this->module->root));
221+
private function _getRoot()
222+
{
223+
$root = Yii::getAlias($this->module->root);
224+
if ($this->module->scanRootParentDirectory) {
225+
$root = dirname($root);
226+
}
227+
228+
return $root;
220229
}
221230

222231
/**

0 commit comments

Comments
 (0)