-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b49733c
Showing
7 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace LavoWeb\TranslationInherit\Framework; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\ObjectManager; | ||
|
||
class Translate extends \Magento\Framework\Translate | ||
{ | ||
const PARENT_LOCALE_PATH = 'general/locale/parent_code'; | ||
|
||
/** | ||
* Initialize translation data | ||
* @override If parent locale is set in configuration, we load that translations first | ||
* | ||
* @param string|null $area | ||
* @param bool $forceReload | ||
* @return $this | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function loadData($area = null, $forceReload = false) | ||
{ | ||
$this->_data = []; | ||
if ($area === null) { | ||
$area = $this->_appState->getAreaCode(); | ||
} | ||
$this->setConfig( | ||
[ | ||
self::CONFIG_AREA_KEY => $area, | ||
] | ||
); | ||
|
||
if (!$forceReload) { | ||
$data = $this->_loadCache(); | ||
if (false !== $data) { | ||
$this->_data = $data; | ||
return $this; | ||
} | ||
} | ||
|
||
// Begin override | ||
$parentLocale = $this->getParentLocale(); | ||
$currentLocale = $this->getLocale(); | ||
if ($parentLocale != '' && $parentLocale != $currentLocale) { | ||
$this->setLocale($parentLocale); | ||
|
||
$this->_loadModuleTranslation(); | ||
$this->_loadPackTranslation(); | ||
$this->_loadThemeTranslation(); | ||
$this->_loadDbTranslation(); | ||
|
||
$this->setLocale($currentLocale); | ||
} | ||
// End override | ||
|
||
$this->_loadModuleTranslation(); | ||
$this->_loadPackTranslation(); | ||
$this->_loadThemeTranslation(); | ||
$this->_loadDbTranslation(); | ||
|
||
if (!$forceReload) { | ||
$this->_saveCache(); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get parent locale | ||
* | ||
* @return string | ||
*/ | ||
protected function getParentLocale() | ||
{ | ||
$config = ObjectManager::getInstance()->get(ScopeConfigInterface::class); | ||
return $config->getValue( | ||
self::PARENT_LOCALE_PATH, | ||
\Magento\Store\Model\ScopeInterface::SCOPE_STORE | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Translation Inherit | ||
|
||
Translation inherit/locale fallback for Magento 2 | ||
|
||
## Installation | ||
|
||
``` | ||
composer require lavoweb/translationinherit | ||
php bin/magento setup:upgrade | ||
``` | ||
|
||
## Configuration | ||
|
||
Stores => Configuration => General => Locale Options => Parent Locale | ||
|
||
If parent locale is set and haven't the same value than current locale, they'll be merged. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "lavoweb/translationinherit", | ||
"description": "Translation inherit/locale fallback for Magento 2", | ||
"require": { | ||
"magento/magento-composer-installer": "*" | ||
}, | ||
"type": "magento2-module", | ||
"version": "0.1.0", | ||
"license": [ | ||
|
||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"LavoWeb\\TranslationInherit\\": "" | ||
} | ||
}, | ||
"extra": { | ||
"map": [ | ||
[ | ||
"*", | ||
"LavoWeb/TranslationInherit" | ||
] | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<section id="general"> | ||
<group id="locale"> | ||
<field id="parent_code" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Parent Locale</label> | ||
<source_model>Magento\Config\Model\Config\Source\Locale</source_model> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Magento\Framework\Translate" type="LavoWeb\TranslationInherit\Framework\Translate" /> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="LavoWeb_TranslationInherit" setup_version="0.1.0" /> | ||
</config> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
\Magento\Framework\Component\ComponentRegistrar::register( | ||
\Magento\Framework\Component\ComponentRegistrar::MODULE, | ||
'LavoWeb_TranslationInherit', | ||
__DIR__ | ||
); |