diff --git a/config/config.php b/config/config.php index 848e41a..be11bf1 100644 --- a/config/config.php +++ b/config/config.php @@ -12,6 +12,8 @@ * @author Martin Auswöger */ +$GLOBALS['TL_HOOKS']['loadLanguageFile'][] = array('MadeYourDay\\Contao\\CustomElements', 'loadLanguageFileHook'); + $GLOBALS['BE_FFL']['rsce_list_start'] = 'MadeYourDay\\Contao\\Widget\\ListStart'; $GLOBALS['BE_FFL']['rsce_list_stop'] = 'MadeYourDay\\Contao\\Widget\\ListStop'; $GLOBALS['BE_FFL']['rsce_list_item_start'] = 'MadeYourDay\\Contao\\Widget\\ListItemStart'; diff --git a/src/MadeYourDay/Contao/CustomElements.php b/src/MadeYourDay/Contao/CustomElements.php index 23da70d..f635e07 100644 --- a/src/MadeYourDay/Contao/CustomElements.php +++ b/src/MadeYourDay/Contao/CustomElements.php @@ -446,8 +446,16 @@ protected function createDcaItem($fieldPrefix, $fieldName, $fieldConfig, &$palet throw new \Exception('Field name must not start or end with "_" (' . $this->getDcaFieldValue($dc, 'type') . ': ' . $fieldName . ').'); } + if (isset($fieldConfig['label'])) { + $fieldConfig['label'] = static::getLabelTranslated($fieldConfig['label']); + } + if ($fieldConfig['inputType'] === 'list') { + if (isset($fieldConfig['elementLabel'])) { + $fieldConfig['elementLabel'] = static::getLabelTranslated($fieldConfig['elementLabel']); + } + $GLOBALS['TL_DCA'][$dc->table]['fields'][$fieldPrefix . $fieldName . '_rsce_list_start'] = array( 'label' => $fieldConfig['label'], 'inputType' => 'rsce_list_start', @@ -1003,14 +1011,14 @@ public static function loadConfig() } if (in_array('content', $element['types'])) { $contents[] = '$GLOBALS[\'TL_CTE\'][\'' . $element['contentCategory'] . '\'][\'' . $element['template'] . '\'] = \'MadeYourDay\\\\Contao\\\\Element\\\\CustomElement\';'; - $contents[] = '$GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'] = ' . var_export($element['label'], true) . ';'; + $contents[] = '$GLOBALS[\'TL_LANG\'][\'CTE\'][\'' . $element['template'] . '\'] = \\MadeYourDay\\Contao\\CustomElements::getLabelTranslated(' . var_export($element['label'], true) . ');'; if (!empty($element['config']['wrapper']['type'])) { $contents[] = '$GLOBALS[\'TL_WRAPPERS\'][' . var_export($element['config']['wrapper']['type'], true) . '][] = ' . var_export($element['template'], true) . ';'; } } if (in_array('module', $element['types'])) { $contents[] = '$GLOBALS[\'FE_MOD\'][\'' . $element['moduleCategory'] . '\'][\'' . $element['template'] . '\'] = \'MadeYourDay\\\\Contao\\\\Element\\\\CustomElement\';'; - $contents[] = '$GLOBALS[\'TL_LANG\'][\'FMD\'][\'' . $element['template'] . '\'] = ' . var_export($element['label'], true) . ';'; + $contents[] = '$GLOBALS[\'TL_LANG\'][\'FMD\'][\'' . $element['template'] . '\'] = \\MadeYourDay\\Contao\\CustomElements::getLabelTranslated(' . var_export($element['label'], true) . ');'; } } @@ -1092,4 +1100,60 @@ protected static function refreshOpcodeCache($path) return true; } + + /** + * Reload translated labels if default language file gets loaded + * + * @param string $name + * @param string $language + * @return void + */ + public function loadLanguageFileHook($name, $language) + { + if ($name === 'default') { + static::loadConfig(); + } + } + + /** + * Return translated label if label configuration contains language keys + * + * @param array $labelConfig + * @return mixed Translated label if exists, otherwise $labelConfig + */ + public static function getLabelTranslated($labelConfig) + { + if (!is_array($labelConfig)) { + return $labelConfig; + } + + // Return if it isn't an associative array + if (!count(array_filter(array_keys($labelConfig), 'is_string'))) { + return $labelConfig; + } + + $language = str_replace('-', '_', $GLOBALS['TL_LANGUAGE']); + if (isset($labelConfig[$language])) { + return $labelConfig[$language]; + } + + // Try the short language code + $language = substr($language, 0, 2); + if (isset($labelConfig[$language])) { + return $labelConfig[$language]; + } + + // Fall back to english + $language = 'en'; + if (isset($labelConfig[$language])) { + return $labelConfig[$language]; + } + + // Return the first item that seems to be a language key + foreach ($labelConfig as $key => $label) { + if (strlen($key) === 2 || substr($key, 2, 1) === '_') { + return $label; + } + } + } }