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