1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5trait TranslationUtilities { 6 7 8 /** 9 * Add the translatable keys to the configuration 10 * 11 * This checks if a configuration for the translation plugin exists and if so 12 * adds all configured languages to the config array. This ensures all types 13 * can have translatable labels. 14 * 15 * @param string[] $keysToInitialize the keys for which to initialize language fields 16 */ 17 protected function initTransConfig(array $keysToInitialize = array('label', 'hint')) { 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] = array(); 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 /** 49 * Returns the translated key 50 * 51 * Uses the current language as determined by $conf['lang']. Falls back to english 52 * and then to the provided default 53 * 54 * @param string $key 55 * @param string $default the default to return if there is no translation set for $key 56 * 57 * @return string 58 */ 59 public function getTranslatedKey($key, $default) { 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