Skip to content

[2.2 develop] [backport #19018] [issue #17833] child theme does not inherit translations from parent theme #19023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

78 changes: 74 additions & 4 deletions lib/internal/Magento/Framework/Translate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework;

Expand Down Expand Up @@ -327,16 +328,21 @@ protected function _addData($data)
}

/**
* Load current theme translation
* Load current theme translation according to fallback
*
* @return $this
*/
protected function _loadThemeTranslation()
{
$file = $this->_getThemeTranslationFile($this->getLocale());
if ($file) {
$this->_addData($this->_getFileData($file));
$themeFiles = $this->getThemeTranslationFilesList($this->getLocale());

/** @var string $file */
foreach ($themeFiles as $file) {
if ($file) {
$this->_addData($this->_getFileData($file));
}
}

return $this;
}

Expand Down Expand Up @@ -377,11 +383,75 @@ protected function _getModuleTranslationFile($moduleName, $locale)
return $file;
}

/**
* Get theme translation locale file name
*
* @param string $locale
* @param array $config
* @return string|null
*/
private function getThemeTranslationFileName(string $locale, array $config): ?string
{
$fileName = $this->_viewFileSystem->getLocaleFileName(
'i18n' . '/' . $locale . '.csv',
$config
);

return $fileName ? $fileName : null;
}

/**
* Get parent themes for the current theme in fallback order
*
* @return array
*/
private function getParentThemesList(): array
{
$themes = [];

$parentTheme = $this->_viewDesign->getDesignTheme()->getParentTheme();
while ($parentTheme) {
$themes[] = $parentTheme;
$parentTheme = $parentTheme->getParentTheme();
}
$themes = array_reverse($themes);

return $themes;
}

/**
* Retrieve translation files for themes according to fallback
*
* @param string $locale
*
* @return array
*/
private function getThemeTranslationFilesList($locale): array
{
$translationFiles = [];

/** @var \Magento\Framework\View\Design\ThemeInterface $theme */
foreach ($this->getParentThemesList() as $theme) {
$config = $this->_config;
$config['theme'] = $theme->getCode();
$translationFiles[] = $this->getThemeTranslationFileName($locale, $config);
}

$translationFiles[] = $this->getThemeTranslationFileName($locale, $this->_config);

return $translationFiles;
}


/**
* Retrieve translation file for theme
*
* @param string $locale
* @return string
*
* @deprecated
*
* @see \Magento\Framework\Translate::getThemeTranslationFilesList
*/
protected function _getThemeTranslationFile($locale)
{
Expand Down