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