Skip to content

Commit

Permalink
static translations calls for modules in order to fix extension
Browse files Browse the repository at this point in the history
translation problems #1448
  • Loading branch information
nadar committed Sep 5, 2017
1 parent be56aa1 commit 551e8ce
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 125 deletions.
117 changes: 73 additions & 44 deletions core/base/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ abstract class Module extends \yii\base\Module
* Yii::t('luya/admin', 'MyVariableInAdminPhp');
* ```
*/
public $translations = [];
public static $translations = [];

/**
* @inheritdoc
Expand All @@ -137,49 +137,7 @@ public function init()
}
}

$this->registerTranslationArray($this->translations);
}

/**
* Internal used to register the translations from the translation array.
* @param array $translations
*/
protected function registerTranslationArray(array $translations)
{
foreach ($translations as $translation) {
$this->registerTranslation($translation['prefix'], $translation['basePath'], $translation['fileMap']);
}
}

/**
* Register a Translation to the i18n component.
*
* In order to register Translations you can either use the $translations array or calling this method
* straight after the init() method:
*
* ```php
* public function init()
* {
* parent::init();
*
* $this->registerTranslation('mymodule*', '@mymoduleid/messages', [
* 'mymodule' => 'mymodule.php',
* 'mymodule/sub' => 'sub.php',
* ]);
* }
* ```
*
* @param string $prefix
* @param string $basePath
* @param array $fileMap
*/
public function registerTranslation($prefix, $basePath, array $fileMap)
{
Yii::$app->i18n->translations[$prefix] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => $basePath,
'fileMap' => $fileMap,
];
static::onLoad();
}

/**
Expand Down Expand Up @@ -307,4 +265,75 @@ public function getControllerPath()
{
return Yii::getAlias('@' . str_replace('\\', '/', $this->controllerNamespace), false);
}

// STATIC METHODS

/**
* Internal used to register the translations from the translation array or set alias paths.
*
* This is a static behavior, so we can call this call without the object context, for example when
* the composer plugin registers blocks but the module is not registered with translations.
*
* @return void
*/
public static function onLoad()
{
}

/**
* Register a Translation to the i18n component.
*
* In order to register Translations you can either use the $translations array or calling this method
* straight after the init() method:
*
* ```php
* public function init()
* {
* parent::init();
*
* $this->registerTranslation('mymodule*', '@mymoduleid/messages', [
* 'mymodule' => 'mymodule.php',
* 'mymodule/sub' => 'sub.php',
* ]);
* }
* ```
*
* @param string $prefix
* @param string $basePath
* @param array $fileMap
*/
public static function registerTranslation($prefix, $basePath, array $fileMap)
{
Yii::$app->i18n->translations[$prefix] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => $basePath,
'fileMap' => $fileMap,
];
}

/**
* Get base path from static view port.
*
* @return string
*/
public static function staticBasePath()
{
$class = new \ReflectionClass(get_called_class());

return dirname($class->getFileName());
}

/**
*
* @param unknown $category
* @param unknown $message
* @param array $params
* @param unknown $language
* @return string
*/
public static function baseT($category, $message, array $params = [], $language = null)
{
static::onLoad();
return Yii::t($category, $message, $params, $language);
}
}
30 changes: 20 additions & 10 deletions docs/guide/app-translation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,32 @@ Now all the message with the prefix `app` will be loaded into the message compon
└── app-otherparts.php
```

In order to register an {{luya\base\Module}} translation call the {{luya\base\Module::registerTranslation}} method after the init() of your Module as followed:
## Module Translations

In order to register an {{luya\base\Module}} translation call the {{luya\base\Module::onLoad()}} method, there you can also define an alias you can reuse.

```php
public function init()
class Module extends \luya\base\Module
{
parent::init();

$this->registerTranslation('mymodule*', '@mymoduleid/messages', [
'mymodule' => 'mymodule.php',
'mymodule/sub' => 'sub.php',
);
public static function onLoad()
{
//Yii::setAlias('@mymodulealias', static::staticBasePath());

self::registerTranslation('mymodule', static::staticBasePath() . '/messages', [
'fileMap' => [
'mymodule' => 'mymodule.php',
],
]);
}

public static function t($message, array $params = [])
{
return parent::baseT('mymodule', $message, $params);
}
}
```


The above registered module translation messages can be retrieved as `Yii::t('mymodule', 'Key', 'Value')` or for the subsection `Yii::t('mymodule/subsection', 'Key', 'Value')`.
The above registered module translation messages can be retrieved as `Module::t('Key', 'Value')`.

### Message Source Content

Expand Down
4 changes: 3 additions & 1 deletion envs/dev/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"fabpot/goutte" : "^3.1",
"jakeasmith/http_build_url" : "^0.1",
"ifsnop/mysqldump-php":"2.*",
"paypal/rest-api-sdk-php" : "~1.6.0"
"paypal/rest-api-sdk-php" : "~1.6.0",
"luyadev/luya-bootstrap4": "^1.0@dev",
"luyadev/luya-composer": "^1.0"
},
"extra": {
"asset-installer-paths": {
Expand Down
122 changes: 120 additions & 2 deletions envs/dev/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 7 additions & 13 deletions modules/admin/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,12 @@ final class Module extends \luya\admin\base\Module implements CoreModuleInterfac
*/
public $moduleMenus = [];

/**
* @var array Registering translation files for the admin module.
*/
public $translations = [
[
'prefix' => 'admin*',
'basePath' => '@admin/messages',
'fileMap' => [
'admin' => 'admin.php',
],
],
];
public static function onLoad()
{
self::registerTranslation('admin*', '@admin/messages', [
'admin' => 'admin.php',
]);
}

/**
* Returns all Asset files to registered in the administration interfaces.
Expand Down Expand Up @@ -318,6 +312,6 @@ public function import(ImportControllerInterface $import)
*/
public static function t($message, array $params = [], $language = null)
{
return Yii::t('admin', $message, $params, $language);
return parent::baseT('admin', $message, $params, $language);
}
}
11 changes: 9 additions & 2 deletions modules/cms/src/admin/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getJsTranslationMessages()
];
}

public $translations = [
public static $translations = [
[
'prefix' => 'cmsadmin*',
'basePath' => '@cmsadmin/messages',
Expand All @@ -116,6 +116,13 @@ public function getJsTranslationMessages()
],
];

public static function onLoad()
{
self::registerTranslation('cmsadmin*', '@cmsadmin/messages', [
'cmsadmin' => 'cmsadmin.php',
]);
}

/**
* @var array Defined blocks to hidde from the cmsadmin. Those blocks are not listed in the Page Content blocks overview. You can override this
* variable inside your configuration of the cmsadmin.
Expand Down Expand Up @@ -243,7 +250,7 @@ public function import(ImportControllerInterface $importer)
*/
public static function t($message, array $params = [])
{
return Yii::t('cmsadmin', $message, $params);
return parent::baseT('cmsadmin', $message, $params);
}

private static $_authorUserId = 0;
Expand Down
Loading

0 comments on commit 551e8ce

Please sign in to comment.