Skip to content

Commit

Permalink
first release
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienLavorel committed May 3, 2019
0 parents commit b49733c
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Framework/Translate.php
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
);
}
}
16 changes: 16 additions & 0 deletions Readme.md
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.
28 changes: 28 additions & 0 deletions composer.json
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"
]
]
}
}
13 changes: 13 additions & 0 deletions etc/adminhtml/system.xml
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>
4 changes: 4 additions & 0 deletions etc/di.xml
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>
5 changes: 5 additions & 0 deletions etc/module.xml
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>
7 changes: 7 additions & 0 deletions registration.php
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__
);

0 comments on commit b49733c

Please sign in to comment.