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