aboutsummaryrefslogtreecommitdiff
path: root/lib/plugins/core/TranslatablePluginTrait.php
blob: 15bed9329720deffda0cb17e8bae00eef7ee40de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
 * Trait used to allow plugins to be translated in a generic way.
 *
 * @author  Jan-Hendrik Willms <tleilax+studip@gmail.com>
 * @license GPL2 or any later version
 * @since Stud.IP 5.0
 */
trait TranslatablePluginTrait
{
    protected $translation_domain = null;

    /**
     * Initializes the translation for the plugin.
     *
     * @param string $domain
     */
    protected function initializeTranslation($domain)
    {
        bindtextdomain($domain, $this->getPluginPath() . '/locale');
        bind_textdomain_codeset($domain, 'UTF-8');
        $this->translation_domain = $domain;
    }

    /**
     * Returns the defined translation domain from plugin manifest. If none
     * is set, false is returned.
     *
     * @return false|string The translation domain from manifest, if set
     */
    protected function getTranslationDomain()
    {
        if ($this->translation_domain === null) {
            $manifest = $this->getMetadata();
            $this->translation_domain = $manifest['localedomain'] ?? false;

            if ($this->translation_domain !== false) {
                $this->initializeTranslation($this->translation_domain);
            }
        }
        return $this->translation_domain;
    }

    /**
     * Returns whether the plugin has a translation defined or not.
     *
     * @return bool
     */
    public function hasTranslation()
    {
        return $this->getTranslationDomain() !== false;
    }

    /**
     * Plugin localization for a single string.
     *
     * @param string $string String to translate
     * @return string
     */
    public function _($string)
    {
        $domain = $this->getTranslationDomain();
        if (!$domain) {
            return $string;
        }

        $result = dgettext($domain, $string);

        // Fallback to possible translations from core system
        if ($result === $string) {
            $result = _($string);
        }

        return $result;
    }

    /**
     * Plugin localization for plural strings.
     *
     * @param string $string0 String to translate (singular)
     * @param string $string1 String to translate (plural)
     * @param mixed  $n       Quantity factor (may be an array or array-like)
     * @return string
     */
    public function _n($string0, $string1, $n)
    {
        if (is_array($n)) {
            $n = count($n);
        }

        $domain = $this->getTranslationDomain();
        if (!$domain) {
            return $n == 1 ? $string0 : $string1;
        }

        $result = dngettext($domain, $string0, $string1, $n);

        // Fallback to possible translations from core system
        if ($result === $string0 || $result === $string1) {
            $result = ngettext($string0, $string1, $n);
        }

        return $result;
    }
}