1f800af69SMichael Große<?php 2f800af69SMichael Große 3f800af69SMichael Großenamespace dokuwiki\plugin\struct\meta; 4f800af69SMichael Große 5d6d97f60SAnna Dabrowskatrait TranslationUtilities 6d6d97f60SAnna Dabrowska{ 7f800af69SMichael Große /** 8f800af69SMichael Große * Add the translatable keys to the configuration 9f800af69SMichael Große * 10f800af69SMichael Große * This checks if a configuration for the translation plugin exists and if so 11f800af69SMichael Große * adds all configured languages to the config array. This ensures all types 12f800af69SMichael Große * can have translatable labels. 13f800af69SMichael Große * 14f800af69SMichael Große * @param string[] $keysToInitialize the keys for which to initialize language fields 15f800af69SMichael Große */ 16*7234bfb1Ssplitbrain protected function initTransConfig(array $keysToInitialize = ['label', 'hint']) 17d6d97f60SAnna Dabrowska { 18f800af69SMichael Große global $conf; 19f800af69SMichael Große $lang = $conf['lang']; 20f800af69SMichael Große if (isset($conf['plugin']['translation']['translations'])) { 21f800af69SMichael Große $lang .= ' ' . $conf['plugin']['translation']['translations']; 22f800af69SMichael Große } 23f800af69SMichael Große $langs = explode(' ', $lang); 24f800af69SMichael Große $langs = array_map('trim', $langs); 25f800af69SMichael Große $langs = array_filter($langs); 26f800af69SMichael Große $langs = array_unique($langs); 27f800af69SMichael Große 28f800af69SMichael Große foreach ($keysToInitialize as $key) { 29f800af69SMichael Große if (!isset($this->config[$key])) { 30*7234bfb1Ssplitbrain $this->config[$key] = []; 31f800af69SMichael Große } 32f800af69SMichael Große // initialize missing keys 33f800af69SMichael Große foreach ($langs as $lang) { 34f800af69SMichael Große if (!isset($this->config[$key][$lang])) { 35f800af69SMichael Große $this->config[$key][$lang] = ''; 36f800af69SMichael Große } 37f800af69SMichael Große } 38f800af69SMichael Große // strip unknown languages 39f800af69SMichael Große foreach (array_keys($this->config[$key]) as $langKey) { 40f800af69SMichael Große if (!in_array($langKey, $langs)) { 41f800af69SMichael Große unset($this->config[$key][$langKey]); 42f800af69SMichael Große } 43f800af69SMichael Große } 44f800af69SMichael Große } 45f800af69SMichael Große } 46f800af69SMichael Große 47f800af69SMichael Große /** 48f800af69SMichael Große * Returns the translated key 49f800af69SMichael Große * 50f800af69SMichael Große * Uses the current language as determined by $conf['lang']. Falls back to english 51f800af69SMichael Große * and then to the provided default 52f800af69SMichael Große * 53f800af69SMichael Große * @param string $key 54f800af69SMichael Große * @param string $default the default to return if there is no translation set for $key 55f800af69SMichael Große * 56f800af69SMichael Große * @return string 57f800af69SMichael Große */ 58d6d97f60SAnna Dabrowska public function getTranslatedKey($key, $default) 59d6d97f60SAnna Dabrowska { 60f800af69SMichael Große global $conf; 61f800af69SMichael Große $lang = $conf['lang']; 62f800af69SMichael Große if (!blank($this->config[$key][$lang])) { 63f800af69SMichael Große return $this->config[$key][$lang]; 64f800af69SMichael Große } 65f800af69SMichael Große if (!blank($this->config[$key]['en'])) { 66f800af69SMichael Große return $this->config[$key]['en']; 67f800af69SMichael Große } 68f800af69SMichael Große return $default; 69f800af69SMichael Große } 70f800af69SMichael Große} 71